def main(): configfilename = "/etc/spade/spade.xml" jabberxml = "/etc/spade/xmppd.xml" if sys.platform[:6] == 'netbsd': configfilename = os.sep + "usr" + os.sep + "pkg" + configfilename jabberxml = os.sep + "usr" + os.sep + "pkg" + jabberxml web = False if len(sys.argv) < 2: pass elif sys.argv[1] in ["--help", "-h"]: print_help() elif sys.argv[1] in ["--version", "-v"]: print_version() #elif sys.argv[1] in ["--web", "-w"]: web = True dbg = [] if os.name != "posix" or not os.path.exists(jabberxml) or not os.path.exists(configfilename): configfilename = "spade.xml" configfilename = os.path.abspath(configfilename) jabberxml = "xmppd.xml" jabberxml = os.path.abspath(jabberxml) try: for opt, arg in getopt(sys.argv[1:], "hvdwc:j:", ["help", "version", "debug", "web", "configfile=", "jabber="])[0]: if opt in ["-h", "--help"]: print_help() elif opt in ["-v", "--version"]: print_version() elif opt in ["-c", "--configfile"]: configfilename = arg elif opt in ["-j", "--jabber"]: jabberxml = arg #elif opt in ["-w", "--web"]: web = True elif opt in ["-d", "--debug"]: dbg = ['always'] except: pass print(("SPADE ", colors.color_yellow + __version__ + colors.color_none, " <*****@*****.**> - http://spade2.googlecode.com")) try: import psyco psyco.full() #psyco.profile() except ImportError: pass # print "W: Psyco optimizing compiler not found." sys.stdout.write("Starting SPADE") sys.stdout.flush() sys.stdout.write(".") sys.stdout.flush() jabberxml = os.path.abspath(jabberxml) if not os.path.exists(jabberxml): print(('\n There is no jabber config file (xmppd.xml)' + colors.color_red + " [failed]" + colors.color_none)) print_help() raise SystemExit if sys.platform[:6] == 'netbsd': pyvers = 'python' + str(numb) path = "/usr/pkg/lib" + os.sep + pyvers + "/site-packages/xmppd/" os.chdir(path) else: pass # os.chdir("xmppd") s = Server(cfgfile=jabberxml, cmd_options={'enable_debug': dbg, 'enable_psyco': False}) sys.stdout.write(".") sys.stdout.flush() _thread.start_new_thread(s.run, tuple()) try: sys.stdout.write(".") sys.stdout.flush() if not os.path.exists(configfilename): print(('\n There is no SPADE config file (spade.xml)' + colors.color_red + " [failed]" + colors.color_none)) print_help() raise SystemExit if dbg==['always']: platform = spade_backend.SpadeBackend(s, configfilename, debug=True) else: platform = spade_backend.SpadeBackend(s, configfilename) sys.stdout.write(".") sys.stdout.flush() platform.start() sys.stdout.write(".") sys.stdout.flush() s.DEBUG = platform.DEBUG sys.stdout.write(".") sys.stdout.flush() except Exception as e: _exception = sys.exc_info() if _exception[0]: print(('\n' + ''.join(traceback.format_exception(_exception[0], _exception[1], _exception[2])).rstrip())) print((str(e))) print((colors.color_red + " [failed]" + colors.color_none)) platform.shutdown() s.shutdown("Jabber server terminated...") raise SystemExit print((colors.color_green + " [done]" + colors.color_none)) if platform.acc.wui: print(("\n " + colors.color_yellow + " [info] " + colors.color_none + "WebUserInterface serving at port " + str(platform.acc.wui.port) + "\n ")) alive = True while alive: try: time.sleep(1) if not platform.alive: #The platform died (probable restart). Let's start it over platform.shutdown() print(("\n " + colors.color_green + "SPADE Platform restarting..." + colors.color_none)) platform.start() print((colors.color_green + " [done]" + colors.color_none)) except KeyboardInterrupt: alive = False sys.stdout.write("Exiting") sys.stdout.flush() platform.shutdown() sys.stdout.write(".") sys.stdout.flush() s.shutdown("SPADE Jabber server terminated...") sys.stdout.write(".") sys.stdout.flush() time.sleep(1) del s sys.stdout.write(".") sys.stdout.flush() print((colors.color_green + " Bye." + colors.color_none)) #raise SystemExit sys.exit(0)
def main(): configfilename = "/etc/spade/spade.xml" jabberxml = "/etc/spade/xmppd.xml" if sys.platform[:6] == 'netbsd': configfilename = os.sep + "usr" + os.sep + "pkg" + configfilename jabberxml = os.sep + "usr" + os.sep + "pkg" + jabberxml web = False if len(sys.argv) < 2: pass elif sys.argv[1] in ["--help", "-h"]: print_help() elif sys.argv[1] in ["--version", "-v"]: print_version() #elif sys.argv[1] in ["--web", "-w"]: web = True dbg = [] if os.name != "posix" or not os.path.exists( jabberxml) or not os.path.exists(configfilename): configfilename = "spade.xml" configfilename = os.path.abspath(configfilename) jabberxml = "xmppd.xml" jabberxml = os.path.abspath(jabberxml) try: for opt, arg in getopt( sys.argv[1:], "hvdwc:j:", ["help", "version", "debug", "web", "configfile=", "jabber="])[0]: if opt in ["-h", "--help"]: print_help() elif opt in ["-v", "--version"]: print_version() elif opt in ["-c", "--configfile"]: configfilename = arg elif opt in ["-j", "--jabber"]: jabberxml = arg #elif opt in ["-w", "--web"]: web = True elif opt in ["-d", "--debug"]: dbg = ['always'] except: pass print "SPADE ", colors.color_yellow + __version__ + colors.color_none, " <*****@*****.**> - https://github.com/javipalanca/SPADE" try: import psyco psyco.full() #psyco.profile() except ImportError: pass # print "W: Psyco optimizing compiler not found." sys.stdout.write("Starting SPADE") sys.stdout.flush() sys.stdout.write(".") sys.stdout.flush() jabberxml = os.path.abspath(jabberxml) if not os.path.exists(jabberxml): print '\n There is no jabber config file (xmppd.xml)' + colors.color_red + " [failed]" + colors.color_none print_help() raise SystemExit if sys.platform[:6] == 'netbsd': pyvers = 'python' + str(numb) path = "/usr/pkg/lib" + os.sep + pyvers + "/site-packages/xmppd/" os.chdir(path) else: pass # os.chdir("xmppd") s = Server(cfgfile=jabberxml, cmd_options={ 'enable_debug': dbg, 'enable_psyco': False }) sys.stdout.write(".") sys.stdout.flush() thread.start_new_thread(s.run, tuple()) try: sys.stdout.write(".") sys.stdout.flush() if not os.path.exists(configfilename): print '\n There is no SPADE config file (spade.xml)' + colors.color_red + " [failed]" + colors.color_none print_help() raise SystemExit if dbg == ['always']: platform = spade_backend.SpadeBackend(s, configfilename, debug=True) else: platform = spade_backend.SpadeBackend(s, configfilename) sys.stdout.write(".") sys.stdout.flush() platform.start() sys.stdout.write(".") sys.stdout.flush() s.DEBUG = platform.DEBUG sys.stdout.write(".") sys.stdout.flush() except Exception, e: _exception = sys.exc_info() if _exception[0]: print '\n' + ''.join( traceback.format_exception(_exception[0], _exception[1], _exception[2])).rstrip() print str(e) print colors.color_red + " [failed]" + colors.color_none platform.shutdown() s.shutdown("Jabber server terminated...") raise SystemExit
def main(taxi, passenger, coordinator, port, num_taxis, num_passengers, scenario, name, passwd, backend_port, verbose): """Console script for taxi_simulator.""" if verbose > 0: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) # generate config if not os.path.exists("spade.xml") or not os.path.exists("xmppd.xml"): os.system("configure.py 127.0.0.1") # reset user_db with open("user_db.xml", 'w') as f: pickle.dump({"127.0.0.1": {}}, f) debug_level = ['always'] if verbose > 2 else [] s = Server(cfgfile="xmppd.xml", cmd_options={ 'enable_debug': debug_level, 'enable_psyco': False }) thread.start_new_thread(s.run, tuple()) logger.info("XMPP server running.") platform = spade_backend.SpadeBackend(s, "spade.xml") platform.start() logger.info("Running SPADE platform.") debug_level = ['always'] if verbose > 1 else [] coordinator_agent = CoordinatorAgent(name + "@127.0.0.1", password=passwd, debug=debug_level, http_port=port, backend_port=backend_port, debug_level=debug_level) coordinator_agent.set_strategies(coordinator, taxi, passenger) coordinator_agent.start() logger.info("Creating {} taxis and {} passengers.".format( num_taxis, num_passengers)) create_agents_batch("taxi", num_taxis, coordinator_agent) create_agents_batch("passenger", num_passengers, coordinator_agent) if scenario: with open(scenario, 'r') as f: logger.info("Loading scenario {}".format(scenario)) scenario = json.load(f) for taxi in scenario["taxis"]: agent = create_agent(TaxiAgent, taxi["name"], taxi["password"], taxi["position"], None, debug_level) coordinator_agent.taxi_agents[taxi["name"]] = agent agent.start() for passenger in scenario["passengers"]: agent = create_agent(PassengerAgent, passenger["name"], passenger["password"], passenger["position"], passenger["dest"], debug_level) coordinator_agent.passenger_agents[passenger["name"]] = agent agent.start() app = Flask(__name__) @app.route("/generate/taxis/<int:ntaxis>/passengers/<int:npassengers>") @crossdomain(origin='*') def generate(ntaxis, npassengers): logger.info("Creating {} taxis and {} passengers.".format( ntaxis, npassengers)) create_agents_batch("taxi", ntaxis, coordinator_agent) create_agents_batch("passenger", npassengers, coordinator_agent) return "" app.run(host="127.0.0.1", port=backend_port) click.echo("\nTerminating...") coordinator_agent.stop_agents() coordinator_agent.stop() platform.shutdown() s.shutdown("") sys.exit(0)