from common import bounty, peers, settings
from common.safeprint import safeprint
from multiprocessing import Queue, Value
from time import sleep, time
import pickle


def sync():
    from multiprocessing import Manager
    man = Manager()
    items = {'config': man.dict(),
             'peerList': man.list(),
             'bountyList': man.list(),
             'bountyLock': bounty.bountyLock,
             'keyList': man.list()}
    items['config'].update(settings.config)
    items['peerList'].extend(peers.peerlist)
    items['bountyList'].extend(bounty.bountyList)
    safeprint(items)
    peers.sync(items)
    return items


def initParallels():
    queue = Queue()
    live = Value('b', True)
    ear = peers.listener(settings.config['port'], settings.config['outbound'], queue, live, settings.config['server'])
    ear.daemon = True
    ear.items = sync()
    ear.start()
    mouth = peers.propagator(settings.config['port'] + 1, live)
    mouth.daemon = True
    mouth.items = ear.items
    mouth.start()
    feedback = []
    stamp = time()
    while queue.empty():
        if time() - 15 > stamp:
            break
    try:
        feedback = queue.get(False)
        settings.outbound = feedback[0]
        if settings.outbound is not True:
            global ext_ip, ext_port
            ext_ip, ext_port = feedback[1:3]
    except:
        safeprint("No feedback received from listener")
        global ext_ip, ext_port
        ext_ip = ""
        ext_port = -1
    return live


def main():
    # Begin Init
    settings.setup()
    try:
        import miniupnpc
    except:
        safeprint("Miniupnpc is not installed. Running in outbound only mode")
        settings.config['outbound'] = True
    safeprint("settings are:")
    safeprint(settings.config)
    live = initParallels()
    global ext_ip, ext_port
    peers.initializePeerConnections(settings.config['port'], ext_ip, ext_port)
    # End Init

    # Begin main loop
    if settings.config.get('seed'):
        safeprint("Seed mode activated")
        try:
            while True and not settings.config.get('test'):
                sleep(0.1)
        except KeyboardInterrupt:
            safeprint("Keyboard Interrupt")
    elif settings.config.get('server'):
        safeprint("Server mode activated")
    else:
        safeprint("Client mode activated")
    # End main loop

    # Begin shutdown
    safeprint("Beginning exit process")
    live.value = False
    settings.saveSettings()
    peers.saveToFile()
    bounty.saveToFile()
    # End shutdown

if __name__ == "__main__":
    main()

@@ -1122,16 +1122,78 @@
   break%0A
+    global ext_ip, ext_port%0A    ext_ip = %22%22%0A    ext_port = -1%0A
     try:
@@ -1185,32 +1185,32 @@
 t = -1%0A    try:%0A
-
         feedback
@@ -1315,44 +1315,8 @@
 ue:%0A
-            global ext_ip, ext_port%0A
     
@@ -1360,32 +1360,32 @@
 :3%5D%0A    except:%0A
+
         safeprin
@@ -1428,82 +1428,8 @@
 r%22)%0A
-        global ext_ip, ext_port%0A        ext_ip = %22%22%0A        ext_port = -1%0A
     

Fix global issue