1- ## SmartChat
1+ # SmartChat
22
33SmartChat is application for chating between two and more nodes.
44
55This is an example of Chang Roberts Leader election algorithm.
66
7- Connection are mannaged by WebSockets.
7+ Connections are mannaged by WebSockets.
88
9- ### Getting started
9+ Author is Tashpulatov Jakhongir.
10+
11+ ## Getting started
1012First install Node.js v14.x. If you have already installed it, you can skip this step.
1113
1214``` shell
@@ -31,7 +33,7 @@ $ PORT=8080 node .
3133Default port is __ 7777__
3234
3335Or, you can download the package and run ` bash instal.sh ` . It will require sudo user password.
34- ### Commands
36+ ## Commands
3537Application supports this non-trivial commands:
3638> ` status `
3739
@@ -67,16 +69,16 @@ This command allows you to export all messages to file. It will be named that wa
6769This command allows you unexpected close the application even if you are connected to a network. In that case you
6870don't care what will be with them. It is not your business.
6971
70- #### But really. What happens?
72+ ### But really. What happens?
7173Nothing. When other nodes will receive no sign from you, they repair the network. See repair section.
7274
73- ### Connection
75+ ## Connection
7476For connection, you will be need a host and port of at least one member of network. Or another alone node.
7577The topology of network is __ ring__ . Every new node will be trying to join the ring. See Repair section for more.
7678
7779For understanding how works connection look into [ diagram meaning] ( DIAGRAM.md )
7880
79- ### Handshake
81+ ## Handshake
8082When node is trying to join the network, it sends __ handshake__ message and gets __ handshaked__ when joining is
8183established. Now it can fully access all function of chat.
8284From this moment it has a node whom it connected to(__ NEXT__ node) and node who connected to it(__ PREVIOUS__ node).
@@ -88,7 +90,7 @@ the mode by typing empty string and press __Enter__.
8890To send a message to __ all members__ just type it and press enter. Sooner or later your message will get all members
8991of the network.
9092
91- #### How it works?
93+ ### How it works?
9294Simple. You send a message only to next node. When next node receive it - it checks for a stamp. Stamp can be made only
9395by the leader.
9496
@@ -97,11 +99,11 @@ he did it and who did it. If he is not the leader it sends the message to next n
9799topology sooner or later the message should get it owner. If he checks, that message has not been stamped yet,
98100that means - the leader doesn't exist. This is the moment, when __ election__ starting.
99101
100- ### Election
102+ ## Election
101103The [ Chang and Roberts algorithm] ( https://en.wikipedia.org/wiki/Chang_and_Roberts_algorithm ) is a ring-based
102104coordinator election algorithm. In our case leader is needed for creating stamps on messages.
103105
104- #### How election works?
106+ ### How election works?
1051071 . Initially each node in the ring is marked as non-participant.
1061082 . A node that notices a lack of leader(see message section) starts an election. It
107109 creates an election message containing its UUID (v1 for comparing). It then sends this message to next node
@@ -128,13 +130,65 @@ When a node starts acting as the leader, it begins the second stage of the algor
128130 over.
129131
130132
131- ### Repair
132- Repair is algorithm for repairing network when someone leaves it or joins it. Let's look into this more .
133+ ## Repair
134+ Repair is algorithm for repairing network when someone leaves or joins it. Let's look.
133135If you don't understand diagrams, look into [ diagram meaning] ( DIAGRAM.md )
134- #### The sitaution: there is already a network, and you are connected to it assuming that there are N > 2 nodes or more.
136+
137+ ### The situation: there is already a network, and you are connected to it assuming that there are N > 2 nodes.
135138What happens when you're leaving the network by typing ` disconnect ` or unexpectedly close application?
136139
137140Thanks to WebSockets, every action you made are sent to server/client even if you're disconnecting. So, when you are
138141leaving the NEXT and PREVIOUS nodes will know it.
139142
140143Let's assume there is a network about 5 nodes. And you're the node A.
144+
145+ ![ 1] ( ./static/exit_a/1.png )
146+
147+ And you're disconnecting/leaving. You broke connection with NEXT and PREVIOUS nodes:
148+
149+ ![ 2] ( ./static/exit_a/2.png )
150+
151+ When B knows that you disconnected, it will do nothing. He is the weak node in a broken network.
152+
153+ When E knows that you disconnected, it will start repairing network by sending message containing his URL to his NEXT
154+ node - D
155+
156+ If D has connected NEXT node, we know it has(C), it sends the message to C. And so on. they will be sending to next
157+ the message nodes until they found a node without connected client(NEXT node). That means they found the weak node.
158+
159+ ![ 3] ( ./static/exit_a/3.png )
160+
161+ When B gets the message containing E's URL, it will connect to it. And that it is. __ Network is repaired__
162+
163+ ![ 4] ( ./static/exit_a/4.png )
164+
165+
166+ ### The situation: there is already a network, and you are connected to it assuming that there are 2 nodes.
167+
168+ Simple. When you're leaving, that means other node disconnects too, and you're both alone nodes now.
169+
170+ ### The situation: there is already a network, and you are NOT connected to it. And you want to join the network.
171+
172+ Let's assume you're node A.
173+
174+ ![ 1] ( ./static/join_a/1.png )
175+
176+ You are connecting to node E, so you run ` connect ` and enter E's URL.
177+
178+ ![ 2] ( ./static/join_a/2.png )
179+
180+ When E accept your connection you need to send ` handshake ` to join the network. Without it you're not a member of the
181+ network, but just connected to E.
182+
183+ ![ 3] ( ./static/join_a/3.png )
184+
185+ Now when E know about you, he disconnectes from B and send you ` hadshaked ` message. That means, that you're connected
186+ to network. But as you can see now, the network is broken. There is no connection between A and B. So you have to repair
187+ the network by sending repair message as shown in another situation(the first one)
188+
189+ ![ 4] ( ./static/join_a/4.png )
190+
191+ When B gets message to repair, it will connect to you and now the network is fixed.
192+
193+
194+ ![ 5] ( ./static/exit_a/1.png )
0 commit comments