KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Core > ReturnSharedAcceptanceTester


1 /*
2  * ReturnSharedAcceptanceTester.java
3  *
4  * Created on 21. duben 2004, 19:00
5  */

6
7 package SOFA.SOFAnet.Core;
8
9 import SOFA.SOFAnet.Repository.NodeNameList;
10 import SOFA.SOFAnet.Transport.*;
11 import java.lang.Object JavaDoc;
12 import java.util.*;
13
14 /**
15  * The object of this class is used to test which share client can return it's licence to the share manager.
16  * The inquiring of the share clients is done paralelly.
17  *
18  * @author Ladislav Sobr
19  */

20 class ReturnSharedAcceptanceTester
21 {
22   private final String JavaDoc bundleName;
23   private final NodeNameList clients;
24   private final TransportInterface transport;
25   
26   private Object JavaDoc lock;
27   private int counter;
28   private final int num;
29   private int result;
30
31   private class OneNodeTester extends Thread JavaDoc
32   {
33     private String JavaDoc nodeName;
34     private int index;
35     
36     OneNodeTester(String JavaDoc nodeName, int index)
37     {
38       this.nodeName = nodeName;
39       this.index = index;
40     }
41
42     public void run()
43     {
44       IOParams ioParams = new IOParams();
45       ioParams.setBundleName(bundleName);
46       ioParams.setNodeName(nodeName);
47       
48       boolean can = false;
49       try
50       {
51         transport.canReturnShared(ioParams);
52         if (ioParams.getErrCode() == 0) can = true;
53       }
54       catch (TransportException e)
55       {
56         can = false;
57         Reporter.warning("Accessing to SOFAnode '" + nodeName + "' failed", e);
58       }
59       
60       synchronized (lock)
61       {
62         counter++;
63         if (can) result = index;
64         if (can || counter == num) lock.notify();
65       }
66     }
67   }
68   
69   /** Creates a new instance of ReturnSharedAcceptanceTester
70    *
71    * @param bundleName name of bundle
72    * @param clients list of clients that should be asked
73    * @param transport interface of transport layer
74    */

75   public ReturnSharedAcceptanceTester(String JavaDoc bundleName, NodeNameList clients, TransportInterface transport)
76   {
77     this.bundleName = bundleName;
78     this.clients = clients;
79     this.transport = transport;
80     
81     lock = new Object JavaDoc();
82     counter = 0;
83     num = clients.getList().size();
84     result = -1;
85   }
86
87   /**
88    * Runs tester. Can be called only once per instance!
89    * @return index of client (in clients list) who can return shared bundle; -1 if no such client exists
90    */

91   public int go()
92   {
93     OneNodeTester[] testers = new OneNodeTester[num];
94     
95     int i = 0;
96     Iterator it = clients.getList().iterator();
97     while (it.hasNext())
98     {
99       String JavaDoc nodeName = (String JavaDoc)it.next();
100       
101       testers[i] = new OneNodeTester(nodeName, i);
102       i++;
103     }
104     
105     synchronized (lock)
106     {
107       for (i = 0; i < num; i++) testers[i].start();
108       try
109       {
110         lock.wait(10000); //10 second timeout
111
}
112       catch (InterruptedException JavaDoc e)
113       {
114       }
115       
116       return result;
117     }
118   }
119   
120 }
121
Popular Tags