Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit db69aa7

Browse files
author
Jakhongir
committed
Documantation is written. Ready for a release
1 parent 216e588 commit db69aa7

File tree

10 files changed

+79
-16
lines changed

10 files changed

+79
-16
lines changed

.idea/workspace.xml

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
## SmartChat
1+
# SmartChat
22

33
SmartChat is application for chating between two and more nodes.
44

55
This 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
1012
First 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 .
3133
Default port is __7777__
3234

3335
Or, you can download the package and run `bash instal.sh`. It will require sudo user password.
34-
### Commands
36+
## Commands
3537
Application 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
6769
This command allows you unexpected close the application even if you are connected to a network. In that case you
6870
don't care what will be with them. It is not your business.
6971

70-
#### But really. What happens?
72+
### But really. What happens?
7173
Nothing. When other nodes will receive no sign from you, they repair the network. See repair section.
7274

73-
### Connection
75+
## Connection
7476
For connection, you will be need a host and port of at least one member of network. Or another alone node.
7577
The topology of network is __ring__. Every new node will be trying to join the ring. See Repair section for more.
7678

7779
For understanding how works connection look into [diagram meaning](DIAGRAM.md)
7880

79-
### Handshake
81+
## Handshake
8082
When node is trying to join the network, it sends __handshake__ message and gets __handshaked__ when joining is
8183
established. Now it can fully access all function of chat.
8284
From 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__.
8890
To send a message to __all members__ just type it and press enter. Sooner or later your message will get all members
8991
of the network.
9092

91-
#### How it works?
93+
### How it works?
9294
Simple. You send a message only to next node. When next node receive it - it checks for a stamp. Stamp can be made only
9395
by 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
9799
topology sooner or later the message should get it owner. If he checks, that message has not been stamped yet,
98100
that means - the leader doesn't exist. This is the moment, when __election__ starting.
99101

100-
### Election
102+
## Election
101103
The [Chang and Roberts algorithm](https://en.wikipedia.org/wiki/Chang_and_Roberts_algorithm) is a ring-based
102104
coordinator election algorithm. In our case leader is needed for creating stamps on messages.
103105

104-
#### How election works?
106+
### How election works?
105107
1. Initially each node in the ring is marked as non-participant.
106108
2. 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.
133135
If 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.
135138
What happens when you're leaving the network by typing `disconnect` or unexpectedly close application?
136139

137140
Thanks to WebSockets, every action you made are sent to server/client even if you're disconnecting. So, when you are
138141
leaving the NEXT and PREVIOUS nodes will know it.
139142

140143
Let'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)

static/exit_a/1.png

18.3 KB
Loading

static/exit_a/2.png

20.6 KB
Loading

static/exit_a/3.png

32.2 KB
Loading

static/exit_a/4.png

16.5 KB
Loading

static/join_a/1.png

19.2 KB
Loading

static/join_a/2.png

20 KB
Loading

static/join_a/3.png

22.7 KB
Loading

static/join_a/4.png

26.4 KB
Loading

0 commit comments

Comments
 (0)