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
9834141e
Commit
9834141e
authored
1 year ago
by
Robin Leber
Browse files
Options
Downloads
Patches
Plain Diff
some beautifying
parent
b3e0975e
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
+8
-12
8 additions, 12 deletions
server.py
with
8 additions
and
12 deletions
server.py
+
8
−
12
View file @
9834141e
...
@@ -86,7 +86,6 @@ class Server():
...
@@ -86,7 +86,6 @@ class Server():
time
.
sleep
(
1
)
time
.
sleep
(
1
)
self
.
print_group_view
()
self
.
print_group_view
()
# self.printwt(f'The current leader IP is: {self.leader_uuid}')
#this function enables the server to send a multicast to the server group and receive the answers of existing members
#this function enables the server to send a multicast to the server group and receive the answers of existing members
...
@@ -146,8 +145,7 @@ class Server():
...
@@ -146,8 +145,7 @@ class Server():
if
num_responses
==
1
:
if
num_responses
==
1
:
multicast_send_sock
.
close
()
multicast_send_sock
.
close
()
self
.
isLeader
=
True
self
.
isLeader
=
True
self
.
leader_uuid
=
self
.
uuid
# Hier wird die uuid-Adresse des Leaders zugewiesen
self
.
leader_uuid
=
self
.
uuid
# Hier wird die uuid-Adresse des Leaders zugewiesens
# self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_uuid}')
time
.
sleep
(
1
)
time
.
sleep
(
1
)
...
@@ -214,23 +212,22 @@ class Server():
...
@@ -214,23 +212,22 @@ class Server():
ring_socket
.
bind
((
MY_IP
,
RING_PORT
))
ring_socket
.
bind
((
MY_IP
,
RING_PORT
))
print
(
'
++++++++++++++++++++++++++
'
)
print
(
'
++++++++++++++++++++++++++
'
)
print
(
'
New leader election starts
'
)
print
(
'
New leader election starts
'
)
print
(
'
sending first leader election message to neighbour
'
)
print
(
'
Servers
'
)
print
(
'
Servers
'
)
print
(
self
.
serverList
)
print
(
self
.
serverList
)
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
print
(
'
Neighbour
'
)
print
(
neighbour
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
first_message
=
{
first_message
=
{
"
mid
"
:
self
.
uuid
,
"
mid
"
:
self
.
uuid
,
"
isLeader
"
:
False
"
isLeader
"
:
False
}
}
self
.
participant
=
True
self
.
participant
=
True
print
(
'
sending first leader election message to neighbour
'
)
ring_socket
.
sendto
(
json
.
dumps
(
first_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
ring_socket
.
sendto
(
json
.
dumps
(
first_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
print
(
'
Neighbour
'
)
print
(
neighbour
)
while
True
:
while
True
:
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour
=
get_neighbour
(
form_ring
(
self
.
serverList
),
MY_IP
,
'
left
'
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
neighbour_address
=
(
neighbour
,
RING_PORT
)
...
@@ -238,7 +235,7 @@ class Server():
...
@@ -238,7 +235,7 @@ class Server():
print
(
'
\n
Waiting to receive election message...
\n
'
)
print
(
'
\n
Waiting to receive election message...
\n
'
)
data
,
address
=
ring_socket
.
recvfrom
(
1024
)
data
,
address
=
ring_socket
.
recvfrom
(
1024
)
election_message
=
json
.
loads
(
data
.
decode
())
election_message
=
json
.
loads
(
data
.
decode
())
print
(
'
There is a election message
'
)
print
(
'
There is a election message
:
'
)
print
(
election_message
)
print
(
election_message
)
if
election_message
.
get
(
'
isLeader
'
)
and
self
.
participant
:
if
election_message
.
get
(
'
isLeader
'
)
and
self
.
participant
:
...
@@ -247,6 +244,7 @@ class Server():
...
@@ -247,6 +244,7 @@ class Server():
# forward received election message to left neighbour
# forward received election message to left neighbour
self
.
participant
=
False
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
ring_socket
.
sendto
(
json
.
dumps
(
election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
print
(
f
'
Leader is
{
self
.
leader_uuid
}
'
)
elif
election_message
.
get
(
'
mid
'
)
<
self
.
uuid
and
not
self
.
participant
:
elif
election_message
.
get
(
'
mid
'
)
<
self
.
uuid
and
not
self
.
participant
:
print
(
'
2: mich vorschlagen
'
)
print
(
'
2: mich vorschlagen
'
)
new_election_message
=
{
new_election_message
=
{
...
@@ -272,6 +270,7 @@ class Server():
...
@@ -272,6 +270,7 @@ class Server():
# send new election message to left neighbour
# send new election message to left neighbour
self
.
participant
=
False
self
.
participant
=
False
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
ring_socket
.
sendto
(
json
.
dumps
(
new_election_message
).
encode
(
'
utf-8
'
),
neighbour_address
)
print
(
f
'
I am Leader
{
self
.
leader_uuid
}
'
)
elif
election_message
.
get
(
'
isLeader
'
)
and
not
self
.
participant
:
elif
election_message
.
get
(
'
isLeader
'
)
and
not
self
.
participant
:
print
(
'
5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden
'
)
print
(
'
5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden
'
)
except
Exception
as
e
:
except
Exception
as
e
:
...
@@ -295,9 +294,6 @@ if __name__== '__main__':
...
@@ -295,9 +294,6 @@ if __name__== '__main__':
thread_election
=
threading
.
Thread
(
target
=
server
.
basic_lcr
)
thread_election
=
threading
.
Thread
(
target
=
server
.
basic_lcr
)
thread_election
.
start
()
thread_election
.
start
()
# server.basic_lcr()
# Socket erstellen und binden
# Socket erstellen und binden
server_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
server_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
...
...
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