< prev index next >

## src/com/sun/javatest/agent/ActiveAgentPool.java

Print this page

     337                     e.readAhead();
     338                 }
     339             };
     340             Thread t = new Thread(r, "ActiveAgentPool.EntryWatcher" + entryWatcherCount++);
     341             t.start();
     342         }
     343
     344         synchronized boolean remove(Entry e) {
     345             if (v.contains(e)) {
     346                 v.removeElement(e);
     347                 notifyRemovedFromPool(e);
     348                 return true;
     349             }
     350             else
     351                 return false;
     352         }
     353
     354         synchronized Entry next() {
     355             Entry e = null;
     356             if (v.size() > 0) {
     357                 e = (Entry)(v.elementAt(0));
     358                 v.removeElementAt(0);
     359                 notifyRemovedFromPool(e);
     360             }
     361             return e;
     362         }
     363
     364         synchronized Entry next(int timeout) throws InterruptedException {
     365             long end = System.currentTimeMillis() + timeout;
     366             for (long t = timeout; t > 0;  t = end - System.currentTimeMillis()) {
     367                 if (v.size() == 0)
     368                     wait(t);
     369
     370                 Entry e = next();
     371                 if (e != null)
     372                     return e;
     373             }
     374             return null;
     375         }
     376
     377         synchronized void addObserver(Observer o) {
     378             observers = (Observer[])(DynamicArray.append(observers, o));
     379         }
     380
     381
     382         synchronized void deleteObserver(Observer o) {
     383             observers = (Observer[])(DynamicArray.remove(observers, o));
     384         }
     385
     386         private synchronized void notifyAddedToPool(Entry e) {
     387             for (int i = 0; i < observers.length; i++) {
     388                 observers[i].addedToPool(e);
     389             }
     390         }
     391
     392         private synchronized void notifyRemovedFromPool(Entry e) {
     393             for (int i = 0; i < observers.length; i++) {
     394                 observers[i].removedFromPool(e);
     395             }
     396         }
     397
     398         private Vector v = new Vector();
     399         private Observer[] observers = new Observer[0];
     400     }
     401
     402     /**
     403      * Listen for requests from active agents. Active agents announce their
     404      * willingness to work on behalf of a harness by contacting the harness
     405      * on a nominated port.  When a agent contacts the harness, it is put in
     406      * a pool to be used when agent clients request an unspecified agent.
     407      * @param port      The port on which to listen for agents.
     408      * @param timeout   The maximum time to wait for a agent to contact the
     409      *                  harness when one is needed. The timeout should be
     410      *                  in milliseconds.
     411      * @throws IOException if there a problems with any sockets
     412      *                  while performing this operation.
     413      */
     414     public synchronized void listen(int port, int timeout) throws IOException {
     415         setListening(false);
     416         setPort(port);
     417         setTimeout(timeout);
     418         setListening(true);
     337                     e.readAhead();
     338                 }
     339             };
     340             Thread t = new Thread(r, "ActiveAgentPool.EntryWatcher" + entryWatcherCount++);
     341             t.start();
     342         }
     343
     344         synchronized boolean remove(Entry e) {
     345             if (v.contains(e)) {
     346                 v.removeElement(e);
     347                 notifyRemovedFromPool(e);
     348                 return true;
     349             }
     350             else
     351                 return false;
     352         }
     353
     354         synchronized Entry next() {
     355             Entry e = null;
     356             if (v.size() > 0) {
     357                 e = v.elementAt(0);
     358                 v.removeElementAt(0);
     359                 notifyRemovedFromPool(e);
     360             }
     361             return e;
     362         }
     363
     364         synchronized Entry next(int timeout) throws InterruptedException {
     365             long end = System.currentTimeMillis() + timeout;
     366             for (long t = timeout; t > 0;  t = end - System.currentTimeMillis()) {
     367                 if (v.size() == 0)
     368                     wait(t);
     369
     370                 Entry e = next();
     371                 if (e != null)
     372                     return e;
     373             }
     374             return null;
     375         }
     376
     377         synchronized void addObserver(Observer o) {
     378             observers = (Observer[])(DynamicArray.append(observers, o));
     379         }
     380
     381
     382         synchronized void deleteObserver(Observer o) {
     383             observers = (Observer[])(DynamicArray.remove(observers, o));
     384         }
     385
     386         private synchronized void notifyAddedToPool(Entry e) {
     387             for (int i = 0; i < observers.length; i++) {
     388                 observers[i].addedToPool(e);
     389             }
     390         }
     391
     392         private synchronized void notifyRemovedFromPool(Entry e) {
     393             for (int i = 0; i < observers.length; i++) {
     394                 observers[i].removedFromPool(e);
     395             }
     396         }
     397
     398         private Vector<Entry> v = new Vector<>();
     399         private Observer[] observers = new Observer[0];
     400     }
     401
     402     /**
     403      * Listen for requests from active agents. Active agents announce their
     404      * willingness to work on behalf of a harness by contacting the harness
     405      * on a nominated port.  When a agent contacts the harness, it is put in
     406      * a pool to be used when agent clients request an unspecified agent.
     407      * @param port      The port on which to listen for agents.
     408      * @param timeout   The maximum time to wait for a agent to contact the
     409      *                  harness when one is needed. The timeout should be
     410      *                  in milliseconds.
     411      * @throws IOException if there a problems with any sockets
     412      *                  while performing this operation.
     413      */
     414     public synchronized void listen(int port, int timeout) throws IOException {
     415         setListening(false);
     416         setPort(port);
     417         setTimeout(timeout);
     418         setListening(true);

< prev index next >