def handle(self):
     peer_ip = self.request.getpeername()[0]
     handler = comhandler.Connection(socket=self.request)
     print("Handler", handler.status())
     print("Init Client")
     message = handler.init_client()
     print("Handler", handler.status())
     print(message.__str__())
     if message.command != commands_pb2.Command.version:
         print("Version needed")
         self.request.close()
         return
     if message.string_value in version_allow:
         print("Protocol version matched: {}".format(message.string_value))
         handler.send_void(commands_pb2.Command.ok)
     else:
         print("Protocol version mismatch: {}, should be {}".format(
             message.string_value, version_allow))
         self.request.close()
         handler.send_void(commands_pb2.Command.notok)
         return
     print("Handler", handler.status())
     while True:
         try:
             # Failsafe
             if self.request == -1:
                 raise ValueError(
                     "Inbound: Closed socket from {}".format(peer_ip))
             message = handler.get_message()
             print("Got message", message.__str__())
             print("Handler", handler.status())
         except Exception as e:
             print("Error >{}< for {}.".format(e, peer_ip))
             return
Example #2
0
# Protobuf protocol version with abstraction layer

import time

# Our modules
import comhandler
import commands_pb2

version = "posnet0001"

HOST = "127.0.0.1"
# 6969 may be temporary
PORT = 6969

if __name__ == "__main__":
    handler = comhandler.Connection()
    print("Handler", handler.status())
    try:
        print("Connecting to", HOST, PORT)
        handler.connect(HOST, PORT)
        print("Handler", handler.status())
        # communication starter
        print("Sending version")
        handler.send_string(commands_pb2.Command.version, version)
        print("Handler", handler.status())
        message = handler.get_message()
        print("Got message", message.__str__())
        print("Handler", handler.status())
        if message.command == commands_pb2.Command.ok:
            print("Outbound: Node protocol version of {} matches our client".
                  format(HOST))
# PORT = 6568 #6568 for legacy protocol, 6569 is the server running the protobuff protocol

peer = {}
peer['127.0.0.1'] = 6569
print(peer)
for ip, port in peer.items():
    print(ip, port)

# This client can talk to both
# PROTOCOL_VERSION = comhandler.VER_LEGACY
PROTOCOL_VERSION = comhandler.VER_PROTO

if __name__ == "__main__":

    handler = comhandler.Connection (version=PROTOCOL_VERSION)
    print ("Handler", handler.status ())

    try:
        print ("Connecting to", ip, port)


        handler.connect (ip, port)
        print ("Handler", handler.status ())
        # communication starter

        handler.send_string (commands_pb2.Command.version, version)

        print ("Handler", handler.status ())

        message = handler.get_message ()