Skip to content

Commit 97b638e

Browse files
committed
Switched from print_time to proper logging
1 parent e701cde commit 97b638e

File tree

11 files changed

+203
-171
lines changed

11 files changed

+203
-171
lines changed

examples/echo_example/echoServer.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import asyncio
22
import sys
33
import argparse
4+
45
sys.path.append(sys.path[0] + "/../..")
56
import pytaps as taps # noqa: E402
67

7-
color = "blue"
8+
logger = taps.setup_logger("Echo Server", "yellow")
89

910

10-
class TestServer():
11+
class TestServer:
1112
"""A simple echo server. Listens using TCP by default,
1213
but can also use UDP.
1314
1415
"""
16+
1517
def __init__(self, reliable=True):
1618
self.preconnection = None
1719
self.loop = asyncio.get_event_loop()
1820
self.connection = None
1921
self.reliable = reliable
2022

2123
async def handle_connection_received(self, connection):
22-
taps.print_time("Received new Connection.", color)
24+
logger.info("Received new Connection.")
2325
self.connection = connection
2426
self.connection.on_received_partial(self.handle_received_partial)
2527
self.connection.on_received(self.handle_received)
@@ -30,26 +32,26 @@ async def handle_connection_received(self, connection):
3032

3133
async def handle_received_partial(self, data, context, end_of_message,
3234
connection):
33-
taps.print_time("Received partial message " + str(data) + ".", color)
35+
logger.info("Received partial message " + str(data) + ".")
3436
await self.connection.receive(min_incomplete_length=1, max_length=5)
3537
msgref = await self.connection.send_message(data)
3638

3739
async def handle_received(self, data, context, connection):
38-
taps.print_time("Received message " + str(data) + ".", color)
40+
logger.info("Received message " + str(data) + ".")
3941
await self.connection.receive(min_incomplete_length=1, max_length=5)
4042
await self.connection.send_message(data)
4143

4244
async def handle_listen_error(self):
43-
taps.print_time("Listen Error occured.", color)
45+
logger.warn("Listen Error occured.")
4446
self.loop.stop()
4547

4648
async def handle_sent(self, message_ref, connection):
47-
taps.print_time("Sent cb received, message " + str(message_ref) +
48-
" has been sent.", color)
49+
logger.info("Sent cb received, message " + str(message_ref) +
50+
" has been sent.")
4951
# self.connection.close()
5052

5153
async def handle_stopped(self):
52-
taps.print_time("Listener has been stopped")
54+
logger.info("Listener has been stopped")
5355

5456
async def main(self, args):
5557
# Create endpoint object
@@ -61,13 +63,13 @@ async def main(self, args):
6163
if args.local_host:
6264
lp.with_hostname(args.local_host)
6365
# If nothing to listen on has been specified, listen on localhost
64-
if not args.interface and not args.local_address\
65-
and not args.local_host:
66+
if not args.interface and not args.local_address \
67+
and not args.local_host:
6668
lp.with_hostname("localhost")
6769
if args.local_port:
6870
lp.with_port(args.local_port)
6971

70-
taps.print_time("Created endpoint objects.", color)
72+
logger.info("Created endpoint objects.")
7173

7274
sp = None
7375
if args.secure or args.trust_ca or args.local_identity:
@@ -77,7 +79,7 @@ async def main(self, args):
7779
sp.add_trust_ca(args.trust_ca)
7880
if args.local_identity:
7981
sp.add_identity(args.local_identity)
80-
taps.print_time("Created SecurityParameters.", color)
82+
logger.info("Created SecurityParameters.")
8183

8284
tp = taps.TransportProperties()
8385
tp.ignore("congestion-control")
@@ -91,7 +93,7 @@ async def main(self, args):
9193
transport_properties=tp,
9294
security_parameters=sp)
9395
self.preconnection.on_connection_received(
94-
self.handle_connection_received)
96+
self.handle_connection_received)
9597
self.preconnection.on_listen_error(self.handle_listen_error)
9698
self.preconnection.on_stopped(self.handle_stopped)
9799
# self.preconnection.frame_with(taps.TlvFramer())

examples/framer_example/framerClient.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,103 @@
1+
import argparse
12
import asyncio
23
import sys
3-
import argparse
4-
import ipaddress
4+
55
sys.path.append(sys.path[0] + "/../..")
66
import 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

172172
if __name__ == "__main__":

pytaps/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from .preconnection import Preconnection
77
from .securityParameters import SecurityParameters
88
from .transportProperties import TransportProperties, PreferenceLevel
9-
from .utility import print_time, ConnectionState
9+
from .utility import print_time, ConnectionState, setup_logger

0 commit comments

Comments
 (0)