Skip to content

Commit 98f6f7d

Browse files
emaicusbmcutler
authored andcommitted
[Refactor:Autograding] Network Router Simplification (#23)
* Simplify Custom Router * fix * wip * Fixed indent
1 parent 79a7885 commit 98f6f7d

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

examples/16_docker_network_python/config/test_input/custom_router.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,66 @@ def __init__(self, seed=None, log_file='router_log.txt'):
1919
super().__init__(seed=seed, log_file=log_file)
2020

2121
'''
22-
Override this function to manipuate student messages.
23-
'''
24-
def manipulate_recieved_message(self, sender, recipient, port, message, message_number):
25-
now = datetime.datetime.now()
26-
# The total time the program has been running as of right now.
27-
elapsed_time = now - self.execution_start_time
28-
# Use the time at which this message will be processed. Set to now initially.
29-
process_time = now
30-
# Leave blank to avoid outputting a message to the student on their sequence diagram
31-
message_to_student = None
32-
#If true, this message is entirely discarded.
33-
drop_me = False
22+
Override this function to manipulate student messages.
3423
24+
data has the following keys:
25+
1. sender: The node that sent the message.
26+
2. recipient: The node to which the message is bound
27+
3. port: The port on which the message was received and will be sent.
28+
4. message: The message that was passed.
29+
5. message_number: The sequence number of the message as it was received by the router.
30+
6. receipt_time: The time at which the message was received by the router (sent by the sender)
31+
7. forward_time: The time at which the message is scheduled to be forwarded by the router to the recipient.
32+
Defaults to be equivalent to receipt_time.
33+
8. time_since_test_start: how long the test has currently been running as a timedelta object.
34+
9. drop_message: A boolean, which, if true, states that the message will be dropped.
35+
Defaults to false.
36+
10. diagram_label: A label for this message on the sequence diagram generated by this testcase
37+
Defaults to None
38+
'''
39+
def manipulate_received_message(self, data):
3540
#EXAMPLE: modify one bit of the student's message in 1/10 messages before 2 seconds
36-
if random.randint(1,10) == 10 and elapsed_time.total_seconds() <= 2:
41+
if random.randint(1,10) == 10 and data['time_since_test_start'].total_seconds() <= 2:
3742
# Convert the message int an array of bytes
38-
m_array = bytearray(message)
43+
m_array = bytearray(data['message'])
44+
3945
# Determine which bit will be flipped
4046
flipbit = random.randint(0,len(m_array)-1 )
47+
4148
# Flip the bit
4249
m_array[flipbit] = m_array[flipbit] + 1
50+
4351
# Update the message
44-
message = bytes(m_array)
52+
data['message'] = bytes(m_array)
53+
4554
# OPTIONAL: Tell the student that their message was corrupted via a message in their sequence diagram.
46-
message_to_student = "Corrupted"
55+
data['diagram_label'] = "Corrupted"
56+
4757
#EXAMPLE: delay the student message by up to 1 second in 1/10 messages
4858
elif random.randint(1,10) == 10:
4959
# Choose an amount of delay (in ms) between .1 and 1 second.
5060
milliseconds_dela = random.randint(100,1000)
61+
5162
# Extend the processing time that far into the future
52-
process_time = process_time + timedelta(milliseconds=milliseconds_dela)
63+
data['forward_time'] = data['forward_time'] + timedelta(milliseconds=milliseconds_dela)
64+
5365
# OPTIONAL: add a message to the student's sequence diagram that lets them know about the delay
54-
message_to_student = "Delayed {0} ms".format(milliseconds_dela)
66+
data['diagram_label'] = "Delayed {0} ms".format(milliseconds_dela)
67+
5568
#We drop 1/10th of packets
5669
elif random.randint(1,10) == 10:
57-
drop_me = True
70+
data['drop_message'] = True
5871

59-
data = {
60-
'sender' : sender,
61-
'recipient' : recipient,
62-
'port' : port,
63-
'message' : message,
64-
'message_to_student' : message_to_student,
65-
'drop_message' : drop_me
66-
}
67-
return (process_time, data)
72+
return data
6873

6974

7075
#You must provide a main which is capable of running your router.
7176
if __name__ == '__main__':
77+
#Construct an instance of our router
7278
router = custom_router()
79+
7380
router.log("This is the custom router!")
81+
7482
# submitty_router provides init and run functions for you to call.
7583
router.init()
7684
router.run()

0 commit comments

Comments
 (0)