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
b9915346
Commit
b9915346
authored
1 year ago
by
Robin Leber
Browse files
Options
Downloads
Patches
Plain Diff
election with thread
parent
07245428
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
+17
-7
17 additions, 7 deletions
server.py
with
17 additions
and
7 deletions
server.py
+
17
−
7
View file @
b9915346
...
...
@@ -23,10 +23,6 @@ RING_PORT = 10001
MY_HOST
=
socket
.
gethostname
()
MY_IP
=
socket
.
gethostbyname
(
MY_HOST
)
# bind to ring socket
ring_socket
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
ring_socket
.
bind
((
MY_IP
,
RING_PORT
))
class
Server
():
def
__init__
(
self
):
...
...
@@ -78,6 +74,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
)
...
...
@@ -89,7 +86,7 @@ class Server():
time
.
sleep
(
1
)
self
.
print_group_view
()
# self.basic_lcr()
self
.
printwt
(
f
'
The current leader IP is:
{
self
.
leader_uuid
}
'
)
#
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
...
...
@@ -149,7 +146,7 @@ class Server():
multicast_send_sock
.
close
()
self
.
isLeader
=
True
self
.
leader_uuid
=
self
.
uuid
# Hier wird die uuid-Adresse des Leaders zugewiesen
self
.
printwt
(
f
'
I am the only server in the system, so the leader IP is:
{
self
.
leader_uuid
}
'
)
#
self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_uuid}')
time
.
sleep
(
1
)
...
...
@@ -208,10 +205,16 @@ class Server():
self
.
clients
.
remove
(
client
)
def
basic_lcr
(
self
):
# 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
=
{
...
...
@@ -220,6 +223,9 @@ class Server():
}
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
)
...
...
@@ -273,7 +279,11 @@ if __name__== '__main__':
thread1
=
threading
.
Thread
(
target
=
server
.
ListenForClientAndReply
)
thread1
.
start
()
server
.
basic_lcr
()
thread_election
=
threading
.
Thread
(
target
=
server
.
basic_lcr
)
thread_election
.
start
()
# server.basic_lcr()
# Socket erstellen und binden
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