1 6 7 package SOFA.SOFAnet.Search.RMI; 8 9 import SOFA.SOFAnet.Search.*; 10 import SOFA.SOFAnet.Core.SearchOps; 11 import SOFA.SOFAnet.Repository.Repository; 12 import SOFA.SOFAnet.Repository.NodeNameFilter; 13 import SOFA.SOFAnet.Repository.NodeInfo; 14 import java.util.*; 15 import java.rmi.RemoteException ; 16 import java.rmi.Naming ; 17 import java.net.MalformedURLException ; 18 19 32 public class RMISearchCenter 33 { 34 private Repository rep; 35 private SearchOps searchOps; 36 37 private RMISearchClient client; 38 private RMISearchServer server; 39 40 private List rmiSearchConnections; 41 private RMISearchConfig config; 42 private List seenSearchRequestIDs; 43 private Map rmiSearchCollectors; 44 boolean started; 45 private Random randomGenerator; 46 private String myNodeName; 47 48 49 public RMISearchCenter() 50 { 51 started = false; 52 53 client = new RMISearchClient(this); 54 server = null; 55 56 rmiSearchConnections = Collections.synchronizedList(new LinkedList()); 57 seenSearchRequestIDs = Collections.synchronizedList(new LinkedList()); 58 rmiSearchCollectors = Collections.synchronizedMap(new HashMap()); 59 60 randomGenerator = new Random(); 61 62 myNodeName = NodeInfo.getLocalNodeName(); 63 } 64 65 public void setRepository(Repository repository) 66 { 67 rep = repository; 68 } 69 70 public void setSearchOps(SearchOps searchOps) 71 { 72 this.searchOps = searchOps; 73 } 74 75 public SearchInterface getSearchInterface() 76 { 77 return client; 78 } 79 80 81 public void start() throws RMISearchException 82 { 83 config = new RMISearchConfig(rep, rmiSearchConnections); 85 config.loadFromStorage(true); 86 87 started = true; 88 89 synchronized (rmiSearchConnections) 91 { 92 Iterator it = rmiSearchConnections.iterator(); 93 while (it.hasNext()) 94 { 95 RMISearchConnection connection = (RMISearchConnection)it.next(); 96 connection.connect(); 97 } 98 } 99 100 102 NodeInfo myNodeInfo = new NodeInfo(); 103 try 104 { 105 myNodeInfo.setNodeName(myNodeName); 106 } 107 catch (NodeInfo.InvalidNodeNameException e) 108 { 109 throw new RMISearchException("Invalid name of local SOFA node: " + myNodeName, e); 110 } 111 112 try 113 { 114 server = new RMISearchServer(this, searchOps, myNodeInfo); 115 Naming.rebind("//" + myNodeInfo.getAddressAndPort() + "/SOFAnet/RMISearchServer", server); 116 } 117 catch (RemoteException e) 118 { 119 throw new RMISearchException("RMI search server failed to start", e); 120 } 121 catch (MalformedURLException e) 122 { 123 throw new RMISearchException("RMI search server failed to start", e); 124 } 125 126 } 127 128 129 public RMISearchConfiguration getConfiguration() 130 { 131 return config.getConfiguration(); 132 } 133 134 135 public void setConfiguration(RMISearchConfiguration configuration) 136 { 137 config.setConfiguration(configuration); 138 } 139 140 145 public void applyConfiguration() 146 { 147 config.loadFromStorage(false); 148 149 synchronized (rmiSearchConnections) 151 { 152 Iterator it = rmiSearchConnections.iterator(); 153 while (it.hasNext()) 154 { 155 RMISearchConnection connection = (RMISearchConnection)it.next(); 156 if (!connection.isConnected()) connection.connect(); 157 } 158 } 159 160 } 161 162 163 List getRMISearchConnections() 164 { 165 return rmiSearchConnections; 166 } 167 168 boolean isStarted() 169 { 170 return started; 171 } 172 173 174 NodeNameFilter getNodeFilter() 175 { 176 if (!started) return null; 177 return config.getNodeFilter(); 178 } 179 180 181 List getSeenSearchRequestIDs() 182 { 183 return seenSearchRequestIDs; 184 } 185 186 187 RMISearchCollector newCollector() 188 { 189 int requestMark; 190 191 synchronized (rmiSearchCollectors) 192 { 193 do 194 { 195 requestMark = randomGenerator.nextInt(); 196 } while (rmiSearchCollectors.get(new Integer (requestMark)) != null); 197 198 RMISearchCollector collector = new RMISearchCollector(this, requestMark, myNodeName); 199 rmiSearchCollectors.put(new Integer (requestMark), collector); 200 return collector; 201 } 202 } 203 204 205 RMISearchCollector getCollector(int requestMark) 206 { 207 return (RMISearchCollector)rmiSearchCollectors.get(new Integer (requestMark)); 208 } 209 210 211 void removeCollector(int requestMark) 212 { 213 rmiSearchCollectors.remove(new Integer (requestMark)); 214 } 215 } 216 | Popular Tags |