Example #1
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():
            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()
Example #2
0
def on_outbox_packet(outpacket,
                     wide,
                     callbacks,
                     target=None,
                     route=None,
                     response_timeout=None,
                     keep_alive=True):
    """
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            lg.warn(
                'skip creating new outbox packet because found similar packet: %r'
                % active_packet)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route,
                                response_timeout, keep_alive)
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([('packet', outpacket.PacketID)])
    return pkt_out
Example #3
0
 def __repr__(self):
     args = '%s(%s)' % (str(self.Command), str(self.PacketID))
     if _Debug:
         if lg.is_debug(_DebugLevel):
             args += ' %s|%s for %s' % (nameurl.GetName(
                 self.OwnerID), nameurl.GetName(
                     self.CreatorID), nameurl.GetName(self.RemoteID))
     return 'signed{ %s }' % args
Example #4
0
 def __repr__(self):
     args = '%s(%s)' % (str(self.Command), str(self.PacketID))
     if _Debug:
         if lg.is_debug(_DebugLevel):
             args += ' %s|%s for %s' % (
                 nameurl.GetName(self.OwnerID),
                 nameurl.GetName(self.CreatorID),
                 nameurl.GetName(self.RemoteID))
     return 'signed.Packet[%s]' % args
Example #5
0
def ReadLocalFiles():
    """
    This method scans local backups and build the whole "local" matrix.
    """
    global _LocalFilesNotifyCallback
    local_files().clear()
    local_max_block_numbers().clear()
    local_backup_size().clear()
    _counter = [0]

    def visit(realpath, subpath, name):
        # subpath is something like 0/0/1/0/F20131120053803PM/0-1-Data
        if not os.path.isfile(realpath):
            return True
        if realpath.startswith("newblock-"):
            return False
        if subpath in [
            settings.BackupIndexFileName(),
            settings.BackupInfoFileName(),
            settings.BackupInfoFileNameOld(),
            settings.BackupInfoEncryptedFileName(),
        ]:
            return False
        try:
            version = subpath.split("/")[-2]
        except:
            return False
        if not packetid.IsCanonicalVersion(version):
            return True
        LocalFileReport(packetID=subpath)
        _counter[0] += 1
        return False

    bpio.traverse_dir_recursive(visit, settings.getLocalBackupsDir())
    lg.out(8, "backup_matrix.ReadLocalFiles  %d files indexed" % _counter[0])
    if lg.is_debug(8):
        try:
            if sys.version_info >= (2, 6):
                # localSZ = sys.getsizeof(local_files())
                # remoteSZ = sys.getsizeof(remote_files())
                import lib.getsizeof

                localSZ = lib.getsizeof.total_size(local_files())
                remoteSZ = lib.getsizeof.total_size(remote_files())
                indexByName = lib.getsizeof.total_size(backup_fs.fs())
                indexByID = lib.getsizeof.total_size(backup_fs.fsID())
                lg.out(10, "    all local info uses %d bytes in the memory" % localSZ)
                lg.out(10, "    all remote info uses %d bytes in the memory" % remoteSZ)
                lg.out(10, "    index by name takes %d bytes in the memory" % indexByName)
                lg.out(10, "    index by ID takes %d bytes in the memory" % indexByID)
        except:
            lg.exc()
    if _LocalFilesNotifyCallback is not None:
        _LocalFilesNotifyCallback()
Example #6
0
def on_outbox_packet(outpacket, wide, callbacks, target=None, route=None):
    """
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route)
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([('packet', outpacket.PacketID)])
    return pkt_out
Example #7
0
def on_outbox_packet(outpacket, wide, callbacks, target=None, route=None):
    """
    
    """
    started_packets = packet_out.search_similar_packets(outpacket)
    if started_packets:
        for active_packet, active_item in started_packets:
            if callbacks:
                for command, cb in callbacks.items():
                    active_packet.set_callback(command, cb)
            return active_packet
    pkt_out = packet_out.create(outpacket, wide, callbacks, target, route)
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([("packet", outpacket.PacketID)])
    return pkt_out
Example #8
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()
Example #9
0
def init():
    global _WSGIListener
    global _WSGIPort
    result = Deferred()
    if _Debug:
        lg.out(_DebugLevel, 'control.init')
    request_update()
    if _WSGIListener:
        if _Debug:
            lg.out(_DebugLevel, '    SKIP listener already exist')
        result.callback(0)
        return result

    try:
        import django


#         ver = django.get_version()
#         if not ver.startswith('1.7'):
#             if _Debug:
#                 lg.out(_DebugLevel, '    Django version must be 1.7, skip!')
#             result.callback(0)
#             return result
    except:
        lg.exc()
        result.callback(0)
        return result

    if _Debug:
        lg.out(_DebugLevel + 6, '    \n' + pprint.pformat(sys.path))

    if _Debug:
        lg.out(
            _DebugLevel,
            '    setting environment DJANGO_SETTINGS_MODULE=web.asite.settings'
        )
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings")

    from django.core.wsgi import get_wsgi_application
    from django.conf import settings as django_settings
    from django.core import management
    # from django.contrib.auth.management.commands import changepassword

    if _Debug:
        lg.out(_DebugLevel,
               '    configuring WSGI bridge from Twisted to Django')
    wsgi_handler = get_wsgi_application()
    my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler)
    pool = threadpool.ThreadPool()
    pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler)
    root = DjangoRootResource(resource)
    root_static_dir = os.path.join(bpio.getExecutableDir(), "web")
    for sub in os.listdir(root_static_dir):
        static_path = os.path.join(root_static_dir, sub, 'static')
        if not os.path.isdir(static_path):
            continue
        node = static.File(static_path)
        root.putChild(sub, node)
        if _Debug:
            lg.out(_DebugLevel,
                   '        added static dir: %s->%s' % (sub, static_path))
        if sub == 'asite':
            admin_path = os.path.join(root_static_dir, sub, 'admin', 'static')
            node.putChild('admin', static.File(admin_path))
            if _Debug:
                lg.out(
                    _DebugLevel,
                    '        added ADMIN static dir: admin->%s' % admin_path)
    site = server.Site(root)
    _WSGIPort = 8080  # TODO: read port num from settings
    if _Debug:
        lg.out(_DebugLevel, '        %s' % my_wsgi_handler)
        lg.out(_DebugLevel, '        %s' % resource)
        lg.out(_DebugLevel, '        %s' % site)

    verbosity = 0
    if lg.is_debug(18):
        verbosity = 3
    if lg.is_debug(12):
        verbosity = 2
    if lg.is_debug(8):
        verbosity = 1

    # lg.out(4, '    running django "flush" command')
    # management.call_command('flush', interactive=False, verbosity=verbosity)

    # lg.out(4, '    running django "createsuperuser" command')
    # management.call_command('createsuperuser',
    #     interactive=False, verbosity=verbosity,
    #     username="******", email="admin@localhost")
    # command = changepassword.Command()
    # command._get_pass = lambda *args: 'admin'
    # command.execute("admin")

    if _Debug:
        lg.out(_DebugLevel, '    running django "syncdb" command')
    management.call_command('syncdb',
                            stdout=open(
                                os.path.join(settings.LogsDir(),
                                             'django-syncdb.log'), 'w'),
                            interactive=False,
                            verbosity=verbosity)

    _ShortPoolPort = 8081  # TODO: read port num from settings
    # shortpool.init(get_update_items, set_updated, _ShortPoolPort)

    if _Debug:
        lg.out(_DebugLevel, '    starting listener: %s' % site)
    result = start_listener(site)
    result.addCallback(lambda portnum: post_init(portnum))

    return result
Example #10
0
def inbox(info):
    """
    1) The protocol modules write to temporary files and gives us that filename
    2) We unserialize 3) We check that it is for us 4) We check that it is from
    one of our contacts.

    5) We use signed.validate() to check signature and that number
    fields are numbers 6) Any other sanity checks we can do and if
    anything funny we toss out the packet . 7) Then change the filename
    to the PackedID that it should be.    and call the right function(s)
    for this new packet    (encryptedblock, scrubber, remotetester,
    customerservice, ...)    to dispatch it to right place(s). 8) We
    have to keep track of bandwidth to/from everyone, and make a report
    every 24 hours    which we send to BitDust sometime in the 24 hours
    after that.
    """
    global _LastInboxPacketTime
    #     if _DoingShutdown:
    #         if _Debug:
    #             lg.out(_DebugLevel - 4, "gateway.inbox ignoring input since _DoingShutdown ")
    #         return None
    if info.filename == "" or not os.path.exists(info.filename):
        lg.err("bad filename=" + info.filename)
        return None
    try:
        data = bpio.ReadBinaryFile(info.filename)
    except:
        lg.err("gateway.inbox ERROR reading file " + info.filename)
        return None
    if len(data) == 0:
        lg.err("gateway.inbox ERROR zero byte file from %s://%s" %
               (info.proto, info.host))
        return None
    if callback.run_finish_file_receiving_callbacks(info, data):
        lg.warn(
            'incoming data of %d bytes was filtered out in file receiving callbacks'
            % len(data))
        return None
    try:
        newpacket = signed.Unserialize(data)
    except:
        lg.err("gateway.inbox ERROR during Unserialize data from %s://%s" %
               (info.proto, info.host))
        lg.exc()
        return None
    if newpacket is None:
        lg.warn("newpacket from %s://%s is None" % (info.proto, info.host))
        return None
    # newpacket.Valid() will be called later in the flow in packet_in.handle() method
    try:
        Command = newpacket.Command
        OwnerID = newpacket.OwnerID
        CreatorID = newpacket.CreatorID
        PacketID = newpacket.PacketID
        Date = newpacket.Date
        Payload = newpacket.Payload
        RemoteID = newpacket.RemoteID
        Signature = newpacket.Signature
        packet_sz = len(data)
    except:
        lg.err("gateway.inbox ERROR during Unserialize data from %s://%s" %
               (info.proto, info.host))
        lg.err("data length=" + str(len(data)))
        lg.exc()
        #         fd, filename = tmpfile.make('other', '.bad')
        #         os.write(fd, data)
        #         os.close(fd)
        return None
    _LastInboxPacketTime = time.time()
    if _Debug:
        lg.out(
            _DebugLevel - 8,
            "gateway.inbox [%s] signed by %s|%s (for %s) from %s://%s" %
            (Command, nameurl.GetName(OwnerID), nameurl.GetName(CreatorID),
             nameurl.GetName(RemoteID), info.proto, info.host))
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    # control.request_update([('packet', newpacket.PacketID)])
    return newpacket
Example #11
0
def init(UI='', options=None, args=None, overDict=None, executablePath=None):
    """
    In the method ``main()`` program firstly checks the command line arguments
    and then calls this method to start the whole process.

    This initialize some low level modules and finally create an
    instance of ``initializer()`` state machine and send it an event
    "run".
    """
    global AppDataDir

    from logs import lg
    lg.out(4, 'bpmain.run UI="%s"' % UI)

    from system import bpio

    #---settings---
    from main import settings
    if overDict:
        settings.override_dict(overDict)
    settings.init(AppDataDir)
    if not options or options.debug is None:
        lg.set_debug_level(settings.getDebugLevel())
    from main import config
    config.conf().addCallback('logs/debug-level',
                              lambda p, value, o, r: lg.set_debug_level(value))

    #---USE_TRAY_ICON---
    if os.path.isfile(settings.LocalIdentityFilename()) and os.path.isfile(settings.KeyFileName()):
        try:
            from system.tray_icon import USE_TRAY_ICON
            if bpio.Mac() or not bpio.isGUIpossible():
                lg.out(4, '    GUI is not possible')
                USE_TRAY_ICON = False
            if USE_TRAY_ICON:
                from twisted.internet import wxreactor
                wxreactor.install()
                lg.out(4, '    wxreactor installed')
        except:
            USE_TRAY_ICON = False
            lg.exc()
    else:
        lg.out(4, '    local identity or key file is not ready')
        USE_TRAY_ICON = False
    lg.out(4, '    USE_TRAY_ICON=' + str(USE_TRAY_ICON))
    if USE_TRAY_ICON:
        from system import tray_icon
        icons_path = bpio.portablePath(os.path.join(bpio.getExecutableDir(), 'icons'))
        lg.out(4, 'bpmain.run call tray_icon.init(%s)' % icons_path)
        tray_icon.init(icons_path)

        def _tray_control_func(cmd):
            if cmd == 'exit':
                from . import shutdowner
                shutdowner.A('stop', 'exit')
        tray_icon.SetControlFunc(_tray_control_func)

    #---OS Windows init---
    if bpio.Windows():
        try:
            from win32event import CreateMutex  # @UnresolvedImport
            mutex = CreateMutex(None, False, "BitDust")
            lg.out(4, 'bpmain.run created a Mutex: %s' % str(mutex))
        except:
            lg.exc()

    #---twisted reactor---
    lg.out(4, 'bpmain.run want to import twisted.internet.reactor')
    try:
        from twisted.internet import reactor  # @UnresolvedImport
    except:
        lg.exc()
        sys.exit('Error initializing reactor in bpmain.py\n')

    #---logfile----
    if lg.logs_enabled() and lg.log_file():
        lg.out(2, 'bpmain.run want to switch log files')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_stop_redirecting()
        lg.close_log_file()
        lg.open_log_file(settings.MainLogFilename())
        # lg.open_log_file(settings.MainLogFilename() + '-' + time.strftime('%y%m%d%H%M%S') + '.log')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_start_redirecting()

    #---memdebug---
#    if settings.uconfig('logs.memdebug-enable') == 'True':
#        try:
#            from logs import memdebug
#            memdebug_port = int(settings.uconfig('logs.memdebug-port'))
#            memdebug.start(memdebug_port)
#            reactor.addSystemEventTrigger('before', 'shutdown', memdebug.stop)
#            lg.out(2, 'bpmain.run memdebug web server started on port %d' % memdebug_port)
#        except:
#            lg.exc()

    #---process ID---
    try:
        pid = os.getpid()
        pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        bpio.WriteTextFile(pid_file_path, str(pid))
        lg.out(2, 'bpmain.run wrote process id [%s] in the file %s' % (str(pid), pid_file_path))
    except:
        lg.exc()

#    #---reactor.callLater patch---
#    if lg.is_debug(12):
#        patchReactorCallLater(reactor)
#        monitorDelayedCalls(reactor)

#    #---plugins---
#    from plugins import plug
#    plug.init()
#    reactor.addSystemEventTrigger('before', 'shutdown', plug.shutdown)

    lg.out(2, "    python executable is: %s" % sys.executable)
    lg.out(2, "    python version is:\n%s" % sys.version)
    lg.out(2, "    python sys.path is:\n                %s" % ('\n                '.join(sys.path)))

    lg.out(2, "bpmain.run UI=[%s]" % UI)

    if lg.is_debug(20):
        lg.out(0, '\n' + bpio.osinfofull())

    lg.out(4, 'import automats')

    #---START!---
    from automats import automat
    automat.LifeBegins(lg.when_life_begins())
    automat.OpenLogFile(settings.AutomatsLog())

    from main import events
    events.init()

    from main import initializer
    IA = initializer.A()
    lg.out(4, 'sending event "run" to initializer()')
    reactor.callWhenRunning(IA.automat, 'run', UI)  # @UndefinedVariable
    return IA
Example #12
0
def init():
    global _WSGIListener
    global _WSGIPort
    result = Deferred()
    lg.out(4, 'control.init')
    request_update()
    if _WSGIListener:
        lg.out(4, '    SKIP listener already exist')
        result.callback(0)
        return result

    try:
        import django
        ver = django.get_version()
        if not ver.startswith('1.7'):
            lg.out(4, '    Django version must be 1.7, skip!')
            result.callback(0)
            return result
    except:
        lg.exc()
        result.callback(0)
        return result

    lg.out(10, '    \n' + pprint.pformat(sys.path))

    lg.out(4, '    setting environment DJANGO_SETTINGS_MODULE=web.asite.settings')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings")

    from django.core.wsgi import get_wsgi_application
    from django.conf import settings as django_settings
    from django.core import management
    from django.contrib.auth.management.commands import changepassword

    lg.out(4, '    configuring WSGI bridge from Twisted to Django')
    wsgi_handler = get_wsgi_application()
    my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler)
    pool = threadpool.ThreadPool()
    pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler)
    root = DjangoRootResource(resource)
    root_static_dir = os.path.join(bpio.getExecutableDir(), "web")
    for sub in os.listdir(root_static_dir):
        static_path = os.path.join(root_static_dir, sub, 'static')
        if not os.path.isdir(static_path):
            continue
        node = static.File(static_path)
        root.putChild(sub, node)
        lg.out(4, '        added static dir: %s->%s' % (sub, static_path))
        if sub == 'asite':
            admin_path = os.path.join(root_static_dir, sub, 'admin', 'static')
            node.putChild('admin', static.File(admin_path))
            lg.out(4, '        added ADMIN static dir: admin->%s' % admin_path)
    site = server.Site(root)
    _WSGIPort = 8080
    lg.out(4, '        %s' % my_wsgi_handler)
    lg.out(4, '        %s' % resource)
    lg.out(4, '        %s' % site)

    verbosity = 0
    if lg.is_debug(18):
        verbosity = 3
    if lg.is_debug(12):
        verbosity = 2
    if lg.is_debug(8):
        verbosity = 1

    # lg.out(4, '    running django "flush" command')
    # management.call_command('flush', interactive=False, verbosity=verbosity)

    # lg.out(4, '    running django "createsuperuser" command')
    # management.call_command('createsuperuser',
    #     interactive=False, verbosity=verbosity,
    #     username="******", email="admin@localhost")
    # command = changepassword.Command()
    # command._get_pass = lambda *args: 'admin'
    # command.execute("admin")

    lg.out(4, '    running django "syncdb" command')
    management.call_command('syncdb', stdout=sys.stdout,
                            interactive=False, verbosity=verbosity)

    lg.out(4, '    starting listener: %s' % site)
    result = start_listener(site)
    result.addCallback(lambda portnum: post_init(portnum))

    return result
Example #13
0
def init(UI='', options=None, args=None, overDict=None, executablePath=None):
    """
    In the method ``main()`` program firstly checks the command line arguments
    and then calls this method to start the whole process.

    This initialize some low level modules and finally create an
    instance of ``initializer()`` state machine and send it an event
    "run".
    """
    global AppDataDir

    from logs import lg
    lg.out(4, 'bpmain.run UI="%s"' % UI)

    from system import bpio

    #---settings---
    from main import settings
    if overDict:
        settings.override_dict(overDict)
    settings.init(AppDataDir)
    if not options or options.debug is None:
        lg.set_debug_level(settings.getDebugLevel())
    from main import config
    config.conf().addCallback('logs/debug-level',
                              lambda p, value, o, r: lg.set_debug_level(value))

    #---USE_TRAY_ICON---
    if os.path.isfile(settings.LocalIdentityFilename()) and os.path.isfile(settings.KeyFileName()):
        try:
            from system.tray_icon import USE_TRAY_ICON
            if bpio.Mac() or not bpio.isGUIpossible():
                lg.out(4, '    GUI is not possible')
                USE_TRAY_ICON = False
            if USE_TRAY_ICON:
                from twisted.internet import wxreactor
                wxreactor.install()
                lg.out(4, '    wxreactor installed')
        except:
            USE_TRAY_ICON = False
            lg.exc()
    else:
        lg.out(4, '    local identity or key file is not ready')
        USE_TRAY_ICON = False
    lg.out(4, '    USE_TRAY_ICON=' + str(USE_TRAY_ICON))
    if USE_TRAY_ICON:
        from system import tray_icon
        icons_path = bpio.portablePath(os.path.join(bpio.getExecutableDir(), 'icons'))
        lg.out(4, 'bpmain.run call tray_icon.init(%s)' % icons_path)
        tray_icon.init(icons_path)

        def _tray_control_func(cmd):
            if cmd == 'exit':
                import shutdowner
                shutdowner.A('stop', 'exit')
        tray_icon.SetControlFunc(_tray_control_func)

    #---OS Windows init---
    if bpio.Windows():
        try:
            from win32event import CreateMutex
            mutex = CreateMutex(None, False, "BitDust")
            lg.out(4, 'bpmain.run created a Mutex: %s' % str(mutex))
        except:
            lg.exc()

    #---twisted reactor---
    lg.out(4, 'bpmain.run want to import twisted.internet.reactor')
    try:
        from twisted.internet import reactor
    except:
        lg.exc()
        sys.exit('Error initializing reactor in bpmain.py\n')

    #---logfile----
    if lg.logs_enabled() and lg.log_file():
        lg.out(2, 'bpmain.run want to switch log files')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_stop_redirecting()
        lg.close_log_file()
        lg.open_log_file(settings.MainLogFilename() + '-' + time.strftime('%y%m%d%H%M%S') + '.log')
        if bpio.Windows() and bpio.isFrozen():
            lg.stdout_start_redirecting()

    #---memdebug---
#    if settings.uconfig('logs.memdebug-enable') == 'True':
#        try:
#            from logs import memdebug
#            memdebug_port = int(settings.uconfig('logs.memdebug-port'))
#            memdebug.start(memdebug_port)
#            reactor.addSystemEventTrigger('before', 'shutdown', memdebug.stop)
#            lg.out(2, 'bpmain.run memdebug web server started on port %d' % memdebug_port)
#        except:
#            lg.exc()

    #---process ID---
    try:
        pid = os.getpid()
        pid_file_path = os.path.join(settings.MetaDataDir(), 'processid')
        bpio.WriteFile(pid_file_path, str(pid))
        lg.out(2, 'bpmain.run wrote process id [%s] in the file %s' % (str(pid), pid_file_path))
    except:
        lg.exc()

#    #---reactor.callLater patch---
#    if lg.is_debug(12):
#        patchReactorCallLater(reactor)
#        monitorDelayedCalls(reactor)

#    #---plugins---
#    from plugins import plug
#    plug.init()
#    reactor.addSystemEventTrigger('before', 'shutdown', plug.shutdown)

    lg.out(2, "bpmain.run UI=[%s]" % UI)

    if lg.is_debug(20):
        lg.out(0, '\n' + bpio.osinfofull())

    lg.out(4, 'bpmain.run import automats')

    #---START!---
    from automats import automat
    automat.LifeBegins(lg.when_life_begins())
    automat.OpenLogFile(settings.AutomatsLog())

    import initializer
    I = initializer.A()
    lg.out(4, 'bpmain.run send event "run" to initializer()')
    reactor.callWhenRunning(I.automat, 'run', UI)
    return I
Example #14
0
def inbox(info):
    """
    1) The protocol modules write to temporary files and gives us that filename
    2) We unserialize 3) We check that it is for us 4) We check that it is from
    one of our contacts.

    5) We use signed.validate() to check signature and that number
    fields are numbers 6) Any other sanity checks we can do and if
    anything funny we toss out the packet . 7) Then change the filename
    to the PackedID that it should be.    and call the right function(s)
    for this new packet    (encryptedblock, scrubber, remotetester,
    customerservice, ...)    to dispatch it to right place(s). 8) We
    have to keep track of bandwidth to/from everyone, and make a report
    every 24 hours    which we send to BitDust sometime in the 24 hours
    after that.
    """
    global _DoingShutdown
    global _LastInboxPacketTime
    if _DoingShutdown:
        if _Debug:
            lg.out(_DebugLevel - 4, "gateway.inbox ignoring input since _DoingShutdown ")
        return None
    if info.filename == "" or not os.path.exists(info.filename):
        lg.err("bad filename=" + info.filename)
        return None
    try:
        data = bpio.ReadBinaryFile(info.filename)
    except:
        lg.err("gateway.inbox ERROR reading file " + info.filename)
        return None
    if len(data) == 0:
        lg.err("gateway.inbox ERROR zero byte file from %s://%s" % (info.proto, info.host))
        return None
    try:
        newpacket = signed.Unserialize(data)
    except:
        lg.err("gateway.inbox ERROR during Unserialize data from %s://%s" % (info.proto, info.host))
        lg.exc()
        return None
    if newpacket is None:
        lg.warn("newpacket from %s://%s is None" % (info.proto, info.host))
        return None
    try:
        Command = newpacket.Command
        OwnerID = newpacket.OwnerID
        CreatorID = newpacket.CreatorID
        PacketID = newpacket.PacketID
        Date = newpacket.Date
        Payload = newpacket.Payload
        RemoteID = newpacket.RemoteID
        Signature = newpacket.Signature
        packet_sz = len(data)
    except:
        lg.err("gateway.inbox ERROR during Unserialize data from %s://%s" % (info.proto, info.host))
        lg.err("data length=" + str(len(data)))
        lg.exc()
        fd, filename = tmpfile.make("other", ".bad")
        os.write(fd, data)
        os.close(fd)
        return None
    _LastInboxPacketTime = time.time()
    if _Debug:
        lg.out(
            _DebugLevel - 8,
            "gateway.inbox [%s] signed by %s|%s (for %s) from %s://%s"
            % (
                Command,
                nameurl.GetName(OwnerID),
                nameurl.GetName(CreatorID),
                nameurl.GetName(RemoteID),
                info.proto,
                info.host,
            ),
        )
    if _Debug and lg.is_debug(_DebugLevel):
        monitoring()
    control.request_update([("packet", newpacket.PacketID)])
    return newpacket