Пример #1
0
    def _send_new_identity(self):
        """
        Send created identity to the identity server to register it.

        TODO: need to close transport and gateway after that
        """
        lg.out(4, 'id_registrator._send_new_identity ')
        from transport import gateway
        from transport import network_transport
        from transport.tcp import tcp_interface
        gateway.init()
        interface = tcp_interface.GateInterface()
        transport = network_transport.NetworkTransport('tcp', interface)
        transport.automat('init', gateway.listener())
        transport.automat('start')
        gateway.start()
        sendfilename = settings.LocalIdentityFilename() + '.new'
        dlist = []
        for idurl in self.new_identity.sources:
            self.free_idurls.remove(idurl)
            _, host, _, _ = nameurl.UrlParse(idurl)
            _, tcpport = known_servers.by_host().get(
                host, (settings.IdentityWebPort(), settings.IdentityServerPort()))
            srvhost = '%s:%d' % (host, tcpport)
            dlist.append(gateway.send_file_single(
                idurl, 'tcp', srvhost, sendfilename, 'Identity'))
        assert len(self.free_idurls) == 0
        return DeferredList(dlist)
Пример #2
0
 def _send_new_identity(self):
     """
     Send created identity to the identity server to register it.
     TODO: need to close transport and gateway after that
     """
     lg.out(4, 'id_registrator._send_new_identity ')
     from transport import gateway
     from transport import network_transport
     from transport.tcp import tcp_interface
     gateway.init()
     interface = tcp_interface.GateInterface()
     transport = network_transport.NetworkTransport('tcp', interface)
     transport.automat('init', gateway.listener())
     transport.automat('start')
     gateway.start()
     sendfilename = settings.LocalIdentityFilename() + '.new'
     dlist = []
     for idurl in self.new_identity.sources:
         self.free_idurls.remove(strng.to_bin(idurl))
         _, host, _, _ = nameurl.UrlParse(idurl)
         _, tcpport = known_servers.by_host().get(
             host,
             (settings.IdentityWebPort(), settings.IdentityServerPort()))
         srvhost = net_misc.pack_address((
             host,
             tcpport,
         ))
         dlist.append(
             gateway.send_file_single(idurl, 'tcp', srvhost, sendfilename,
                                      'Identity'))
     # assert len(self.free_idurls) == 0
     return DeferredList(dlist, fireOnOneCallback=True)
Пример #3
0
 def doStartNetworkTransports(self, *args, **kwargs):
     """
     Action method.
     """
     if not driver.is_on('service_gateway'):
         self.automat('gateway-is-not-started')
         return
     from transport import gateway
     restarted_transports = gateway.start()
     if len(restarted_transports) == 0:
         self.automat('all-network-transports-ready')
Пример #4
0
def main():
    lg.set_debug_level(18)
    lg.life_begins()
    from crypt import key
    key.InitMyKey()
    from contacts import identitycache
    identitycache.init()
    from system import tmpfile
    tmpfile.init()
    from services import driver

    required_services = [
        'service_http_connections',
        'service_http_transport',
        'service_gateway',
        'service_network',
    ]
    available_services_dir = os.path.join(bpio.getExecutableDir(), 'services')
    for filename in os.listdir(available_services_dir):
        if not filename.endswith('.py') and not filename.endswith(
                '.pyo') and not filename.endswith('.pyc'):
            continue
        if not filename.startswith('service_'):
            continue
        name = str(filename[:filename.rfind('.')])
        if name in required_services:
            continue
        driver.disabled_services().add(name)
    driver.init()
    driver.enabled_services().clear()
    driver.enabled_services().add('service_http_transport')
    driver.enabled_services().add('service_http_connections')
    driver.enabled_services().add('service_gateway')
    driver.enabled_services().add('service_network')
    driver.start()
    # options = { 'idurl': my_id.getIDURL(),}
    # options['host'] = nameurl.GetName(my_id.getIDURL())+'@'+'somehost.org'
    # options['dht_port'] = int(settings.getDHTPort())
    # options['udp_port'] = int(settings.getUDPPort())
    # udp.listen(int(settings.getUDPPort()))
    # dht_service.init(settings.getDHTPort())
    # dht_service.connect()
    # udp_node.A('go-online', options)
    reactor.addSystemEventTrigger('before', 'shutdown', gateway.shutdown)
    gateway.init()
    gateway.start()

    def _ok_to_send(transport, oldstate, newstate):
        if newstate != 'LISTENING':
            return
        # [filename] [peer idurl]
        if len(sys.argv) >= 3:
            pass
            # bpio.WriteFile(sys.argv[1]+'.signed', p.Serialize())


#             def _try_reconnect():
#                 sess = udp_session.get_by_peer_id(sys.argv[2])
#                 reconnect = False
#                 if not sess:
#                     reconnect = True
#                     print 'sessions', udp_session.sessions_by_peer_id().keys()
#                     print map(lambda s: s.peer_id, udp_session.sessions().values())
#                 else:
#                     if sess.state != 'CONNECTED':
#                         print 'state: ', sess.state
#                         reconnect = True
#                 if reconnect:
#                     print 'reconnect', sess
#                     udp_session.add_pending_outbox_file(
#                         sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False)
#                     udp_node.A('connect', sys.argv[2])
#                 reactor.callLater(0.5, _try_reconnect)
#
#             def _try_connect():
#                 if udp_node.A().state == 'LISTEN':
#                     print 'connect'
#                     gateway.stop_packets_timeout_loop()
#                     udp_session.add_pending_outbox_file(
#                         sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False)
#                     udp_node.A('connect', sys.argv[2])
#                     reactor.callLater(5, _try_reconnect)
#                 else:
#                     reactor.callLater(1, _try_connect)
#             # _try_connect()
#
#             def _send(c):
#                 from transport.udp import udp_stream
#                 for idurl in sys.argv[2:]:
#                     print '_send', udp_stream.streams().keys()
#                     p = signed.Packet(commands.Data(),
#                                       my_id.getIDURL(),
#                                       my_id.getIDURL(),
#                                       'packet%d' % c,
#                                       bpio.ReadBinaryFile(sys.argv[1]),
#                                       idurl)
#                     gateway.outbox(p)
#                 if c > 1:
#                     reactor.callLater(0.01, _send, c - 1)
#             reactor.callLater(0, _send, 15)

    gateway.add_transport_state_changed_callback(_ok_to_send)
    reactor.run()
Пример #5
0
def main():
    lg.set_debug_level(18)
    lg.life_begins()
    from crypt import key
    key.InitMyKey()
    from contacts import identitycache
    identitycache.init()
    from system import tmpfile
    tmpfile.init()
    from services import driver
    driver.disabled_services().add('service_tcp_connections')
    driver.disabled_services().add('service_p2p_hookups')
    driver.disabled_services().add('service_nodes_lookup')
    driver.disabled_services().add('service_identity_propagate')
    driver.disabled_services().add('service_ip_port_responder')
    driver.init()
    driver.enabled_services().clear()
    driver.enabled_services().add('service_udp_transport')
    driver.enabled_services().add('service_udp_datagrams')
    driver.enabled_services().add('service_my_ip_port')
    driver.enabled_services().add('service_gateway')
    driver.enabled_services().add('service_entangled_dht')
    driver.enabled_services().add('service_network')
    driver.start()
    # options = { 'idurl': my_id.getLocalID(),}
    # options['host'] = nameurl.GetName(my_id.getLocalID())+'@'+'somehost.org'
    # options['dht_port'] = int(settings.getDHTPort())
    # options['udp_port'] = int(settings.getUDPPort())
    # udp.listen(int(settings.getUDPPort()))
    # dht_service.init(settings.getDHTPort())
    # dht_service.connect()
    # udp_node.A('go-online', options)
    reactor.addSystemEventTrigger('before', 'shutdown', gateway.shutdown)
    gateway.init()
    gateway.start()

    def _ok_to_send(transport, oldstate, newstate):
        if newstate != 'LISTENING':
            return
        # [filename] [peer idurl]
        if len(sys.argv) >= 3:
            # bpio.WriteFile(sys.argv[1]+'.signed', p.Serialize())

            def _try_reconnect():
                sess = udp_session.get_by_peer_id(sys.argv[2])
                reconnect = False
                if not sess:
                    reconnect = True
                    print('sessions',
                          list(udp_session.sessions_by_peer_id().keys()))
                    print([
                        s.peer_id
                        for s in list(udp_session.sessions().values())
                    ])
                else:
                    if sess.state != 'CONNECTED':
                        print('state: ', sess.state)
                        reconnect = True
                if reconnect:
                    print('reconnect', sess)
                    udp_session.add_pending_outbox_file(
                        sys.argv[1] + '.signed', sys.argv[2], 'descr',
                        Deferred(), False)
                    udp_node.A('connect', sys.argv[2])
                reactor.callLater(0.5, _try_reconnect)

            def _try_connect():
                if udp_node.A().state == 'LISTEN':
                    print('connect')
                    gateway.stop_packets_timeout_loop()
                    udp_session.add_pending_outbox_file(
                        sys.argv[1] + '.signed', sys.argv[2], 'descr',
                        Deferred(), False)
                    udp_node.A('connect', sys.argv[2])
                    reactor.callLater(5, _try_reconnect)
                else:
                    reactor.callLater(1, _try_connect)

            # _try_connect()

            def _send(c):
                from transport.udp import udp_stream
                for idurl in sys.argv[2:]:
                    print('_send', list(udp_stream.streams().keys()))
                    p = signed.Packet(commands.Data(), my_id.getLocalID(),
                                      my_id.getLocalID(), 'packet%d' % c,
                                      bpio.ReadBinaryFile(sys.argv[1]), idurl)
                    gateway.outbox(p)
                if c > 1:
                    reactor.callLater(0.01, _send, c - 1)

            reactor.callLater(0, _send, 15)

    gateway.add_transport_state_changed_callback(_ok_to_send)
    reactor.run()
Пример #6
0
def main():
    lg.set_debug_level(18)
    lg.life_begins()
    from crypt import key
    key.InitMyKey()
    from contacts import identitycache
    identitycache.init()
    from system import tmpfile
    tmpfile.init()
    from services import driver
    driver.disabled_services().add('service_tcp_connections')
    driver.disabled_services().add('service_p2p_hookups')
    driver.disabled_services().add('service_nodes_lookup')
    driver.disabled_services().add('service_identity_propagate')
    driver.disabled_services().add('service_ip_port_responder')
    driver.init()
    driver.enabled_services().clear()
    driver.enabled_services().add('service_udp_transport')
    driver.enabled_services().add('service_udp_datagrams')
    driver.enabled_services().add('service_my_ip_port')
    driver.enabled_services().add('service_gateway')
    driver.enabled_services().add('service_entangled_dht')
    driver.enabled_services().add('service_network')
    driver.start()
    # options = { 'idurl': my_id.getLocalID(),}
    # options['host'] = nameurl.GetName(my_id.getLocalID())+'@'+'somehost.org'
    # options['dht_port'] = int(settings.getDHTPort())
    # options['udp_port'] = int(settings.getUDPPort())
    # udp.listen(int(settings.getUDPPort()))
    # dht_service.init(settings.getDHTPort())
    # dht_service.connect()
    # udp_node.A('go-online', options)
    reactor.addSystemEventTrigger('before', 'shutdown', gateway.shutdown)
    gateway.init()
    gateway.start()

    def _ok_to_send(transport, oldstate, newstate):
        if newstate != 'LISTENING':
            return
        # [filename] [peer idurl]
        if len(sys.argv) >= 3:
            # bpio.WriteFile(sys.argv[1]+'.signed', p.Serialize())

            def _try_reconnect():
                sess = udp_session.get_by_peer_id(sys.argv[2])
                reconnect = False
                if not sess:
                    reconnect = True
                    print 'sessions', udp_session.sessions_by_peer_id().keys()
                    print map(lambda s: s.peer_id, udp_session.sessions().values())
                else:
                    if sess.state != 'CONNECTED':
                        print 'state: ', sess.state
                        reconnect = True
                if reconnect:
                    print 'reconnect', sess
                    udp_session.add_pending_outbox_file(
                        sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False)
                    udp_node.A('connect', sys.argv[2])
                reactor.callLater(0.5, _try_reconnect)

            def _try_connect():
                if udp_node.A().state == 'LISTEN':
                    print 'connect'
                    gateway.stop_packets_timeout_loop()
                    udp_session.add_pending_outbox_file(
                        sys.argv[1] + '.signed', sys.argv[2], 'descr', Deferred(), False)
                    udp_node.A('connect', sys.argv[2])
                    reactor.callLater(5, _try_reconnect)
                else:
                    reactor.callLater(1, _try_connect)
            # _try_connect()

            def _send(c):
                from transport.udp import udp_stream
                for idurl in sys.argv[2:]:
                    print '_send', udp_stream.streams().keys()
                    p = signed.Packet(commands.Data(),
                                      my_id.getLocalID(),
                                      my_id.getLocalID(),
                                      'packet%d' % c,
                                      bpio.ReadBinaryFile(sys.argv[1]),
                                      idurl)
                    gateway.outbox(p)
                if c > 1:
                    reactor.callLater(0.01, _send, c - 1)
            reactor.callLater(0, _send, 15)

    gateway.add_transport_state_changed_callback(_ok_to_send)
    reactor.run()
Пример #7
0
 def on_resume(self, *args, **kwargs):
     from transport import gateway
     if kwargs.get('cold_start') is True:
         return gateway.cold_start()
     return gateway.start()