KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 JavaDoc;
16 import java.rmi.Naming JavaDoc;
17 import java.net.MalformedURLException JavaDoc;
18
19 /**
20  * Managing class of RMI implementation of search subsystem of the SOFAnet.
21  * <p>
22  * It handles:
23  * <ul>
24  * <li>loading of search configuration
25  * <li>search connections to other nodes
26  * <li>creation of RMI server and client
27  * <li>"collectors" - objects that collects replies for search requests.
28  * </ul>
29  *
30  * @author Ladislav Sobr
31  */

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 JavaDoc myNodeName;
47   
48   /** Creates a new instance of RMISearchCenter */
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   /** Starts RMI search subsystem */
81   public void start() throws RMISearchException
82   {
83     //load configuration
84
config = new RMISearchConfig(rep, rmiSearchConnections);
85     config.loadFromStorage(true);
86     
87     started = true;
88     
89     //connect RMI search connections
90
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     //run RMI search server
101

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 JavaDoc e)
118     {
119       throw new RMISearchException("RMI search server failed to start", e);
120     }
121     catch (MalformedURLException JavaDoc e)
122     {
123       throw new RMISearchException("RMI search server failed to start", e);
124     }
125     
126   }
127   
128   /** Returns configuration of the RMI search subsystem */
129   public RMISearchConfiguration getConfiguration()
130   {
131     return config.getConfiguration();
132   }
133   
134   /** Sets configuration of the RMI search subsystem */
135   public void setConfiguration(RMISearchConfiguration configuration)
136   {
137     config.setConfiguration(configuration);
138   }
139   
140   /**
141    * Reloads configuration of the RMI search subsystem and makes it active.
142    * <p>
143    * Old connections are not destroyed, the new one are created.
144    */

145   public void applyConfiguration()
146   {
147     config.loadFromStorage(false);
148     
149     //connect new RMI search connections
150
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   /** Returns list with search connections (RMISearchConnection) to other nodes */
163   List getRMISearchConnections()
164   {
165     return rmiSearchConnections;
166   }
167   
168   boolean isStarted()
169   {
170     return started;
171   }
172   
173   /** Returns node filter that restricts which nodes can connect to this node */
174   NodeNameFilter getNodeFilter()
175   {
176     if (!started) return null;
177     return config.getNodeFilter();
178   }
179   
180   /** Returns list of seen search request IDs (SearchRequestID) - it is used to stop flood routing */
181   List getSeenSearchRequestIDs()
182   {
183     return seenSearchRequestIDs;
184   }
185   
186   /** Returns new collector for new search request */
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 JavaDoc(requestMark)) != null);
197       
198       RMISearchCollector collector = new RMISearchCollector(this, requestMark, myNodeName);
199       rmiSearchCollectors.put(new Integer JavaDoc(requestMark), collector);
200       return collector;
201     }
202   }
203   
204   /** Returns existing collector that belongs to concrete search request */
205   RMISearchCollector getCollector(int requestMark)
206   {
207     return (RMISearchCollector)rmiSearchCollectors.get(new Integer JavaDoc(requestMark));
208   }
209   
210   /** Deletes colector for concrete search request */
211   void removeCollector(int requestMark)
212   {
213     rmiSearchCollectors.remove(new Integer JavaDoc(requestMark));
214   }
215 }
216
Popular Tags