@@ -19,58 +19,66 @@ def __init__(self, seed=None, log_file='router_log.txt'):
19
19
super ().__init__ (seed = seed , log_file = log_file )
20
20
21
21
'''
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.
34
23
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 ):
35
40
#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 :
37
42
# Convert the message int an array of bytes
38
- m_array = bytearray (message )
43
+ m_array = bytearray (data ['message' ])
44
+
39
45
# Determine which bit will be flipped
40
46
flipbit = random .randint (0 ,len (m_array )- 1 )
47
+
41
48
# Flip the bit
42
49
m_array [flipbit ] = m_array [flipbit ] + 1
50
+
43
51
# Update the message
44
- message = bytes (m_array )
52
+ data ['message' ] = bytes (m_array )
53
+
45
54
# 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
+
47
57
#EXAMPLE: delay the student message by up to 1 second in 1/10 messages
48
58
elif random .randint (1 ,10 ) == 10 :
49
59
# Choose an amount of delay (in ms) between .1 and 1 second.
50
60
milliseconds_dela = random .randint (100 ,1000 )
61
+
51
62
# 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
+
53
65
# 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
+
55
68
#We drop 1/10th of packets
56
69
elif random .randint (1 ,10 ) == 10 :
57
- drop_me = True
70
+ data [ 'drop_message' ] = True
58
71
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
68
73
69
74
70
75
#You must provide a main which is capable of running your router.
71
76
if __name__ == '__main__' :
77
+ #Construct an instance of our router
72
78
router = custom_router ()
79
+
73
80
router .log ("This is the custom router!" )
81
+
74
82
# submitty_router provides init and run functions for you to call.
75
83
router .init ()
76
84
router .run ()
0 commit comments