Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DBE-DS-Group1-WS24
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robin Leber
DBE-DS-Group1-WS24
Commits
b3e0975e
Commit
b3e0975e
authored
1 year ago
by
Robin Leber
Browse files
Options
Downloads
Patches
Plain Diff
leader election funktioniert
parent
ffdb0c68
No related branches found
No related tags found
1 merge request
!2
leader election und heartbeat
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.py
+73
-62
73 additions, 62 deletions
server.py
with
73 additions
and
62 deletions
server.py
+
73
−
62
View file @
b3e0975e
...
...
@@ -17,7 +17,7 @@ SERVER_MULTICAST_PORT = 5974
CLIENT_MULTICAST_PORT
=
5973
# Listening port ring
RING_PORT
=
10001
RING_PORT
=
5972
# Local host information
MY_HOST
=
socket
.
gethostname
()
...
...
@@ -50,7 +50,7 @@ class Server():
# if my IP is not in the server list add it
if
MY_IP
not
in
self
.
serverList
:
self
.
serverList
.
append
(
MY_IP
)
self
.
basic_lcr
()
# create socket bind to server address
multicast_listen_sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
...
...
@@ -75,7 +75,7 @@ class Server():
if
newServer_address
not
in
self
.
serverList
:
self
.
serverList
.
append
(
newServer_address
)
self
.
basic_lcr
()
reply_message
=
MY_IP
multicast_listen_sock
.
sendto
(
str
.
encode
(
reply_message
),
address
)
...
...
@@ -86,7 +86,6 @@ class Server():
time
.
sleep
(
1
)
self
.
print_group_view
()
# self.basic_lcr()
# self.printwt(f'The current leader IP is: {self.leader_uuid}')
...
...
@@ -109,7 +108,7 @@ class Server():
# if my IP is not in the server list add it
if
MY_IP
not
in
self
.
serverList
:
self
.
serverList
.
append
(
MY_IP
)
self
.
basic_lcr
()
# listen for IPs from existing servers
maxLoop
=
5
...
...
@@ -136,7 +135,6 @@ class Server():
# if reply address is not in the server list, add it
if
reply_address
not
in
self
.
serverList
:
self
.
serverList
.
append
(
reply_address
)
self
.
basic_lcr
()
# Erhöhe die Anzahl der eingehenden Antworten
num_responses
+=
1
...
...
@@ -208,65 +206,78 @@ class Server():
self
.
clients
.
remove
(
client
)
def
basic_lcr
(
self
):
time
.
sleep
(
3
)
# bind to ring socket
ring_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
ring_socket
.
bind
((
MY_IP
,
RING_PORT
))
print
(
'
++++++++++++++++++++++++++
'
)
print
(
'
New leader election starts
'
)
print
(
'
sending first leader election message to neighbour
'
)
print
(
'
Servers
'
)
print
(
self
.
serverList
)
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
first_message
=
{
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
False
}
self
.
participant
=
True
ring_socket
.
sendto
(
json
.
dumps
(
first_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
print
(
'
Neighbour
'
)
print
(
neighbour
)
print
(
'
\n
Waiting to receive election message...
\n
'
)
data
,
address
=
ring_socket
.
recvfrom
(
1024
)
election_message
=
json
.
loads
(
data
.
decode
())
print
(
'
There is a election message
'
)
print
(
election_message
)
if
election_message
.
get
(
'
isLeader
'
):
print
(
'
1: leader info weitergeben
'
)
self
.
leader_uuid
=
election_message
[
'
mid
'
]
# forward received election message to left neighbour
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
election_message
),
neighbour
)
if
election_message
.
get
(
'
mid
'
)
<
self
.
uuid
and
not
self
.
participant
:
print
(
'
2: mich vorschlagen
'
)
new_election_message
=
{
try
:
ring_socket
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
ring_socket
.
bind
((
MY_IP
,
RING_PORT
))
print
(
'
++++++++++++++++++++++++++
'
)
print
(
'
New leader election starts
'
)
print
(
'
sending first leader election message to neighbour
'
)
print
(
'
Servers
'
)
print
(
self
.
serverList
)
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
first_message
=
{
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
False
"
isLeader
"
:
False
}
self
.
participant
=
True
# send received election message to left neighbour
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
),
neighbour
)
elif
election_message
.
get
(
'
mid
'
)
>
self
.
uuid
:
# send received election message to left neighbour
print
(
'
3: Jemand anderes vorschlagen
'
)
self
.
participant
=
True
ring_socket
.
sendto
(
json
.
dumps
(
election_message
),
neighbour
)
elif
election_message
.
get
(
'
mid
'
)
==
self
.
uuid
:
print
(
'
4: Ich wurde als Leader definiert
'
)
self
.
leader_uid
=
self
.
uuid
self
.
isLeader
=
True
new_election_message
=
{
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
True
}
# send new election message to left neighbour
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
ring_socket
.
sendto
(
json
.
dumps
(
first_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
print
(
'
Neighbour
'
)
print
(
neighbour
)
while
True
:
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
print
(
'
\n
Waiting to receive election message...
\n
'
)
data
,
address
=
ring_socket
.
recvfrom
(
1024
)
election_message
=
json
.
loads
(
data
.
decode
())
print
(
'
There is a election message
'
)
print
(
election_message
)
if
election_message
.
get
(
'
isLeader
'
)
and
self
.
participant
:
print
(
'
1: leader info weitergeben
'
)
self
.
leader_uuid
=
election_message
[
'
mid
'
]
# forward received election message to left neighbour
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
elif
election_message
.
get
(
'
mid
'
)
<
self
.
uuid
and
not
self
.
participant
:
print
(
'
2: mich vorschlagen
'
)
new_election_message
=
{
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
False
}
self
.
participant
=
True
# send received election message to left neighbour
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
elif
election_message
.
get
(
'
mid
'
)
>
self
.
uuid
:
# send received election message to left neighbour
print
(
'
3: Jemand anderes vorschlagen
'
)
self
.
participant
=
True
ring_socket
.
sendto
(
json
.
dumps
(
election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
elif
election_message
.
get
(
'
mid
'
)
==
self
.
uuid
and
self
.
participant
:
print
(
'
4: Ich wurde als Leader definiert
'
)
self
.
leader_uid
=
self
.
uuid
self
.
isLeader
=
True
new_election_message
=
{
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
True
}
# send new election message to left neighbour
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
elif
election_message
.
get
(
'
isLeader
'
)
and
not
self
.
participant
:
print
(
'
5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden
'
)
except
Exception
as
e
:
print
(
f
"
An error occurred:
{
e
}
"
)
finally
:
ring_socket
.
close
()
# starting all simultaneously working procedures
...
...
@@ -282,8 +293,8 @@ if __name__== '__main__':
thread1
=
threading
.
Thread
(
target
=
server
.
ListenForClientAndReply
)
thread1
.
start
()
#
thread_election = threading.Thread(target = server.basic_lcr)
#
thread_election.start()
thread_election
=
threading
.
Thread
(
target
=
server
.
basic_lcr
)
thread_election
.
start
()
# server.basic_lcr()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment