KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package SOFA.SOFAnet.Search.RMI;
8
9 import java.rmi.server.UnicastRemoteObject JavaDoc;
10 import java.rmi.server.ServerNotActiveException JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12 import SOFA.SOFAnet.Repository.NodeInfo;
13 import SOFA.SOFAnet.Repository.NodeNameFilter;
14 import SOFA.SOFAnet.Repository.ShareGroups;
15 import SOFA.SOFAnet.Search.*;
16 import SOFA.SOFAnet.Core.SearchOps;
17 import java.util.*;
18
19 /**
20  * RMI implementation of search subsytem - server part.
21  *
22  * @author Ladislav Sobr
23  */

24 public class RMISearchServer extends UnicastRemoteObject JavaDoc implements RMISearchInterface
25 {
26   private RMISearchCenter center;
27   private SearchOps searchOps;
28   private NodeInfo myNodeInfo;
29
30   /** Creates a new instance of RMISearchServer */
31   public RMISearchServer(RMISearchCenter center, SearchOps searchOps, NodeInfo myNodeInfo) throws RemoteException JavaDoc
32   {
33     this.center = center;
34     this.searchOps = searchOps;
35     this.myNodeInfo = myNodeInfo;
36   }
37   
38   /**
39    * Connects remote node to us.
40    * <p>
41    * The server checks whether the connection in opposite direction exists and if not, the opposite connection is created
42    */

43   public void connect(String JavaDoc nodeName, ShareGroups shareGroups) throws RMISearchException, RemoteException JavaDoc
44   {
45     NodeInfo nodeInfo = testNodeName(nodeName);
46     
47     //test whether the calling node is allowed here
48
NodeNameFilter nodeFilter = center.getNodeFilter();
49     if (nodeFilter != null && !nodeFilter.pass(nodeName))
50     {
51       throw new RMISearchException("Access to the search subsystem not allowed");
52     }
53     
54     //do we have this connection?
55
boolean connectionFound = false;
56     List rmiSearchConnections = center.getRMISearchConnections();
57     synchronized (rmiSearchConnections)
58     {
59       Iterator it = rmiSearchConnections.iterator();
60       while (it.hasNext())
61       {
62         RMISearchConnection connection = (RMISearchConnection)it.next();
63         if (connection.isNode(nodeName))
64         {
65           connectionFound = true;
66           break;
67         }
68       }
69     }
70     
71     //if not, create it
72
if (!connectionFound)
73     {
74       RMISearchConnection connection = new RMISearchConnection(nodeInfo, shareGroups, false);
75       connection.connect();
76       rmiSearchConnections.add(connection);
77     }
78   }
79   
80   /**
81    * Processes the search request
82    * <p>
83    * If the search request already arrived to the node, it is discarded. But if not,
84    * the request is sent to all connections (flood routing) from the node (If the request has shareGroup filled, only connections with such shareGroup are used).
85    * Then the request is evaluated on this node (this evaluation routine is responsible for replying).
86    */

87   public void request(String JavaDoc nodeName, SearchRequestID requestID, SearchPattern searchPattern, boolean firstOnly, String JavaDoc shareGroup) throws RMISearchException, RemoteException JavaDoc
88   {
89     testNodeName(nodeName);
90     
91     //test whether the calling node is allowed here
92
NodeNameFilter nodeFilter = center.getNodeFilter();
93     if (nodeFilter != null && !nodeFilter.pass(nodeName))
94     {
95       throw new RMISearchException("Access to the search subsystem not allowed");
96     }
97     
98     //haven't we seen the request already?
99
List seenSearchRequestIDs = center.getSeenSearchRequestIDs();
100     
101     if (seenSearchRequestIDs.contains(requestID))
102     {
103       //we have already processed this request
104
return;
105     }
106
107     //ok we will process the request
108

109     //add request to seen requests
110
seenSearchRequestIDs.add(requestID);
111     if (seenSearchRequestIDs.size() > 100) seenSearchRequestIDs.remove(0); //we remember only last 100 seen requests
112

113     //send request to other nodes
114
List rmiSearchConnections = center.getRMISearchConnections();
115     synchronized (rmiSearchConnections)
116     {
117       Iterator it = rmiSearchConnections.iterator();
118       while (it.hasNext())
119       {
120         RMISearchConnection connection = (RMISearchConnection)it.next();
121         if (!connection.isNode(nodeName)) //do not send to the sending node
122
{
123           connection.sendRequest(requestID, searchPattern, firstOnly, shareGroup);
124         }
125       }
126     }
127
128     //evaluate request
129
searchOps.evaluateRequest(requestID, searchPattern, firstOnly, shareGroup);
130   }
131
132   /**
133    * Receives reply to the request.
134    * The appropriate collector of the request is found and the reply is handed over to it
135    */

136   public void reply(String JavaDoc nodeName, SearchReply searchReply) throws RMISearchException, RemoteException JavaDoc
137   {
138     testNodeName(nodeName);
139     
140     if (myNodeInfo.getName().compareTo(searchReply.getRequestID().getSourceNode()) != 0)
141     {
142       throw new RMISearchException("Search reply arrived at node '" + myNodeInfo.getName() + "', but request was made by node'" + searchReply.getRequestID().getSourceNode() + "'");
143     }
144     
145     List replies = searchReply.getReplies();
146     if (replies == null || replies.size() == 0)
147     {
148       throw new RMISearchException("Empty search replies");
149     }
150     
151     RMISearchCollector collector = center.getCollector(searchReply.getRequestID().getRequestMark());
152     
153     if (collector != null)
154     {
155       //probablythere is still someone waiting for replies
156
collector.reply(replies);
157     }
158   }
159   
160   /** Tests wheher specified name of node corresponds to the real host of RMI client */
161   private NodeInfo testNodeName(String JavaDoc nodeName) throws RMISearchException
162   {
163     NodeInfo nodeInfo = new NodeInfo();
164     try
165     {
166       nodeInfo.setNodeName(nodeName);
167     }
168     catch (NodeInfo.InvalidNodeNameException e)
169     {
170       throw new RMISearchException("Invalid name of SOFA node: " + nodeName, e);
171     }
172
173     String JavaDoc realHost = "";
174     
175     try
176     {
177       realHost = getClientHost();
178     }
179     catch (ServerNotActiveException JavaDoc e)
180     {
181       throw new RMISearchException(e.getMessage(), e);
182     }
183     
184     boolean ok = false;
185     try
186     {
187       ok = nodeInfo.isSameHost(realHost);
188     }
189     catch (NodeInfo.InvalidNodeNameException e)
190     {
191       throw new RMISearchException("Invalid RMI client host: " + nodeName, e);
192     }
193     
194     if (!ok) throw new RMISearchException("Name of SOFA node (" + nodeName + ") and RMI client host (" + realHost + ") are incosistent");
195     
196     return nodeInfo;
197   }
198 }
199
Popular Tags