def shutdown_all_outbox_packets():
    """
    """
    if _Debug:
        lg.out(
            _DebugLevel,
            'gateway.shutdown_all_outbox_packets, %d live objects at the moment'
            % len(packet_out.queue()))
    for pkt_out in list(packet_out.queue()):
        pkt_out.event('cancel', 'shutdown')
def packets_timeout_loop():
    global _PacketsTimeOutTask
    # lg.out(18, 'gateway.packets_timeout_loop')
    delay = 5
    if _Debug:
        delay = 1
    _PacketsTimeOutTask = reactor.callLater(delay, packets_timeout_loop)
    for pkt_in in packet_in.items().values():
        if pkt_in.is_timed_out():
            if _Debug:
                lg.out(
                    _DebugLevel - 4,
                    'gateway.packets_timeout_loop %r is timed out: %s' %
                    (pkt_in, pkt_in.timeout))
            pkt_in.automat('cancel', 'timeout')
    for pkt_out in packet_out.queue():
        if pkt_out.is_timed_out():
            if _Debug:
                lg.out(
                    _DebugLevel - 4,
                    'gateway.packets_timeout_loop %r is timed out: %s' %
                    (pkt_out, pkt_out.timeout))
            pkt_out.automat('cancel', 'timeout')
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
Exemple #3
0
def current_bytes_sent():
    res = {}
    for pkt_out in packet_out.queue():
        for item in pkt_out.items:
            if item.transfer_id:
                res[item.transfer_id] = pkt_out.payloadsize
    return res
Exemple #4
0
def SendToIDs(idlist, wide=False, ack_handler=None, timeout_handler=None, response_timeout=20):
    """
    Same, but send to many IDs and also check previous packets to not re-send.
    """
    global _PropagateCounter
    if _Debug:
        lg.out(_DebugLevel, "propagate.SendToIDs to %d users, wide=%s" % (len(idlist), wide))
    if ack_handler is None:
        ack_handler = HandleAck
    if timeout_handler is None:
        timeout_handler = HandleTimeOut
    LocalIdentity = my_id.getLocalIdentity()
    Payload = strng.to_bin(LocalIdentity.serialize())
    alreadysent = set()
    totalsent = 0
    inqueue = {}
    found_previous_packets = 0
    for pkt_out in packet_out.queue():
        if id_url.is_in(pkt_out.remote_idurl, idlist, as_field=False):
            if pkt_out.description.count('Identity'):
                if pkt_out.remote_idurl not in inqueue:
                    inqueue[pkt_out.remote_idurl] = 0
                inqueue[pkt_out.remote_idurl] += 1
                found_previous_packets += 1
    for contact in idlist:
        if not contact:
            continue
        if contact in alreadysent:
            # just want to send once even if both customer and supplier
            continue
        if contact in inqueue and inqueue[contact] > 2:
            # now only 2 protocols is working: tcp and udp
            if _Debug:
                lg.out(_DebugLevel, '        skip sending [Identity] to %s, packet already in the queue' % contact)
            continue
        p = signed.Packet(
            Command=commands.Identity(),
            OwnerID=my_id.getLocalID(),
            CreatorID=my_id.getLocalID(),
            PacketID=('propagate:%d:%s' % (_PropagateCounter, packetid.UniqueID())),
            Payload=Payload,
            RemoteID=contact,
        )
        _PropagateCounter += 1
        if _Debug:
            lg.out(_DebugLevel, "        sending [Identity] to %s" % nameurl.GetName(contact))
        gateway.outbox(p, wide, response_timeout=response_timeout, callbacks={
            commands.Ack(): ack_handler,
            commands.Fail(): ack_handler,
            None: timeout_handler,
        })
        if wide:
            # this is a ping packet - need to clear old info
            p2p_stats.ErasePeerProtosStates(contact)
            p2p_stats.EraseMyProtosStates(contact)
        alreadysent.add(contact)
        totalsent += 1
    del alreadysent
    return totalsent
def current_bytes_sent():
    res = {}
    # for transfer_id, info in transfers_out().items():
    #     res[transfer_id] = info.size
    for pkt_out in packet_out.queue():
        for item in pkt_out.items:
            if item.transfer_id:
                res[item.transfer_id] = pkt_out.payloadsize
    return res
Exemple #6
0
def current_bytes_sent():
    res = {}
    # for transfer_id, info in transfers_out().items():
    #     res[transfer_id] = info.size
    for pkt_out in packet_out.queue():
        for item in pkt_out.items:
            if item.transfer_id:
                res[item.transfer_id] = pkt_out.payloadsize
    return res
Exemple #7
0
def shutdown_all_outbox_packets():
    """
    
    """
    if _Debug:
        lg.out(
            _DebugLevel, "gateway.shutdown_all_outbox_packets, %d live objects at the moment" % len(packet_out.queue())
        )
    for pkt_out in list(packet_out.queue()):
        pkt_out.event("cancel", "shutdown")
Exemple #8
0
def monitoring():
    list_pkt_in = []
    for pkt_in in packet_in.items().values():
        list_pkt_in.append(pkt_in.label)
    list_pkt_out = []
    for pkt_out in packet_out.queue():
        list_pkt_out.append(pkt_out.label)
    if transport_log() and list_pkt_out and list_pkt_in:
        dt = time.time() - lg.when_life_begins()
        mn = dt // 60
        sc = dt - mn * 60
        transport_log().write("%02d:%02d    in: %s   out: %s\n" % (mn, sc, list_pkt_in, list_pkt_out))
        transport_log().flush()
def monitoring():
    list_pkt_in = []
    for pkt_in in packet_in.items().values():
        list_pkt_in.append(pkt_in.label)
    list_pkt_out = []
    for pkt_out in packet_out.queue():
        list_pkt_out.append(pkt_out.label)
    if transport_log() and list_pkt_out and list_pkt_in:
        dt = time.time() - lg.when_life_begins()
        mn = dt // 60
        sc = dt - mn * 60
        transport_log().write('%02d:%02d    in: %s   out: %s\n' %
                              (mn, sc, list_pkt_in, list_pkt_out))
        transport_log().flush()
def _list_in_out_packets(params):
    result = []
    for pkt_out in packet_out.queue():
        result.append({
            'name': pkt_out.outpacket.Command,
            'label': pkt_out.label,
            'from_to': 'to',
            'target': pkt_out.remote_idurl,
        })
    for pkt_in in packet_in.items().values():
        result.append({
            'name': pkt_in.transfer_id,
            'label': pkt_in.label,
            'from_to': 'from',
            'target': pkt_in.sender_idurl,
        })
    return {'result': result, }
Exemple #11
0
def _list_in_out_packets(params):
    result = []
    for pkt_out in packet_out.queue():
        result.append({
            'name': pkt_out.outpacket.Command,
            'label': pkt_out.label,
            'from_to': 'to',
            'target': pkt_out.remote_idurl,
        })
    for pkt_in in list(packet_in.inbox_items()).values():
        result.append({
            'name': pkt_in.transfer_id,
            'label': pkt_in.label,
            'from_to': 'from',
            'target': pkt_in.sender_idurl,
        })
    return {'result': result, }
Exemple #12
0
def packets_timeout_loop():
    global _PacketsTimeOutTask
    # lg.out(18, 'gateway.packets_timeout_loop')
    delay = 5
    if _Debug:
        delay = 1
    _PacketsTimeOutTask = reactor.callLater(delay, packets_timeout_loop)
    for pkt_in in packet_in.items().values():
        if pkt_in.is_timed_out():
            lg.out(8, "gateway.packets_timeout_loop %r is timed out" % pkt_in)
            pkt_in.automat("cancel", "timeout")
    for pkt_out in packet_out.queue():
        if pkt_out.is_timed_out():
            lg.out(8, "gateway.packets_timeout_loop %r is timed out" % pkt_out)
            pkt_out.automat("cancel", "timeout")
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
Exemple #13
0
def packets_timeout_loop():
    global _PacketsTimeOutTask
    delay = 10
    _PacketsTimeOutTask = reactor.callLater(
        delay, packets_timeout_loop)  # @UndefinedVariable
    for pkt_in in list(packet_in.inbox_items().values()):
        if pkt_in.is_timed_out():
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'gateway.packets_timeout_loop %r is timed out: %s' %
                    (pkt_in, pkt_in.timeout))
            pkt_in.automat('cancel', 'timeout')
    for pkt_out in packet_out.queue():
        if pkt_out.is_timed_out():
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'gateway.packets_timeout_loop %r is timed out: %s' %
                    (pkt_out, pkt_out.timeout))
            pkt_out.automat('cancel', 'timeout')
Exemple #14
0
def SendToIDs(idlist,
              wide=False,
              ack_handler=None,
              timeout_handler=None,
              response_timeout=20):
    """
    Same, but send to many IDs and also check previous packets to not re-send.
    """
    lg.out(8, "propagate.SendToIDs to %d users, wide=%s" % (len(idlist), wide))
    if ack_handler is None:
        ack_handler = HandleAck
    if timeout_handler is None:
        timeout_handler = HandleTimeOut
    # MyID = my_id.getLocalID()
    # PacketID = MyID
    LocalIdentity = my_id.getLocalIdentity()
    Payload = strng.to_bin(LocalIdentity.serialize())
    # Hash = key.Hash(Payload)
    alreadysent = set()
    totalsent = 0
    inqueue = {}
    found_previous_packets = 0
    for pkt_out in packet_out.queue():
        if pkt_out.remote_idurl in idlist:
            if pkt_out.description.count('Identity'):
                if pkt_out.remote_idurl not in inqueue:
                    inqueue[pkt_out.remote_idurl] = 0
                inqueue[pkt_out.remote_idurl] += 1
                found_previous_packets += 1
    for contact in idlist:
        if not contact:
            continue
        if contact in alreadysent:
            # just want to send once even if both customer and supplier
            continue
        if contact in inqueue and inqueue[contact] > 2:
            # now only 2 protocols is working: tcp and udp
            lg.out(
                8,
                '        skip sending [Identity] to %s, packet already in the queue'
                % contact)
            continue


#        found_previous_packets = 0
#        for transfer_id in gate.transfers_out_by_idurl().get(contact, []):
#            ti = gate.transfers_out().get(transfer_id, None)
#            if ti and ti.description.count('Identity'):
#                found_previous_packets += 1
#                break
#        if found_previous_packets >= 3:
#            lg.out(8, '        skip sending to %s' % contact)
#            continue
        p = signed.Packet(
            commands.Identity(),
            my_id.getLocalID(),  # MyID,
            my_id.getLocalID(),  # MyID,
            commands.Identity(
            ),  #'Identity',  # my_id.getLocalID(), #PacketID,
            Payload,
            contact,
        )
        lg.out(8,
               "        sending [Identity] to %s" % nameurl.GetName(contact))
        # callback.register_interest(AckHandler, signed.RemoteID, signed.PacketID)
        gateway.outbox(p,
                       wide,
                       response_timeout=response_timeout,
                       callbacks={
                           commands.Ack(): ack_handler,
                           commands.Fail(): ack_handler,
                           None: timeout_handler,
                       })
        if wide:
            # this is a ping packet - need to clear old info
            p2p_stats.ErasePeerProtosStates(contact)
            p2p_stats.EraseMyProtosStates(contact)
        alreadysent.add(contact)
        totalsent += 1
    del alreadysent
    return totalsent