KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RMISearchClient.java
3  *
4  * Created on 29. duben 2004, 16:09
5  */

6
7 package SOFA.SOFAnet.Search.RMI;
8
9 import SOFA.SOFAnet.Search.*;
10 import SOFA.SOFAnet.Repository.NodeInfo;
11 import java.util.*;
12
13 /**
14  * RMI implementation of SearchInterface - client interface of search subsytem.
15  *
16  * @author Ladislav Sobr
17  */

18 public class RMISearchClient implements SearchInterface
19 {
20   private RMISearchCenter center;
21   
22   /** Creates a new instance of RMISearchClient */
23   public RMISearchClient(RMISearchCenter center)
24   {
25     this.center = center;
26   }
27   
28   /**
29    * Sends reply to the requesting node.
30    * <p>
31    * If the connection to the node exists it is used. If not, the conenction is created temporarily.
32    */

33   public void reply(SearchReply reply) throws SearchException
34   {
35     String JavaDoc requestingNodeName = reply.getRequestID().getSourceNode();
36     
37     NodeInfo requestingNodeInfo = new NodeInfo();
38     try
39     {
40       requestingNodeInfo.setNodeName(requestingNodeName);
41     }
42     catch (NodeInfo.InvalidNodeNameException e)
43     {
44       throw new SearchException("Invalid name of SOFA node: " + requestingNodeName, e);
45     }
46     
47     //try to find existing connection to the requesting node and use it to send reply
48
List rmiSearchConnections = center.getRMISearchConnections();
49     synchronized (rmiSearchConnections)
50     {
51       Iterator it = rmiSearchConnections.iterator();
52       while (it.hasNext())
53       {
54         RMISearchConnection connection = (RMISearchConnection)it.next();
55         if (connection.isNode(requestingNodeName))
56         {
57           try
58           {
59             connection.sendReply(reply);
60           }
61           catch (RMISearchException e)
62           {
63             throw new SearchException(e.getMessage(), e);
64           }
65           
66           return;
67         }
68       }
69     }
70     
71     //we didn't find the existing connection to the requesting node, create the new one only for reply
72
RMISearchConnection connection = new RMISearchConnection(requestingNodeInfo, null, true);
73
74     try
75     {
76       connection.sendReply(reply);
77     }
78     catch (RMISearchException e)
79     {
80       throw new SearchException(e.getMessage(), e);
81     }
82     
83   }
84   
85   /**
86    * Sends the search request to the search network.
87    * <p>
88    * The collector for the request is created. The request is sent by this collector that is also responsible
89    * for collecting replies. When the call of collector finishes, the collector is destroyed.
90    */

91   public void request(SearchRequest request) throws SearchException
92   {
93     RMISearchCollector collector = center.newCollector();
94     
95     collector.setup(request.getSearchPattern(), request.getFirstOnly(), request.getShareGroup(), request.getTimeout());
96     List replies = collector.request();
97     center.removeCollector(collector.getRequestMark());
98     
99     request.setReplies(replies);
100     request.setErrCode(0);
101   }
102   
103 }
104
Popular Tags