Changeset 21 for simplerunner/src/main
- Timestamp:
- 09/22/20 08:52:04 (4 years ago)
- Location:
- simplerunner/src/main/java/geniusweb/simplerunner
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
simplerunner/src/main/java/geniusweb/simplerunner/BasicConnection.java
r9 r21 3 3 import java.io.IOException; 4 4 import java.net.URI; 5 import java.util.concurrent.ArrayBlockingQueue; 5 6 6 7 import geniusweb.connection.ConnectionEnd; … … 22 23 // to be initialized 23 24 private Listener<OUT> handler = null; 25 private Thread handlerThread; 26 private ArrayBlockingQueue<OUT> messages = new ArrayBlockingQueue<OUT>(4); 24 27 25 28 /** … … 33 36 this.reference = reference; 34 37 this.uri = uri; 38 35 39 } 36 40 … … 47 51 } 48 52 this.handler = newhandler; 53 54 handlerThread = new Thread(new Runnable() { 55 56 @Override 57 public void run() { 58 try { 59 while (true) { 60 handler.notifyChange(messages.take()); 61 } 62 } catch (InterruptedException e) { 63 System.out.println("connection handler interrupted"); 64 } 65 } 66 }); 67 68 handlerThread.start(); 49 69 } 50 70 … … 55 75 "BasicConnection has not been initialized"); 56 76 } 57 handler.notifyChange(data); 77 try { 78 messages.put(data); 79 } catch (InterruptedException e) { 80 throw new IOException(e); 81 } 58 82 } 59 83 … … 70 94 @Override 71 95 public void close() { 72 96 handlerThread.interrupt(); 97 handlerThread = null; 98 handler = null; 73 99 } 74 100 -
simplerunner/src/main/java/geniusweb/simplerunner/ClassPathConnectionFactory.java
r9 r21 11 11 import geniusweb.actions.Action; 12 12 import geniusweb.actions.PartyId; 13 import geniusweb.inform.Inform; 13 14 import geniusweb.party.Party; 14 import geniusweb.party.inform.Inform;15 15 import geniusweb.protocol.partyconnection.ProtocolToPartyConn; 16 16 import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory; … … 22 22 * <code>classpath:org/my/package/class</code> 23 23 * 24 *25 *26 24 */ 27 25 public class ClassPathConnectionFactory implements ProtocolToPartyConnFactory { -
simplerunner/src/main/java/geniusweb/simplerunner/NegoRunner.java
r18 r21 29 29 private final ProtocolToPartyConnFactory connectionfactory; 30 30 protected final Reporter log; 31 private boolean properlyStopped = false; 31 32 private final static ObjectMapper jackson = new ObjectMapper(); 33 private final int LOOPTIME = 200;// ms 34 private long maxruntime; 32 35 36 /** 37 * 38 * @param settings 39 * @param connectionfactory 40 * @param logger 41 * @param maxruntime limit in millisecs. Ignored if 0 42 */ 33 43 public NegoRunner(NegoSettings settings, 34 ProtocolToPartyConnFactory connectionfactory, Reporter logger) { 44 ProtocolToPartyConnFactory connectionfactory, Reporter logger, 45 long maxruntime) { 35 46 if (settings == null || connectionfactory == null) { 36 47 throw new NullPointerException("Arguments must be not null"); … … 40 51 this.protocol = settings.getProtocol(log); 41 52 this.connectionfactory = connectionfactory; 53 this.maxruntime = maxruntime; 54 } 55 56 public boolean isProperlyStopped() { 57 return properlyStopped; 42 58 } 43 59 … … 46 62 protocol.addListener(evt -> handle(evt)); 47 63 protocol.start(connectionfactory); 64 long remainingtime = maxruntime; 65 while (!properlyStopped && (maxruntime == 0 || remainingtime > 0)) { 66 try { 67 Thread.sleep(LOOPTIME); 68 remainingtime -= LOOPTIME; 69 } catch (InterruptedException e) { 70 e.printStackTrace(); 71 } 72 } 73 System.out.println("end run"); 48 74 } 49 75 … … 56 82 57 83 protected void stop() { 58 Level level = protocol.getState().getError() == null ? Level.INFO 59 : Level.WARNING; 60 logFinal(level, protocol.getState()); 84 logFinal(Level.INFO, protocol.getState()); 85 properlyStopped = true; 61 86 } 62 87 … … 88 113 89 114 NegoRunner runner = new NegoRunner(settings, 90 new ClassPathConnectionFactory(), new StdOutReporter() );115 new ClassPathConnectionFactory(), new StdOutReporter(), 0); 91 116 runner.run(); 92 117 } 93 118 94 119 private static void showusage() { 95 System.err.println("GeniusWeb stand-alone runner. 120 System.err.println("GeniusWeb stand-alone runner."); 96 121 System.err.println("first argument should be <settings.json>."); 97 122 System.err.println( … … 100 125 "See the settings.json example file and the GeniusWeb wiki pages. "); 101 126 127 } 128 129 /** 130 * @return protocol that runs/ran the session. 131 */ 132 public NegoProtocol getProtocol() { 133 return protocol; 102 134 } 103 135 }
Note:
See TracChangeset
for help on using the changeset viewer.