soucevi1_dist_chat package¶
Submodules¶
soucevi1_dist_chat.CMessage module¶
-
class
soucevi1_dist_chat.CMessage.
CMessage
(**kwargs)¶ Bases:
object
Class representing a single instance of a message that is sent during chatting or inside mechanisms of the program.
-
convert_to_json
()¶ Convert message to JSON.
Returns: Message in the JSON format
-
convert_to_string
()¶ Convert message to string
Returns: Message JSON in string format
-
from_json
(received_json)¶ Convert JSON to the CMessage class.
Parameters: received_json – JSON by the server
-
from_string
(message_string)¶ Convert string to CMessage.
Parameters: message_string – JSON in string format Returns:
-
print
()¶ Print the message together with its info.
-
-
class
soucevi1_dist_chat.CMessage.
MessageType
¶ Bases:
enum.Enum
Class wrapping the types of messages used in the program
- user_message: chat message sent by another user
- election_message: message used during the elections
- login_message: message sent by the node that wants to log in
- prev_inform_message: previous node is dead, inform its previous node about address and port to connect to
- i_am_prev_message: message sent by new prev node to its next node
- hello_leader_message: let the leader know about new node
- elected_message: leader is already elected
-
elected_message
= 7¶
-
election_message
= 6¶
-
hello_leader_message
= 5¶
-
i_am_prev_message
= 4¶
-
login_message
= 2¶
-
prev_inform_message
= 3¶
-
user_message
= 1¶
soucevi1_dist_chat.CNode module¶
-
class
soucevi1_dist_chat.CNode.
CNode
(is_leader, address, port, neighbor_address, neighbor_port, name='')¶ Bases:
object
Class that represents the node – one participant of the chat.
-
add_connection_record
(message, reader)¶ Add new connection to the broadcasting list.
Parameters: - message – Received user message
- reader – Stream reader
-
connect_to_leader
()¶
-
craft_message
(m_type, data)¶ Create CMessage instance with information about the sender (current node), given type and body. First, logical clock needs to be incremented.
Parameters: - m_type (MessageType) – Type of the message
- data – Main body of the message.
Returns: CMessage instance
-
distribute_message
(message, exc=None)¶ Distribute message to all known nodes.
Parameters: - message – User message
- exc – Exception in distribution: IP and port of a node that will not receive the message
-
find_in_connections
(reader)¶ Find if there is a record of the connection represented by the given stream writer.
Parameters: reader – Stream writer Returns: True if writer is in connections, False otherwise.
-
handle_elected_message
(message)¶ When received elected_message, it means the new leader is known already. This message should run around the circle until it comes to the sender (leader) again. All non-leader nodes should connect to the leader.
Parameters: message – CMessage with new leader information
-
handle_election_message
(message)¶ Election messages are the cli_main communication during the Chang-Roberts algorithm. The node with the biggest ID (concatenated string “IP+port”) will become the next leader. This coroutine is the cli_main body of the Chang-Roberts algorithm.
Parameters: message – Election CMessage from the previous node
-
handle_hello_leader_message
(message, reader)¶ New node registers to the message broadcast.
Parameters: - message –
- reader –
-
handle_i_am_prev_message
(reader, writer)¶ The previous node of the current node has changed and it’s informing this node about being its new prev, so it can update the instance streams.
Parameters: - reader – Stream reader of the new previous node
- writer – Stream writer of the new previous node
-
handle_login_message
(message, writer)¶ A new node wants to log in. Make the new node this node’s next and tell it where to connect (its next is this node’s old next).
Parameters: - message – Login message to handle
- writer – Writer stream of the new node
-
handle_message
(message, reader, writer)¶ Decide what to do with received message.
Parameters: - message (CMessage) – Received message to handle
- reader – Reader/writer pair of the message sender
- writer – Reader/writer pair of the message sender
-
handle_prev_inform_message
(message)¶ A node died. Its next node detects it and sends a message to the ring. If this node’s next is dead, its new next node is the sender of the message.
Parameters: message – Message to handle
-
handle_user_message
(message, reader)¶ The node received the user message. If it it not a leader node, it just displays the message to the console. If it is a leader, it must distribute the message to all the nodes in the ring.
Parameters: - message – User message to handle
- reader – Stream reader
-
initiate_election
()¶ After the death of the leader, the ring must be renewed at first. After the ring is renewed, the node who was previous to the leader initiates the election algorithm.
-
join_the_ring
()¶ Send message to the open connection to join the ring. The open connection becomes the previous node. The previous node sends a message with information about its next node, that is to become this node’s next node.
-
print_user_message
(message)¶ Print received user message.
Parameters: message – CMessage to be printed.
-
read_input
()¶ Coroutine reading the user’s keyboard input and transforming it into the CMessage. If ‘//exit’ is read here, the program exits.
-
read_socket
()¶ Coroutine keeping the connection to the next node alive. No data is expected except for when the next node dies.
-
remove_from_connections
(reader, writer)¶ Remove the connection represented by streams in argument form the leader connections list.
Parameters: - reader – Stream reader of the removed connection
- writer – Stream writer of the removed connection
-
run
()¶ The cli_main function of the node. Opens connection to the given node, which helps to connect this node into the ring. Starts 3 coroutines: one is reading the user input, one is watching the open connection and one is listening to incoming connections.
-
run_server
(reader, writer)¶ Callback made whenever a connection with the server is made. Try to read some data. If the writer is closing, return (callback ends, there is nothing else to do). If the received data is empty, and this node is exiting, return. If the data is empty and the prev reader is not the current reader, return (the previous node must have changed). Otherwise if the data is empty, close the connection, set the prev streams to none and inform the dead previous’ previous node where to connect. If the data is OK, create a CMessage instance out of them and handle the message.
Parameters: - reader – AsyncIO StreamReader, high level socket for receiving data from the connection.
- writer – ASyncIO StreamWriter, high level socket for sending data through the connection.
-
send_hello_from_leader
(message)¶ The leader node distributes a message telling everyone in the ring (except the new node) that a new node joined.
Parameters: message – Original hello message
-
send_message_to_leader
(message)¶ Send given message directly to the leader node.
Parameters: message – CMessage to be sent
-
send_message_to_ring
(message)¶ Send given message to the next node in the ring.
Parameters: message – CMessage to be sent
-
send_prev_inform_message
()¶ Inform the node whose next is dead to connect to this node.
-
send_user_message
(message)¶ Send user input as a message to the leader node.
Parameters: message – CMessage made from the user input.
-
server_init
()¶ Initialize the server coroutine, make it run forever.
-
set_logical_clock
(other_clock)¶ Synchronize the logical (Lamport) clock of this node.
Parameters: other_clock – Logical clock of the node it is synchronizing with.
-
wait_for_connection
()¶ The leader is alone and must wait for the first connection is order to continue creating the ring.
-
soucevi1_dist_chat.cli module¶
The main entry module for the command-line interface.
cli_main¶
Main function of the CLI program. Runs one instance of the node with given parameters.
param leader: | Bool flag saying if the started node is the leader of a new chatroom. |
---|---|
param address: | IP address of this node. |
param port: | Port the node will listen to and send from |
param neighbor_address: | |
IP adderss of the neighbor | |
param neighbor_port: | |
Port of the neighbor | |
param name: | Name of the user |
param verbose: | If set, log to the file as well as to stdout. If not, log only to file. |
cli_main [OPTIONS]
Options
-
-l
,
--leader
,
-L
,
--not_leader
¶
This is the first node to started in this chatroom, e.g. the leader
-
-a
,
--address
<address>
¶ IP address this node will run on [required]
-
-p
,
--port
<port>
¶ Port this node will bind to
-
-na
,
--neighbor-address
<neighbor_address>
¶ IP address of the neighbor
-
-np
,
--neighbor-port
<neighbor_port>
¶ Port of the neighbor
-
-n
,
--name
<name>
¶ Name that will be displayed to other participants
-
-v
,
--verbose
¶
Logging not only to file, but also to stdout.
soucevi1_dist_chat.colors module¶
Class used to print colored output to the console.