def fingerprint_callback(result): details = json.loads(result['result']['message']) host = details['ip_address'] port = details['port'] config.peer = host, port console.display('Connecting to %s %d' % (host, port)) config.node.send_message(host, port, {'type': 'connect',})
def send_message(raw): try: if config.peer is not None: host, port = config.peer config.node.send_message(host, port, {'type': 'message', 'message': raw}) else: console.display('PLEASE CONNECT TO A PEER: "co "') except: pass
def main(): config.update(vars(parse_arguments())) config.key = crypto.get_my_key() config.contacts = contacts.Contacts("contacts.json") result = post_user_info() if result.status_code >= 400: sys.exit("Couldn't POST to friend server") config.node = node = Node('foo', 'bar', config) config.peer = None reactor.listenTCP(config.port, MessageFactory(node)) reactor.callInThread(prompt) console.display('Fingerprint: %s' % config.key.fingerprint) reactor.run() # have fun!
def connect_to_peer(raw): args = raw.strip().split(' ') if len(args) == 2: def fingerprint_callback(result): details = json.loads(result['result']['message']) host = details['ip_address'] port = details['port'] config.peer = host, port console.display('Connecting to %s %d' % (host, port)) config.node.send_message(host, port, {'type': 'connect',}) peer_fingerprint = config.contacts.get_fingerprint(args[1]) d = get_user_info(peer_fingerprint) d.addCallback(fingerprint_callback) else: console.display('INCORRECT ARGS: co fingerprint')
def message_received(self, message, protocol): """ Handles incoming messages. """ peer = protocol.transport.getPeer() self.config.peer = peer_key = (peer.host, peer.port) if peer_key not in self._contacts: self._contacts[peer_key] = protocol if 'type' not in message: protocol.error('Must specify message type.') return message_type = message['type'] if message_type == 'connect': display('Connection from %s %d' % (peer.host, peer.port)) else: display('[THEM] ' + message['message'])
def dispatch(message): console.display('[YOU] ' + message) if message.startswith('co '): connect_to_peer(message) else: send_message(message)