Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Distributed-Systems
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
Katharina Willig
Distributed-Systems
Commits
11cc00f6
Commit
11cc00f6
authored
7 months ago
by
Katharina
Browse files
Options
Downloads
Patches
Plain Diff
thread event
parent
dedabc49
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
kclient.py
+1
-0
1 addition, 0 deletions
kclient.py
server.py
+18
-7
18 additions, 7 deletions
server.py
with
19 additions
and
7 deletions
kclient.py
+
1
−
0
View file @
11cc00f6
...
...
@@ -56,6 +56,7 @@ def sender():
#print("send message to", broadcast_port)
#checks for whitemarks
if
message
.
strip
():
full_message
=
f
"
{
message_id
}
:
{
nickname
}
:
{
message
}
"
.
encode
()
client_socket
.
sendto
(
full_message
,
(
broadcast_ip
,
broadcast_port
))
#print("message sended to", broadcast_port)
...
...
This diff is collapsed.
Click to expand it.
server.py
+
18
−
7
View file @
11cc00f6
##########unsicher, ob heartbeat funktioniert################
###########deutsch englisch korrigieren###################
################liste mit message ids auf 3-5 setzen########
import
socket
import
multiprocessing
import
uuid
...
...
@@ -82,7 +86,7 @@ def listen_client(shared_data):
#global received_messages
received_messages
=
set
()
#save_last_messages = manager.dict()
#message_queue = multiprocessing.Queue(maxsize=3)
...
...
@@ -196,7 +200,9 @@ def start_election(queue, shared_data):
elif
"
LEADER
"
in
message
:
leader_uuid
=
message
.
split
(
"
:
"
)[
1
]
shared_data
[
'
current_leader
'
]
=
leader_uuid
#time.sleep(1) # Delay to ensure all processes are aware of the leader
print
(
f
"
(leader) Leader elected:
{
shared_data
[
'
current_leader
'
]
}
"
)
leader_election_done
.
set
()
# Signal, dass die Leaderwahl abgeschlossen ist
return
except
multiprocessing
.
queues
.
Empty
:
...
...
@@ -230,12 +236,17 @@ def start_election(queue, shared_data):
# Heartbeat function
def
send_heartbeat
(
shared_data
):
"""
Sends heartbeat messages to keep the server active.
"""
leader_election_done
.
wait
()
# Blockiert, bis die Leaderwahl abgeschlossen ist
print
(
f
"
Heartbeat function started. Leader:
{
shared_data
[
'
current_leader
'
]
}
, Server ID:
{
shared_data
[
'
server_id
'
]
}
"
)
#only if you are the current leader
if
shared_data
[
'
current_leader
'
]
==
shared_data
[
'
server_id
'
]:
time
.
sleep
(
3
0
)
#waiting for leader election #set to 20s
#
time.sleep(
1
0) #waiting for leader election #set to 20s
while
True
:
broadcast_election
(
f
"
HEARTBEAT:
{
shared_data
[
'
server_id
'
]
}
"
)
#please change that to right neighbour after correct leader election
time
.
sleep
(
5
)
try
:
broadcast_election
(
f
"
HEARTBEAT:
{
shared_data
[
'
server_id
'
]
}
"
)
#please change that to right neighbour after correct leader election
time
.
sleep
(
5
)
except
socket
.
error
as
e
:
print
(
f
"
Failed to send heartbeat:
{
e
}
"
)
# Monitor heartbeats
def
monitor_heartbeats
():
...
...
@@ -255,6 +266,7 @@ if __name__ == "__main__":
multiprocessing
.
freeze_support
()
print
(
f
"
Script started with PID:
{
os
.
getpid
()
}
"
)
leader_election_done
=
threading
.
Event
()
# Manager for shared data
manager
=
Manager
()
...
...
@@ -274,14 +286,13 @@ if __name__ == "__main__":
#listener_election_process = multiprocessing.Process(target=listen_broadcast_election, args=(message_queue, shared_data))
listener_client_process
=
multiprocessing
.
Process
(
target
=
listen_client
,
args
=
(
shared_data
,))
election_process
=
multiprocessing
.
Process
(
target
=
start_election
,
args
=
(
message_queue
,
shared_data
))
# Start the listener for clients in a thread
listener_election_thread
=
threading
.
Thread
(
target
=
listen_broadcast_election
,
args
=
(
message_queue
,
shared_data
),
daemon
=
True
)
# Heartbeat threads
heartbeat_thread
=
threading
.
Thread
(
target
=
send_heartbeat
,
args
=
(
shared_data
,),
daemon
=
True
)
heartbeat_monitor_thread
=
threading
.
Thread
(
target
=
monitor_heartbeats
,
daemon
=
True
)
# Start the listener for clients in a thread
listener_election_thread
=
threading
.
Thread
(
target
=
listen_broadcast_election
,
args
=
(
message_queue
,
shared_data
),
daemon
=
True
)
# Start processes and threads
#listener_election_process.start()
listener_client_process
.
start
()
...
...
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