1+ import argparse
12import asyncio
23import sys
3- import argparse
4- import ipaddress
4+
55sys .path .append (sys .path [0 ] + "/../.." )
66import pytaps as taps # noqa: E402
77
8- color = " yellow"
8+ logger = taps . setup_logger ( "Framer Client" , " yellow")
99
1010
11- class testFramer (taps .Framer ):
11+ class TestFramer (taps .Framer ):
1212 async def start (self , connection ):
13- taps . print_time ("Framer got new connection" , color )
13+ logger . info ("Framer got new connection" )
1414 return
1515
1616 async def new_sent_message (self , data , context , eom ):
17- taps . print_time ("Framing new message " + str (data ), color )
17+ logger . info ("Framing new message " + str (data ))
1818 tlv = (data [0 ] + "/" + str (len (str (data [1 ]))) + "/" +
1919 str (data [1 ]))
2020 return tlv .encode ()
2121
2222 async def handle_received_data (self , connection ):
2323 byte_stream , context , eom = connection .parse ()
2424 byte_stream = byte_stream .decode ()
25- taps . print_time ("Deframing " + byte_stream , color )
25+ logger . info ("Deframing " + byte_stream )
2626 try :
2727 tlv = byte_stream .split ("/" )
2828 except Exception :
29- taps . print_time ("Error splitting" , color )
29+ logger . warn ("Error splitting" )
3030 raise taps .DeframingFailed
31- return
3231
3332 if len (tlv ) < 3 :
34- taps . print_time ("Deframing error: missing length," +
35- " value or type parameter." , color )
33+ logger . warning ("Deframing error: missing length," +
34+ " value or type parameter." )
3635 raise taps .DeframingFailed
37- return
3836
39- if ( len (tlv [2 ]) < int (tlv [1 ]) ):
40- taps . print_time ("Deframing error: actual length of message" +
41- " shorter than indicated" , color )
37+ if len (tlv [2 ]) < int (tlv [1 ]):
38+ logger . warning ("Deframing error: actual length of message" +
39+ " shorter than indicated" )
4240 raise taps .DeframingFailed
43- return
41+
4442 len_message = len (tlv [0 ]) + len (tlv [1 ]) + int (tlv [1 ]) + 2
4543 message = (str (tlv [0 ]), str (tlv [2 ][0 :int (tlv [1 ])]))
46- return (context , message , len_message , eom )
47- """
44+ return context , message , len_message , eom
45+
46+
47+ """
4848 self.advance_receive_cursor(connection, len_message)
4949 self.deliver(connection, context, message, eom)
5050 """
5151
5252
53- class TestClient () :
53+ class TestClient :
5454 def __init__ (self ):
5555 self .connection = None
5656 self .preconnection = None
5757 self .loop = asyncio .get_event_loop ()
5858
5959 async def handle_received_partial (self , data , context , end_of_message ,
6060 connection ):
61- taps . print_time ("Received partial message " + str (data ) + "." , color )
61+ logger . info ("Received partial message " + str (data ) + "." )
6262 # self.loop.stop()
6363
6464 async def handle_received (self , data , context , connection ):
65- taps . print_time ("Received message " + str (data ) + "." , color )
65+ logger . info ("Received message " + str (data ) + "." )
6666 # self.loop.stop()
6767
6868 async def handle_sent (self , message_ref , connection ):
69- taps . print_time ("Sent cb received, message " + str (message_ref ) +
70- " has been sent." , color )
69+ logger . info ("Sent cb received, message " + str (message_ref ) +
70+ " has been sent." )
7171 await self .connection .receive (min_incomplete_length = 1 )
7272
7373 async def handle_send_error (self , msg , connection ):
74- taps . print_time ("SendError cb received." , color )
74+ logger . info ("SendError cb received." )
7575 print ("Error sending message" )
7676
7777 async def handle_initiate_error (self , connection ):
78- taps . print_time ("InitiateError cb received." , color )
78+ logger . info ("InitiateError cb received." )
7979 print ("Error init" )
8080 self .loop .stop ()
8181
8282 async def handle_closed (self , connection ):
83- taps . print_time ("Connection closed, stopping event loop." , color )
83+ logger . info ("Connection closed, stopping event loop." )
8484 # self.loop.stop()
8585
8686 async def handle_ready (self , connection ):
87- taps . print_time ("Ready cb received from connection to " +
88- connection .remote_endpoint .address + ":" +
89- str (connection .remote_endpoint .port ) +
90- " (hostname: " +
91- str (connection .remote_endpoint .host_name ) +
92- ")" , color )
87+ logger . info ("Ready cb received from connection to " +
88+ connection .remote_endpoint .address + ":" +
89+ str (connection .remote_endpoint .port ) +
90+ " (hostname: " +
91+ str (connection .remote_endpoint .host_name ) +
92+ ")" )
9393
9494 # Set connection callbacks
9595 self .connection .on_sent (self .handle_sent )
9696 self .connection .on_send_error (self .handle_send_error )
9797 self .connection .on_closed (self .handle_closed )
9898 self .connection .on_received_partial (self .handle_received_partial )
9999 self .connection .on_received (self .handle_received )
100- taps . print_time ("Connection cbs set." , color )
100+ logger . info ("Connection cbs set." )
101101
102102 msgref = await self .connection .send_message (("STR" , "Hello there" ))
103103 msgref = await self .connection .send_message (("STR" , "This is a test" ))
@@ -109,7 +109,7 @@ async def handle_ready(self, connection):
109109 msgref = await self.connection.send_message("Is")
110110 msgref = await self.connection.send_message("a")
111111 msgref = await self.connection.send_message("Test")"""
112- taps . print_time ("send_message called." , color )
112+ logger . info ("send_message called." )
113113
114114 async def main (self , args ):
115115
@@ -132,7 +132,7 @@ async def main(self, args):
132132 if args .local_port :
133133 lp .with_port (args .local_port )
134134
135- taps . print_time ("Created endpoint objects." , color )
135+ logger . info ("Created endpoint objects." )
136136
137137 if args .secure or args .trust_ca or args .local_identity :
138138 # Use TLS
@@ -141,7 +141,7 @@ async def main(self, args):
141141 sp .add_trust_ca (args .trust_ca )
142142 if args .local_identity :
143143 sp .add_identity (args .local_identity )
144- taps . print_time ("Created SecurityParameters." , color )
144+ logger . info ("Created SecurityParameters." )
145145
146146 # Create transportProperties Object and set properties
147147 # Does nothing yet
@@ -160,13 +160,13 @@ async def main(self, args):
160160 self .preconnection .on_initiate_error (self .handle_initiate_error )
161161 self .preconnection .on_ready (self .handle_ready )
162162 # Set the framer
163- framer = testFramer ()
163+ framer = TestFramer ()
164164 self .preconnection .add_framer (framer )
165- taps . print_time ("Created preconnection object and set cbs." , color )
165+ logger . info ("Created preconnection object and set cbs." )
166166 # Initiate the connection
167167 self .connection = await self .preconnection .initiate ()
168168 # msgref = await self.connection.send_message("Hello\n")
169- taps . print_time ("Called initiate, connection object created." , color )
169+ logger . info ("Called initiate, connection object created." )
170170
171171
172172if __name__ == "__main__" :
0 commit comments