aboutsummaryrefslogtreecommitdiff
path: root/src/WaiterClient.java
blob: f6b00b56bcf73a150a4ca07d421733d44d8c36b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * a helper class to implement the Ivy.waitForClient method
 *
 * @see Ivy.waitForClient
 *
 * @author	Yannick Jestin
 * @author	<a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a>
 *
 *  CHANGELOG:
 *  1.2.8:
 *    added a test during the waiting loop
 */

package fr.dgac.ivy ;
import java.util.Map;

class WaiterClient extends IvyApplicationAdapter implements Runnable {
    private static final int INCREMENT = 100;
    int timeout;
    private IvyClient received=null;
    private boolean forever=false;
    private Thread t;
    String name;
    private Map <Integer,IvyClient>clients ;

    WaiterClient(String n,int timeout,Map <Integer,IvyClient>clients) {
      this.timeout=timeout;
      this.clients=clients;
      name=n;
      if (timeout<=0) forever=true;
      t=new Thread(this);
      t.setName("Ivy Waiter thread, for client");
    }

    IvyClient waitForClient() {
      t.start();
      try { t.join(); } catch (InterruptedException ie) { return null; }
      return received;
    }

    public void run() {
      boolean encore=true;
      // System.out.println("DEV WaiterClient start");
      while (encore) {
	try {
	  if (INCREMENT>0) Thread.sleep(INCREMENT);
	  if (!forever) {
	    timeout-=INCREMENT;
	    if (timeout<=0) encore=false;
	  }
	} catch (InterruptedException ie) {
	  break;
	}
        if ((received=Ivy.alreadyThere(clients,name))!=null) break;
      }
      // System.out.println("DEV WaiterClient stop");
    }

    public void connect(fr.dgac.ivy.IvyClient client)  {
      if (name.compareTo(client.getApplicationName())!=0) return;
      received=client;
      t.interrupt();
    }
  }