Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece428mp1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
azukor2
ece428mp1
Commits
3d4feade
Commit
3d4feade
authored
3 years ago
by
Jeffrey Zhang
Browse files
Options
Downloads
Patches
Plain Diff
Correctly wait for clients to be set up
parent
3ebf343d
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
SelectServer.py
+11
-7
11 additions, 7 deletions
SelectServer.py
TransactionDeliverer.py
+0
-2
0 additions, 2 deletions
TransactionDeliverer.py
main.py
+0
-3
0 additions, 3 deletions
main.py
with
11 additions
and
12 deletions
SelectServer.py
+
11
−
7
View file @
3d4feade
...
...
@@ -2,6 +2,7 @@ import socket
import
pickle
import
select
import
threading
import
time
from
dataclasses
import
replace
from
Messages
import
Message
from
TransactionDeliverer
import
TransactionDeliverer
...
...
@@ -18,14 +19,14 @@ class Client:
message_len
=
self
.
socket
.
recv
(
2
)
if
len
(
message_len
)
<
2
:
print
(
f
"
Less than two bytes -
{
self
.
client_name
}
-
{
len
(
message_len
)
}
"
)
#
print(f"Less than two bytes - {self.client_name} - {len(message_len)}")
return
False
message_len
=
int
.
from_bytes
(
message_len
,
'
big
'
)
data
=
self
.
socket
.
recv
(
message_len
)
if
len
(
data
)
<
message_len
:
print
(
"
Less than msg len
"
)
#
print("Less than msg len")
return
False
obj
:
Message
=
pickle
.
loads
(
data
)
...
...
@@ -37,6 +38,10 @@ class Client:
if
not
self
.
client_name
:
self
.
client_name
=
obj
.
sender
# Client sockets may not be set up yet, wait until they are
while
not
self
.
client_name
in
self
.
td
.
client_sockets
:
time
.
sleep
(
1
)
rebroadcast
=
obj
.
receive_action
(
self
.
td
)
# Rebroadcast for reliable multicast
...
...
@@ -69,7 +74,7 @@ class Server(threading.Thread):
self
.
waiting_sockets
=
set
([
self
.
server_socket
])
def
disconnect_client
(
self
,
client
:
Client
,
sock
:
socket
.
socket
):
print
(
f
"
Disconnected client
{
client
.
client_name
}
"
)
#
print(f"Disconnected client {client.client_name}")
self
.
waiting_sockets
.
remove
(
sock
)
del
self
.
clients
[
sock
]
sock
.
shutdown
(
socket
.
SHUT_RDWR
)
...
...
@@ -90,18 +95,18 @@ class Server(threading.Thread):
client
=
Client
(
self
.
td
,
client_socket
,
ip
,
port
)
self
.
clients
[
client_socket
]
=
client
self
.
waiting_sockets
.
add
(
client_socket
)
print
(
f
"
Accepted connection from ip:
{
ip
}
, port:
{
port
}
"
)
#
print(f"Accepted connection from ip: {ip}, port: {port}")
else
:
# Read from node client
client
=
self
.
clients
[
sock
]
if
not
client
.
handle
():
# No data, disconnect
print
(
f
"
No data -
{
client
.
client_name
}
- disconnect
"
)
#
print(f"No data - {client.client_name} - disconnect")
self
.
disconnect_client
(
client
,
sock
)
def
handle_err_socket
(
self
,
sock
:
socket
.
socket
):
client
=
self
.
clients
[
sock
]
print
(
f
"
Client error -
{
client
.
client_name
}
"
)
#
print(f"Client error - {client.client_name}")
self
.
disconnect_client
(
client
,
sock
)
# Called by thread start
...
...
@@ -126,7 +131,6 @@ class Server(threading.Thread):
self
.
shutdown
()
def
shutdown
(
self
):
print
(
"
Server shutdown
"
)
for
s
in
self
.
waiting_sockets
:
s
.
shutdown
(
socket
.
SHUT_RDWR
)
s
.
close
()
...
...
This diff is collapsed.
Click to expand it.
TransactionDeliverer.py
+
0
−
2
View file @
3d4feade
...
...
@@ -52,7 +52,6 @@ class TransactionDeliverer:
def
new_transaction_message
(
self
,
transaction
:
Transaction
):
# Receives a transaction and builds the message to be sending out to all other nodes
#print("Sending message")
transaction_message
=
TransactionMessage
(
str
(
uuid4
()),
self
.
node_name
,
self
.
node_name
,
transaction
,
str
(
uuid4
()),
False
)
# Multicasting race condition prevention
self
.
received_messages
.
add
(
transaction_message
.
message_id
)
...
...
@@ -172,7 +171,6 @@ class TransactionDeliverer:
sock
.
close
()
def
shutdown
(
self
):
print
(
"
TD shutdown
"
)
for
sock
in
self
.
client_sockets
.
values
():
sock
.
shutdown
(
socket
.
SHUT_RDWR
)
sock
.
close
()
...
...
This diff is collapsed.
Click to expand it.
main.py
+
0
−
3
View file @
3d4feade
...
...
@@ -3,12 +3,10 @@
import
sys
import
threading
from
TransactionDeliverer
import
TransactionDeliverer
#from MessageServer import start_server
from
SelectServer
import
Server
from
Messages
import
TransactionMessage
from
Transaction
import
Transaction
,
TransactionType
from
signal
import
SIGINT
,
signal
import
time
is_running
=
True
...
...
@@ -67,7 +65,6 @@ if __name__ == "__main__":
server_thread
=
Server
(
td
,
addresses
[
0
],
ports
[
0
])
server_thread
.
start
()
td
.
bring_up_clients
()
time
.
sleep
(
2
)
process_inputs
(
td
)
server_thread
.
end
.
set
()
server_thread
.
join
()
\ No newline at end of file
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