async def handle_connection_received(self, connection): taps.print_time("Received new Connection.", color) self.connection = connection self.connection.on_received_partial(self.handle_received_partial) self.connection.on_received(self.handle_received) self.connection.on_sent(self.handle_sent) await self.connection.receive(min_incomplete_length=1, max_length=-1)
async def handle_connection_received(self, connection): self.connections.append(connection) taps.print_time("Received new Data Connection (%d total)." % len(self.connections), color) connection.on_received(self.handle_received) # jake 2019-11-16: should handle_closed have the connection object? connection.on_closed(self.handle_closed) await connection.receive()
async def handle_connection_received(self, connection): taps.print_time("Received new Connection.", color) self.connection = connection self.connection.on_received_partial(self.handle_received_partial) self.connection.on_received(self.handle_received) self.connection.on_sent(self.handle_sent) await self.connection.receive()
async def handle_closed(self, connection): taps.print_time("Connection closed.", color) for idx, conn in zip(range(len(self.connections)), self.connections): if conn is connection: break if idx < len(self.connections): del self.connections[idx]
def got_packet(self, data, source_port): # TBD: handle wrapping # TBD: graceful shutdown signal to pick a refresh_deadline refresh_deadline = 0 dig = self.hasher.digest(data, source_port) self.cur_hashes.append(dig) self.packet_seq += 1 if len(self.cur_hashes) < self.packet_limit: if not self.time_limit: return since = datetime.datetime.now() - self.last_wrote if since < self.time_limit: return manifest = struct.pack('IIIHH', self.manifest_id, self.manifest_seq, self.packet_seq - len(self.cur_hashes), refresh_deadline, len(self.cur_hashes)) manifest += b''.join(self.cur_hashes) if self.sender: taps.print_time("New manifest pushing (%d conns)" % len(self.sender.connections), color) self.loop.create_task(self.sender.push_new_manifest(manifest)) else: taps.print_time("New manifest without sender)", color) self.last_wrote = datetime.datetime.now() self.cur_hashes = [] self.manifest_seq += 1
async def main(self, fname): self.preconnection = taps.Preconnection().from_yangfile(fname) taps.print_time("Loaded YANG file: %s." % fname, color) self.preconnection.on_connection_received( self.handle_connection_received) self.preconnection.on_listen_error(self.handle_listen_error) self.preconnection.on_stopped(self.handle_stopped) await self.preconnection.listen()
async def main(self): self.preconnection = taps.Preconnection.from_yang(taps.yang_validate.YANG_FMT_JSON, json.dumps(self.yang_data, indent=4)); self.preconnection.on_listen_error(self.handle_listen_error) self.preconnection.on_connection_received(self.handle_connection_received) taps.print_time("Created preconnection object and set cbs.", color) # Initiate the connection self.listener = await self.preconnection.listen() taps.print_time("Called initiate, connection object created.", color)
async def handle_received_data(self, connection): byte_stream, context, eom = connection.parse() byte_stream = byte_stream.decode() taps.print_time("Deframing " + byte_stream, color) try: tlv = byte_stream.split("/") except Exception: taps.print_time("Error splitting", color) raise taps.DeframingFailed return if len(tlv) < 3: taps.print_time( "Deframing error: missing length," + " value or type parameter.", color) raise taps.DeframingFailed return if (len(tlv[2]) < int(tlv[1])): taps.print_time( "Deframing error: actual length of message" + " shorter than indicated", color) raise taps.DeframingFailed return len_message = len(tlv[0]) + len(tlv[1]) + int(tlv[1]) + 2 message = (str(tlv[0]), str(tlv[2][0:int(tlv[1])])) return (context, message, len_message, eom) """
async def main(self, args): # Create endpoint objects ep = taps.RemoteEndpoint() if args.remote_address: ep.with_address(args.remote_address) elif args.remote_host: ep.with_hostname(args.remote_host) if args.remote_port: ep.with_port(args.remote_port) lp = None sp = None if args.interface or args.local_address or args.local_port: lp = taps.LocalEndpoint() if args.interface: lp.with_interface(args.interface) if args.local_address: lp.with_port(args.local_address) if args.local_port: lp.with_port(args.local_port) taps.print_time("Created endpoint objects.", color) if args.secure or args.trust_ca or args.local_identity: # Use TLS sp = taps.SecurityParameters() if args.trust_ca: sp.addTrustCA(args.trust_ca) if args.local_identity: sp.addIdentity(args.local_identity) taps.print_time("Created SecurityParameters.", color) # Create transportProperties Object and set properties # Does nothing yet tp = taps.TransportProperties() # tp.prohibit("reliability") tp.ignore("congestion-control") tp.ignore("preserve-order") # tp.add("Reliable_Data_Transfer", taps.preferenceLevel.REQUIRE) # Create the preconnection object with the two prev created EPs self.preconnection = taps.Preconnection(remote_endpoint=ep, local_endpoint=lp, transport_properties=tp, security_parameters=sp) # Set callbacks self.preconnection.on_initiate_error(self.handle_initiate_error) self.preconnection.on_ready(self.handle_ready) # Set the framer framer = testFramer() self.preconnection.add_framer(framer) taps.print_time("Created preconnection object and set cbs.", color) # Initiate the connection self.connection = await self.preconnection.initiate() # msgref = await self.connection.send_message("Hello\n") taps.print_time("Called initiate, connection object created.", color)
async def main(self, args): # Create endpoint object lp = taps.LocalEndpoint() if args.interface: lp.with_interface(args.interface) if args.local_address: lp.with_address(args.local_address) if args.local_host: lp.with_hostname(args.local_host) # If nothing to listen on has been specified, listen on localhost if not args.interface and not args.local_address\ and not args.local_host: lp.with_hostname("localhost") if args.local_port: lp.with_port(args.local_port) taps.print_time("Created endpoint objects.", color) sp = None if args.secure or args.trust_ca or args.local_identity: # Use TLS sp = taps.SecurityParameters() if args.trust_ca: sp.addTrustCA(args.trust_ca) if args.local_identity: sp.addIdentity(args.local_identity) taps.print_time("Created SecurityParameters.", color) tp = taps.TransportProperties() tp.ignore("congestion-control") tp.ignore("preserve-order") if self.reliable == "False": tp.prohibit("reliability") if self.reliable == "Both": tp.ignore("reliability") self.preconnection = taps.Preconnection(local_endpoint=lp, transport_properties=tp, security_parameters=sp) self.preconnection.on_connection_received( self.handle_connection_received) self.preconnection.on_listen_error(self.handle_listen_error) self.preconnection.on_stopped(self.handle_stopped) # self.preconnection.frame_with(taps.TlvFramer()) await self.preconnection.listen()
async def handle_ready(self, connection): taps.print_time( "Ready cb received from connection to " + connection.remote_endpoint.address + ":" + str(connection.remote_endpoint.port) + " (hostname: " + str(connection.remote_endpoint.host_name) + ")", color) # Set connection callbacks self.connection.on_sent(self.handle_sent) self.connection.on_send_error(self.handle_send_error) self.connection.on_closed(self.handle_closed) self.connection.on_received_partial(self.handle_received_partial) self.connection.on_received(self.handle_received) taps.print_time("Connection cbs set.", color) msgref = await self.connection.send_message(("STR", "Hello there")) msgref = await self.connection.send_message(("STR", "This is a test")) msgref = await self.connection.send_message(("INT", 334353)) msgref = await self.connection.send_message(("STR", "Hope it worked")) # Send message """ msgref = await self.connection.send_message("This") msgref = await self.connection.send_message("Is") msgref = await self.connection.send_message("a") msgref = await self.connection.send_message("Test")""" taps.print_time("send_message called.", color)
async def handle_ready(self, connection): taps.print_time("Ready cb received from connection to " + connection.remote_endpoint.address + ":" + str(connection.remote_endpoint.port) + " (hostname: " + str(connection.remote_endpoint.host_name) + ")", color) # Set connection callbacks self.connection.on_sent(self.handle_sent) self.connection.on_send_error(self.handle_send_error) self.connection.on_closed(self.handle_closed) self.connection.on_received_partial(self.handle_received_partial) self.connection.on_received(self.handle_received) taps.print_time("Connection cbs set.", color) # Send messages msgref = await self.connection.send_message("Hello\n") msgref = await self.connection.send_message("There") msgref = await self.connection.send_message("Friend") msgref = await self.connection.send_message("How") msgref = await self.connection.send_message("Are") msgref = await self.connection.send_message("You\n") msgref = await self.connection.send_message("Today?\n") msgref = await self.connection.send_message("343536") taps.print_time("send_message called.", color)
async def main(self, args): fname = args.file[0] self.preconnection = taps.Preconnection().from_yangfile(fname) taps.print_time("Loaded YANG file: %s." % fname, color) self.preconnection.on_initiate_error(self.handle_initiate_error) self.preconnection.on_ready(self.handle_ready) taps.print_time("Created preconnection object and set cbs.", color) # Initiate the connection self.connection = await self.preconnection.initiate() taps.print_time("Called initiate, connection object created.", color)
async def handle_initiate_error(self, connection): taps.print_time("InitiateError cb received.", color) print("Error init") self.loop.stop()
async def handle_send_error(self, msg, connection): taps.print_time("SendError cb received.", color) print("Error sending message")
async def handle_sent(self, message_ref, connection): taps.print_time("Sent cb received, message " + str(message_ref) + " has been sent.", color) await self.connection.receive(min_incomplete_length=1)
async def handle_received(self, data, context, connection): taps.print_time("Received message " + str(data) + ".", color)
async def handle_received_partial(self, data, context, end_of_message, connection): taps.print_time("Received partial message " + str(data) + ".", color)
async def new_sent_message(self, data, context, eom): taps.print_time("Framing new message " + str(data), color) tlv = (data[0] + "/" + str(len(str(data[1]))) + "/" + str(data[1])) return tlv.encode()
async def handle_closed(self, conn): taps.print_time("Connection closed.", color)
async def handle_sent(self, message_ref, connection): taps.print_time( "Sent cb received, message " + str(message_ref) + " has been sent.", color)
async def handle_listen_error(self): taps.print_time("Listen Error occured.", color) self.loop.stop()
async def handle_received(self, data, context, connection): taps.print_time("Received message " + str(data) + ".", color) await self.connection.receive(min_incomplete_length=1, max_length=5) await self.connection.send_message(data)
async def handle_received_partial(self, data, context, end_of_message, connection): taps.print_time("Received partial message " + str(data) + ".", color) await self.connection.receive(min_incomplete_length=1, max_length=5) msgref = await self.connection.send_message(data)
async def handle_received(self, data, context, connection): taps.print_time("Received manifest " + str(data) + ".", color2) await self.connection.receive(min_incomplete_length=1)
async def handle_ready(self, connection): taps.print_time("TCP connection ready", color2) self.connection.on_received_partial(self.handle_received_partial) self.connection.on_received(self.handle_received) await self.connection.receive(min_incomplete_length=1)
async def handle_received(self, data, context, connection): taps.print_time("Received multicast message " + str(data) + ".", color) await self.connection.receive()
async def handle_closed(self, connection): taps.print_time("Connection closed, stopping event loop.", color)
async def handle_stopped(self): taps.print_time("Listener has been stopped")
async def handle_connection_received(self, connection): taps.print_time("Received new Connection.", color) self.connections.append(connection) connection.on_sent(self.handle_sent) connection.on_closed(self.handle_closed)