Skip to content

Commit 169d07a

Browse files
committed
Add various comments into the code
1 parent d1395c2 commit 169d07a

8 files changed

Lines changed: 68 additions & 4 deletions

File tree

RpiCluster/RpiBasicSecondaryThread.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010

1111
class RpiBasicSecondaryThread(threading.Thread):
12+
"""Relatively basic class to represent a "simple" secondary node that connects to the primary and performs no actions.
13+
14+
Creating children from this base will allow more complex systems to be created. This inherits from thread so
15+
you are able to start this running and perform other processing at the same time.
16+
17+
Attributes:
18+
uuid: A UUID to represent the secondary thread. This is assigned by the primary
19+
server_address: A tuple representing the IP address and port to use for the primary
20+
connection_handler: A connection handler object which will be used for sending/recieving messages.
21+
"""
1222

1323
def __init__(self, primary_ip, socket_port):
1424
threading.Thread.__init__(self)
@@ -17,6 +27,7 @@ def __init__(self, primary_ip, socket_port):
1727
self.connection_handler = None
1828

1929
def run(self):
30+
"""Base method to begin running the thread, this will connect to the primary and then repeatedly call self.perform_action"""
2031
logger.info("Starting script...")
2132

2233
while True:
@@ -47,6 +58,7 @@ def run(self):
4758
message = self.connection_handler.get_message()
4859
logger.info("We have information about the primary " + json.dumps(message['payload']))
4960

61+
# TODO: possibly add some way to signal to the thread that we want to stop
5062
while True:
5163
self.perform_action()
5264

@@ -55,6 +67,7 @@ def run(self):
5567
logger.info("Secondary will try and reconnect once primary is back online")
5668

5769
def perform_action(self):
70+
"""This method is going to be the primary one you will override to make the node to custom exciting things"""
5871
logger.info("Now sending a keepalive to the primary")
5972
self.connection_handler.send_message("I am still alive, client: {num}".format(num=self.uuid))
6073
time.sleep(5)

RpiCluster/RpiClusterClient.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88

99

1010
class RpiClusterClient(threading.Thread):
11+
"""This class is used to handle each secondary node that connects to the primary.
12+
13+
When running this will continually get messages from the secondary node and return a response
14+
in some form.
15+
16+
Attributes:
17+
uuid: A random UUID created for the node to give everyone a random ID
18+
primary: a reference to the primary to get information from it
19+
connection_handler: A handler that manages recieving and sending messages in the payload format
20+
address: Address of the client
21+
node_specifications: Details of the node if it provides it
22+
23+
"""
1124

1225
def __init__(self, primary, clientsocket, address):
1326
threading.Thread.__init__(self)
@@ -18,6 +31,7 @@ def __init__(self, primary, clientsocket, address):
1831
self.node_specifications = None
1932

2033
def run(self):
34+
"""Method that runs handling the secondary and serving all of its messages"""
2135
try:
2236
message = True
2337
while message:

RpiCluster/RpiPrimary.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44

55

66
class RpiPrimary:
7+
"""Class to create a primary node which will handle connections coming in from a secondary
8+
9+
This will form the basis of a general primary node which will be in charge of listening for connections
10+
and handling each one.
11+
12+
Attributes:
13+
socket_bind_ip: IP address to bind the listening socket server to
14+
socket_port: Port number to bind the listening socket server to
15+
connected_clients: Dict of connected clients
16+
"""
717

818
def __init__(self, socket_bind_ip, socket_port):
919
self.socket_bind_ip = socket_bind_ip
1020
self.socket_port = socket_port
1121
self.connected_clients = {}
1222

1323
def start(self):
24+
"""Start the handling of secondary nodes"""
1425
logger.info("Starting script...")
1526

1627
listening_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -26,15 +37,16 @@ def start(self):
2637
rpi_client.start()
2738

2839
def remove_client(self, rpi_client):
40+
"""Removes a given client from the list of connected clients, typically called after disconnection"""
2941
del self.connected_clients[rpi_client.uuid]
3042

3143
def get_secondary_details(self):
44+
"""Allows retrieving some basic information of every secondary connected to the primary"""
3245
secondary_details = {}
3346
for uuid in self.connected_clients:
3447
secondary_details[uuid] = {
3548
"uuid": uuid,
3649
"address": str(self.connected_clients[uuid].address[0]) + ":" + str(self.connected_clients[uuid].address[1]),
37-
3850
}
3951

4052
return secondary_details

RpiCluster/RpiWebserverThread.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66

77
class RpiWebserverSecondaryThread(RpiBasicSecondaryThread):
8+
"""Subclass of the main RpiBasicSecondaryThread which handles connecting as a webserver
9+
10+
This is used to continually request data from the primary with the details of the cluster.
11+
Static variables in this class are used to keep track of this data and make it easily accessible
12+
by the webserver.
13+
"""
814

915
current_primary_details = None
1016
current_secondary_details = None

RpiCluster/Tasks/MachineInfo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66

77
def get_base_machine_info():
8+
"""When called various interesting stastics about the machine are returned
9+
10+
Currently this includes: hostname, cpu percentage, total ram, CPU name, number of cores the CPU has
11+
"""
812
return {
13+
#TODO: Consider making this an object
914
'hostname': socket.gethostname(),
1015
'cpu_percent_used': psutil.cpu_percent(1),
1116
'ram': psutil.virtual_memory().total,

RpiCluster/Tasks/NodeTasks.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,49 @@
33

44

55
def reboot():
6+
"""When called the node will be rebooted"""
67
subprocess.call(["sudo", "reboot"])
78

89

910
def shutdown():
11+
"""When called the node will be shut down, make sure you can turn it back on if needed!"""
1012
subprocess.call(["sudo", "shutdown", "-h"])
1113

1214

1315
def update_node():
14-
node_update_log_1 = subprocess.check_output(["sudo", "apt-get", "update"])
15-
node_update_log_2 = subprocess.check_output(["sudo", "apt-get", "upgrade"])
16+
"""When called the node will run apt update followed by apt upgrade"""
17+
node_update_log_1 = subprocess.check_output(["sudo", "apt", "update"])
18+
node_update_log_2 = subprocess.check_output(["sudo", "apt", "upgrade", "-y"])
1619
return [node_update_log_1, node_update_log_2]
1720

1821

1922
def get_node_time():
23+
"""Returns the current node time which may be useful for times when the Pi clock might be out of sync"""
2024
return time.time()
2125

2226

2327
def get_node_codebase_revision():
28+
"""Gets the revision of the codebase currently on the filesystem
29+
30+
TODO: Improve this so we store the revision "on load" rather than hoping its the same
31+
"""
2432
# Note: This is not necessarily the revision of the code currently running
2533
current_rev = subprocess.check_output(["git", "rev-parse", "HEAD"])
2634
return current_rev.rstrip("\r\n")
2735

2836

2937
def get_node_codebase_revision_time():
38+
"""Gets the time of the revision of the codebase currently on the filesystem
39+
40+
TODO: Improve this so we store the revision "on load" rather than hoping its the same
41+
"""
3042
node_rev = get_node_codebase_revision()
3143
rev_time = subprocess.check_output(["git", "show", "-s", "--format=%ct", node_rev])
3244
return int(rev_time.rstrip("\r\n"))
3345

3446

3547
def update_node_codebase():
48+
"""Updates the codebase using git pull, note if any local changes are present this might fail horribly """
3649
update_log = subprocess.check_output(["git", "pull"])
3750
return update_log
3851

basic_primary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
add_file_logger("primary.log")
1515

16+
# The RpiPrimary class handles all of the interesting bits of work that the primary performs
1617
primary = RpiPrimary(socket_bind_ip, socket_port)
1718
primary.start()
1819

basic_secondary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
add_file_logger("secondary.log")
1515

16+
# This class creates and runs a basic secondary thread
1617
basic_secondary_thread = RpiBasicSecondaryThread(primary_ip, socket_port)
17-
1818
basic_secondary_thread.start()
1919

0 commit comments

Comments
 (0)