1 6 7 package SOFA.SOFAnet.Search.RMI; 8 9 import SOFA.SOFAnet.Search.*; 10 import java.util.*; 11 12 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 shareGroup; 28 private int timeout; 29 30 31 public RMISearchCollector(RMISearchCenter center, int requestMark, String 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 41 public int getRequestMark() 42 { 43 return requestMark; 44 } 45 46 47 public void setup(SearchPattern searchPattern, boolean firstOnly, String shareGroup, int timeout) 48 { 49 this.searchPattern = searchPattern; 50 this.firstOnly = firstOnly; 51 this.shareGroup = shareGroup; 52 this.timeout = timeout; 53 } 54 55 59 public synchronized List request() 60 { 61 if (finished) return replies; 62 63 center.getSeenSearchRequestIDs().add(requestID); 65 66 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 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 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 |