1 6 7 package SOFA.SOFAnet.Search.RMI; 8 9 import java.rmi.server.UnicastRemoteObject ; 10 import java.rmi.server.ServerNotActiveException ; 11 import java.rmi.RemoteException ; 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 24 public class RMISearchServer extends UnicastRemoteObject implements RMISearchInterface 25 { 26 private RMISearchCenter center; 27 private SearchOps searchOps; 28 private NodeInfo myNodeInfo; 29 30 31 public RMISearchServer(RMISearchCenter center, SearchOps searchOps, NodeInfo myNodeInfo) throws RemoteException 32 { 33 this.center = center; 34 this.searchOps = searchOps; 35 this.myNodeInfo = myNodeInfo; 36 } 37 38 43 public void connect(String nodeName, ShareGroups shareGroups) throws RMISearchException, RemoteException 44 { 45 NodeInfo nodeInfo = testNodeName(nodeName); 46 47 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 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 (!connectionFound) 73 { 74 RMISearchConnection connection = new RMISearchConnection(nodeInfo, shareGroups, false); 75 connection.connect(); 76 rmiSearchConnections.add(connection); 77 } 78 } 79 80 87 public void request(String nodeName, SearchRequestID requestID, SearchPattern searchPattern, boolean firstOnly, String shareGroup) throws RMISearchException, RemoteException 88 { 89 testNodeName(nodeName); 90 91 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 List seenSearchRequestIDs = center.getSeenSearchRequestIDs(); 100 101 if (seenSearchRequestIDs.contains(requestID)) 102 { 103 return; 105 } 106 107 109 seenSearchRequestIDs.add(requestID); 111 if (seenSearchRequestIDs.size() > 100) seenSearchRequestIDs.remove(0); 113 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)) { 123 connection.sendRequest(requestID, searchPattern, firstOnly, shareGroup); 124 } 125 } 126 } 127 128 searchOps.evaluateRequest(requestID, searchPattern, firstOnly, shareGroup); 130 } 131 132 136 public void reply(String nodeName, SearchReply searchReply) throws RMISearchException, RemoteException 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 collector.reply(replies); 157 } 158 } 159 160 161 private NodeInfo testNodeName(String 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 realHost = ""; 174 175 try 176 { 177 realHost = getClientHost(); 178 } 179 catch (ServerNotActiveException 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 |