[90] | 1 | import json
|
---|
| 2 | import logging
|
---|
| 3 | from pathlib import Path
|
---|
| 4 | import sys
|
---|
| 5 | import time
|
---|
| 6 | import traceback
|
---|
| 7 | from typing import List, Optional
|
---|
| 8 |
|
---|
| 9 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 10 | from tudelft.utilities.listener.Listener import Listener
|
---|
| 11 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
| 12 |
|
---|
| 13 | from geniusweb.events.ProtocolEvent import ProtocolEvent
|
---|
| 14 | from geniusweb.protocol.CurrentNegoState import CurrentNegoState
|
---|
| 15 | from geniusweb.protocol.NegoProtocol import NegoProtocol
|
---|
| 16 | from geniusweb.protocol.NegoSettings import NegoSettings
|
---|
| 17 | from geniusweb.protocol.NegoState import NegoState
|
---|
| 18 | from geniusweb.simplerunner.ClassPathConnectionFactory import ClassPathConnectionFactory
|
---|
| 19 | from geniusweb.simplerunner.Runner import Runner
|
---|
| 20 | from geniusweb.simplerunner.gui.GUI import GUI
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | class StdOutReporter (Reporter):
|
---|
| 24 |
|
---|
| 25 | def log(self, level:int , msg:str, exc:Optional[BaseException]=None):
|
---|
| 26 | if level >= logging.WARNING:
|
---|
| 27 | print(logging.getLevelName(level) + ":" + msg, file=sys.stderr)
|
---|
| 28 | else:
|
---|
| 29 | print(logging.getLevelName(level) + ":" + msg)
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | class NegoRunner:
|
---|
| 33 | '''
|
---|
| 34 | A simple tool to run a negotiation stand-alone, without starting the servers.
|
---|
| 35 | All referred files and classes need to be stored locally (or be in the
|
---|
| 36 | dependency list if you use maven).
|
---|
| 37 | <p>
|
---|
| 38 | <em>IMPORTANT</em> SimpleRunner has a number of restrictions, compared to a
|
---|
| 39 | run using a runserver and partyserver
|
---|
| 40 | <ul>
|
---|
| 41 | <li>With stand-alone runner, your parties are run together in a single
|
---|
| 42 | classloader. The main implication is that there may arise version conflicts
|
---|
| 43 | between parties.
|
---|
| 44 | <li>Stand-alone runner does NOT enforce the time deadline. Parties may
|
---|
| 45 | continue running indefinitely and thus bog down the JVM and stalling
|
---|
| 46 | tournaments.
|
---|
| 47 | </ul>
|
---|
| 48 | '''
|
---|
| 49 |
|
---|
| 50 | @staticmethod
|
---|
| 51 | def main(args:List[str]):
|
---|
| 52 | '''
|
---|
| 53 | The main runner
|
---|
| 54 |
|
---|
| 55 | @param args should have 0 or 1 argument. If 0 arguments, the {@link GUI}
|
---|
| 56 | is started. If one argoment, it should be a filename
|
---|
| 57 | containing a settings.json file. That session is then run.
|
---|
| 58 | @throws IOException if problem occurs
|
---|
| 59 | '''
|
---|
| 60 | if len(args) == 0:
|
---|
| 61 | GUI.main([])
|
---|
| 62 | return
|
---|
| 63 |
|
---|
| 64 | if len(args) != 1:
|
---|
| 65 | NegoRunner.showusage()
|
---|
| 66 | return;
|
---|
| 67 |
|
---|
| 68 | serialized = Path(args[0]).read_text("utf-8")
|
---|
| 69 | settings:NegoSettings = ObjectMapper().parse(json.loads(serialized),
|
---|
| 70 | NegoSettings) # type:ignore
|
---|
| 71 |
|
---|
| 72 | runner = Runner(settings,
|
---|
| 73 | ClassPathConnectionFactory(), StdOutReporter(), 0)
|
---|
| 74 | runner.run()
|
---|
| 75 |
|
---|
| 76 | @staticmethod
|
---|
| 77 | def showusage():
|
---|
| 78 | print("GeniusWeb stand-alone runner.")
|
---|
| 79 | print("first argument should be <settings.json>.")
|
---|
| 80 | print("The settings.json file should contain the NegoSettings.")
|
---|
| 81 | print("See the settings.json example file and the GeniusWeb wiki pages. ")
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | if __name__ == '__main__':
|
---|
| 85 | NegoRunner.main(sys.argv[1:])
|
---|
| 86 |
|
---|