KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Search > RMI > RMISearchCollector


1 /*
2  * RMISearchCollector.java
3  *
4  * Created on 30. duben 2004, 14:38
5  */

6
7 package SOFA.SOFAnet.Search.RMI;
8
9 import SOFA.SOFAnet.Search.*;
10 import java.util.*;
11
12 /**
13  * The object sends search request to the search network and gathers the search replies/
14  *
15  * @author Ladislav Sobr
16  */

17 public class RMISearchCollector
18 {
19   private RMISearchCenter center;
20   private int requestMark;
21   private boolean finished;
22   private SearchRequestID requestID;
23   private List replies;
24   
25   private SearchPattern searchPattern;
26   private boolean firstOnly;
27   private String JavaDoc shareGroup;
28   private int timeout;
29   
30   /** Creates a new instance of RMISearchCollector */
31   public RMISearchCollector(RMISearchCenter center, int requestMark, String JavaDoc myNodeName)
32   {
33     this.center = center;
34     this.requestMark = requestMark;
35     requestID = new SearchRequestID(requestMark, myNodeName);
36     replies = new LinkedList();
37     finished = false;
38   }
39   
40   /** Returns identification of collector (and its search request) on the local node */
41   public int getRequestMark()
42   {
43     return requestMark;
44   }
45   
46   /** Sets the collector up. See SearchRequest class */
47   public void setup(SearchPattern searchPattern, boolean firstOnly, String JavaDoc shareGroup, int timeout)
48   {
49     this.searchPattern = searchPattern;
50     this.firstOnly = firstOnly;
51     this.shareGroup = shareGroup;
52     this.timeout = timeout;
53   }
54   
55   /**
56    * Sends the request to the search network and returns arrived replies
57    * @return list of SearchReplyItem objects
58    */

59   public synchronized List request()
60   {
61     if (finished) return replies;
62     
63     //add request to the seen requests
64
center.getSeenSearchRequestIDs().add(requestID);
65     
66     //send request to other nodes
67
List rmiSearchConnections = center.getRMISearchConnections();
68     synchronized (rmiSearchConnections)
69     {
70       Iterator it = rmiSearchConnections.iterator();
71       while (it.hasNext())
72       {
73         RMISearchConnection connection = (RMISearchConnection)it.next();
74         connection.sendRequest(requestID, searchPattern, firstOnly, shareGroup);
75       }
76     }
77     
78     long startTime = System.currentTimeMillis();
79     
80     for (;;)
81     {
82       long currentTime = System.currentTimeMillis();
83       long t = timeout - (currentTime - startTime);
84       if (t < 1) t = 100;
85       
86       try
87       {
88         wait(t);
89       }
90       catch (InterruptedException JavaDoc e)
91       {
92       }
93       
94       currentTime = System.currentTimeMillis();
95       if (firstOnly && replies.size() > 0 || currentTime - startTime > timeout)
96       {
97         finished = true;
98         return replies;
99       }
100     }
101   }
102   
103   /**
104    * Adds new replies to the collector
105    * @param newReplies list of SearchReplyItem objects
106    */

107   public synchronized void reply(List newReplies)
108   {
109     if (finished) return;
110     if (newReplies == null || newReplies.size() == 0) return;
111     
112     if (firstOnly)
113     {
114       if (replies.size() > 0) return;
115       
116       replies.add(newReplies.get(0));
117       notify();
118       return;
119     }
120     else replies.addAll(newReplies);
121   }
122   
123 }
124
Popular Tags