Public Member Functions | |
int | jvnGetObjectId () throws java.rmi.RemoteException,jvn.JvnException |
void | jvnRegisterObject (String jon, JvnObject jo, JvnRemoteServer js) throws java.rmi.RemoteException,jvn.JvnException |
JvnObject | jvnLookupObject (String jon, Class<?extends JvnObject > type, JvnRemoteServer js) throws java.rmi.RemoteException,jvn.JvnException |
Serializable | jvnLockRead (int joi, JvnRemoteServer js) throws java.rmi.RemoteException, JvnException |
Serializable | jvnLockWrite (int joi, JvnRemoteServer js) throws java.rmi.RemoteException, JvnException |
void | jvnServerTerminated (JvnRemoteServer js) throws RemoteException, JvnException |
Static Public Member Functions | |
static void | main (String argv[]) throws Exception |
Private Member Functions | |
JvnCoordImpl () throws Exception | |
Private Attributes | |
int | currID = -1 |
HashMap< String, JvnServers > | jvnServersOfNames = new HashMap<String, JvnServers>() |
HashMap< Integer, JvnServers > | jvnServersOfIdentifiers = new HashMap<Integer, JvnServers>() |
Static Private Attributes | |
static final long | serialVersionUID = -115305878534728396L |
static JvnCoordImpl | jo = null |
Definition at line 132 of file JvnCoordImpl.java.
jvn.JvnCoordImpl.JvnCoordImpl | ( | ) | throws Exception [private] |
Default constructor. Will rebind itself with the RMI registry.
Definition at line 168 of file JvnCoordImpl.java.
References jvn.JvnCoordImpl.currID.
Referenced by jvn.JvnCoordImpl.main().
00168 { 00169 Naming.rebind("AT_LD_jvnCoordinator", this); 00170 System.out.println("Javanaise Coordinator is ready."); 00171 currID = 0; 00172 }
static void jvn.JvnCoordImpl.main | ( | String | argv[] | ) | throws Exception [static] |
This is the main method. Its content is empty since the only thing it needs to do is done by the constructor.
Exception | ||
IOException |
Definition at line 180 of file JvnCoordImpl.java.
References jvn.JvnCoordImpl.jo, and jvn.JvnCoordImpl.JvnCoordImpl().
00180 { 00181 if(jo==null){ 00182 jo = new JvnCoordImpl(); 00183 System.out.println("Press ENTER to terminate the Javanaise Coordinator."); 00184 try { 00185 System.in.read(); 00186 } catch (IOException e) {} 00187 } 00188 }
int jvn.JvnCoordImpl.jvnGetObjectId | ( | ) | throws java.rmi.RemoteException,jvn.JvnException |
Gets a new object ID. Object IDs are unique for each coordinator.
Implements jvn.JvnRemoteCoord.
Definition at line 193 of file JvnCoordImpl.java.
References jvn.JvnCoordImpl.currID.
Referenced by jvn.JvnCoordImpl.jvnRegisterObject().
00194 { 00195 synchronized(this) { 00196 return (++currID); 00197 } 00198 }
void jvn.JvnCoordImpl.jvnRegisterObject | ( | String | jon, | |
JvnObject | jo, | |||
JvnRemoteServer | js | |||
) | throws java.rmi.RemoteException,jvn.JvnException |
Associate a symbolic name with a JVN object.
jon | : the JVN object name | |
jo | : the JVN object | |
js | : the remote reference of the JVNServer |
Implements jvn.JvnRemoteCoord.
Definition at line 209 of file JvnCoordImpl.java.
References jvn.JvnCoordImpl.jo, jvn.JvnCoordImpl.jvnGetObjectId(), jvn.JvnCoordImpl.jvnServersOfIdentifiers, jvn.JvnCoordImpl.jvnServersOfNames, and jvn.JvnServers.lockState.
00210 { 00211 // No one synchronizes on jvnServersOfNames, 00212 // synchronize on jvnServersOfIdentifiers 00213 synchronized(jvnServersOfIdentifiers) { 00214 00215 // Verify stuff 00216 if( jo==null ) { 00217 throw new JvnException("Invalid object!"); 00218 } else if( jo.jvnGetObjectId() < 0 ) { 00219 throw new JvnException( "Invalid object ID! Please call jvnGetObjectId " + 00220 "to get a new object ID and associate it with the " + 00221 "Javanaise object using the jvnSetObjectId method." ); 00222 } else if( jvnServersOfIdentifiers.containsKey(jo.jvnGetObjectId()) ) { 00223 throw new JvnException("Object with such an ID is already registered with this coordinator!"); 00224 } else if( jvnServersOfNames.containsKey(jon) ) { 00225 throw new JvnException("Object with such a name is already registered with this coordinator!"); 00226 } 00227 00228 System.out.println("Registering object \""+jon+"\" (Object ID: "+jo.jvnGetObjectId()+")"); 00229 00230 // All verifications are done, add to lists. 00231 JvnServers servers = new JvnServers(js,jo); 00232 servers.lockState = JvnObjectState.STATE_WLOCKT; 00233 jvnServersOfNames.put(jon, servers); 00234 jvnServersOfIdentifiers.put(jo.jvnGetObjectId(), servers); 00235 } 00236 }
JvnObject jvn.JvnCoordImpl.jvnLookupObject | ( | String | jon, | |
Class<?extends JvnObject > | type, | |||
JvnRemoteServer | js | |||
) | throws java.rmi.RemoteException,jvn.JvnException |
Get the reference of a JVN object managed by a given JVN server. The returned JVN object doesn't have any lock, therefore a JvnObject#jvnLockRead() or a JvnObject#jvnLockWrite() must be done after you get the reference.
jon | : the JVN object name | |
js | : the remote reference of the JVNServer |
Implements jvn.JvnRemoteCoord.
Definition at line 247 of file JvnCoordImpl.java.
References jvn.JvnServers.jvnObject, jvn.JvnCoordImpl.jvnServersOfIdentifiers, and jvn.JvnCoordImpl.jvnServersOfNames.
00248 { 00249 // Verify arguments 00250 if( js==null ) { 00251 throw new JvnException( "Invalid server! When calling this method " + 00252 "from a JvnRemoteServer, set the second "+ 00253 "argument to \"this\"."); 00254 } 00255 00256 System.out.println("Looked up object: "+jon); 00257 00258 // No one synchronizes on jvnServersOfNames, 00259 // synchronize on jvnServersOfIdentifiers 00260 synchronized(jvnServersOfIdentifiers) { 00261 JvnServers servers = jvnServersOfNames.get(jon); 00262 if( servers != null ) { 00263 System.out.println("Found object with name \"" + jon + "\""); 00264 return servers.jvnObject; 00265 } else { 00266 System.out.println("Not found object with name \"" + jon + "\""); 00267 return null; 00268 } 00269 } 00270 }
Serializable jvn.JvnCoordImpl.jvnLockRead | ( | int | joi, | |
JvnRemoteServer | js | |||
) | throws java.rmi.RemoteException, JvnException |
Get a read lock on a JVN object for a given JVN server.
joi | : the JVN object identification | |
js | : the remote reference of the server |
Implements jvn.JvnRemoteCoord.
Definition at line 280 of file JvnCoordImpl.java.
References jvn.JvnRemoteServer.jvnInvalidateWriterForReader(), jvn.JvnServers.jvnListOfServers, jvn.JvnCoordImpl.jvnServersOfIdentifiers, jvn.JvnServers.latestJvnObjectContent, and jvn.JvnServers.lockState.
00281 { 00282 // Verify arguments 00283 if( js==null ) { 00284 throw new JvnException( "Invalid server! When calling this method " + 00285 "from a JvnRemoteServer, set the second "+ 00286 "argument to \"this\"."); 00287 } 00288 00289 synchronized(jvnServersOfIdentifiers) 00290 { 00291 JvnServers servers = jvnServersOfIdentifiers.get(joi); 00292 if( servers == null ) { 00293 throw new JvnException( "The given object identifier is not registered" + 00294 "with this coordinator!" ); 00295 } else if( servers.lockState == JvnObjectState.STATE_NOLOCK ) { 00296 // No lock, just read lock and set the only reference 00297 System.out.println("Read lock: getting lock (was not locked before)."); 00298 servers.lockState = JvnObjectState.STATE_RLOCKT; 00299 servers.jvnListOfServers.clear(); 00300 servers.jvnListOfServers.add(js); 00301 } else if( servers.lockState == JvnObjectState.STATE_RLOCKT ) { 00302 // Read lock, just add a reference 00303 System.out.println("Read lock: adding reference."); 00304 servers.jvnListOfServers.add(js); 00305 } else if( servers.lockState == JvnObjectState.STATE_WLOCKT ) { 00306 try { 00307 System.out.println("Read lock: invalidating writer for reader."); 00308 JvnRemoteServer server = servers.jvnListOfServers.iterator().next(); 00309 servers.latestJvnObjectContent = server.jvnInvalidateWriterForReader(joi); 00310 servers.lockState = JvnObjectState.STATE_RLOCKT; 00311 if( server.equals(js)) { 00312 System.out.println("Read lock: downgraded from write lock."); 00313 } else { 00314 System.out.println("Read lock: adding reference."); 00315 servers.jvnListOfServers.add(js); 00316 } 00317 } catch( Exception e ) { 00318 // This will catch all kinds of stuff, including null pointers 00319 throw new JvnException("Error getting the read lock!\n"+e); 00320 } 00321 } else { 00322 throw new JvnException("Unexpected lock state: " + servers.lockState + " !\n"); 00323 } 00324 00325 return servers.latestJvnObjectContent; 00326 } 00327 }
Serializable jvn.JvnCoordImpl.jvnLockWrite | ( | int | joi, | |
JvnRemoteServer | js | |||
) | throws java.rmi.RemoteException, JvnException |
Get a Write lock on a JVN object for a given JVN server.
joi | : the JVN object identification | |
js | : the remote reference of the server |
Implements jvn.JvnRemoteCoord.
Definition at line 337 of file JvnCoordImpl.java.
References jvn.JvnServers.jvnListOfServers, jvn.JvnCoordImpl.jvnServersOfIdentifiers, jvn.JvnServers.latestJvnObjectContent, and jvn.JvnServers.lockState.
00338 { 00339 // Verify arguments 00340 if( js==null ) { 00341 throw new JvnException( "Invalid server! When calling this method " + 00342 "from a JvnRemoteServer, set the second "+ 00343 "argument to \"this\"."); 00344 } 00345 00346 synchronized(jvnServersOfIdentifiers) 00347 { 00348 JvnServers servers = jvnServersOfIdentifiers.get(joi); 00349 if( servers == null ) { 00350 throw new JvnException( "The given object identifier is not registered" + 00351 "with this coordinator!" ); 00352 } else if( servers.lockState == JvnObjectState.STATE_NOLOCK ) { 00353 // No lock, just read lock 00354 System.out.println("Write lock: getting lock (was not locked before)."); 00355 servers.lockState = JvnObjectState.STATE_WLOCKT; 00356 } else if( servers.lockState == JvnObjectState.STATE_RLOCKT ) { 00357 System.out.println("Write lock: invalidating readers."); 00358 // Get back the read lock from all servers 00359 int numberOfReaders = 0; 00360 Semaphore s = new Semaphore(0); 00361 00362 Iterator<JvnRemoteServer> i = servers.jvnListOfServers.iterator(); 00363 while(i.hasNext()) { 00364 numberOfReaders++; 00365 (new JvnInvalidateReaderThread(s,i.next(),joi)).start(); 00366 } 00367 00368 // Wait for all threads to end 00369 if( numberOfReaders>0 ) { 00370 try { 00371 s.acquire(numberOfReaders); 00372 } catch (InterruptedException e) {} 00373 } 00374 System.out.println("Write lock: invalidated "+numberOfReaders+" readers."); 00375 } else if( servers.lockState == JvnObjectState.STATE_WLOCKT ) { 00376 try { 00377 // Write-locked, request object 00378 System.out.println("Write lock: invalidating writer."); 00379 servers.latestJvnObjectContent = servers.jvnListOfServers.iterator().next().jvnInvalidateWriter(joi); 00380 System.out.println("Write lock: invalidated writer."); 00381 } catch( Exception e ) { 00382 throw new JvnException("Error getting the write lock!\n"+e); 00383 } 00384 } else { 00385 throw new JvnException("Unexpected lock state: "+servers.lockState+" !\n"); 00386 } 00387 00388 servers.jvnListOfServers.clear(); 00389 servers.jvnListOfServers.add(js); 00390 servers.lockState = JvnObjectState.STATE_WLOCKT; 00391 00392 return servers.latestJvnObjectContent; 00393 } 00394 }
void jvn.JvnCoordImpl.jvnServerTerminated | ( | JvnRemoteServer | js | ) | throws RemoteException, JvnException |
Called when a Javanaise Server has terminated, will remove all references to that server.
joi | : the remote reference of the server |
Implements jvn.JvnRemoteCoord.
Definition at line 402 of file JvnCoordImpl.java.
References jvn.JvnObject.jvnInvalidateWriter(), jvn.JvnServers.jvnListOfServers, jvn.JvnServers.jvnObject, jvn.JvnCoordImpl.jvnServersOfIdentifiers, jvn.JvnServers.latestJvnObjectContent, and jvn.JvnServers.lockState.
00402 { 00403 // All verifications are done, add to lists. 00404 // No one synchronizes on jvnServersOfNames, 00405 // synchronize on jvnServersOfIdentifiers 00406 System.out.println("A server has terminated: unregistering."); 00407 00408 synchronized(jvnServersOfIdentifiers) { 00409 Iterator<JvnServers> i = jvnServersOfIdentifiers.values().iterator(); 00410 Iterator<JvnRemoteServer> j = null; 00411 JvnRemoteServer server = null; 00412 JvnServers servers = null; 00413 00414 // Loop on the list of registered objects 00415 while(i.hasNext()) { 00416 servers = i.next(); 00417 j = servers.jvnListOfServers.iterator(); 00418 00419 // Loop on the list of servers 00420 while(j.hasNext()) { 00421 server = j.next(); 00422 00423 // Server found! Invalidate... 00424 if(server.equals(js)) { 00425 if(servers.lockState == JvnObjectState.STATE_WLOCKT) { 00426 System.out.println("A server has terminated: invalidating writer."); 00427 servers.latestJvnObjectContent = servers.jvnObject.jvnInvalidateWriter(); 00428 } 00429 servers.jvnListOfServers.remove(server); 00430 if(servers.jvnListOfServers.isEmpty()) { 00431 servers.lockState = JvnObjectState.STATE_NOLOCK; 00432 } 00433 break; 00434 } 00435 } 00436 } 00437 } 00438 System.out.println("A server has terminated: unregister complete."); 00439 }
final long jvn.JvnCoordImpl.serialVersionUID = -115305878534728396L [static, private] |
Automatically generated serial version ID
Definition at line 136 of file JvnCoordImpl.java.
int jvn.JvnCoordImpl.currID = -1 [private] |
Current object ID, -1 means an error.
Definition at line 144 of file JvnCoordImpl.java.
Referenced by jvn.JvnCoordImpl.JvnCoordImpl(), and jvn.JvnCoordImpl.jvnGetObjectId().
JvnCoordImpl jvn.JvnCoordImpl.jo = null [static, private] |
A Javanaise Coordinator is managed as a singleton
Definition at line 149 of file JvnCoordImpl.java.
Referenced by jvn.JvnCoordImpl.jvnRegisterObject(), and jvn.JvnCoordImpl.main().
HashMap<String,JvnServers> jvn.JvnCoordImpl.jvnServersOfNames = new HashMap<String, JvnServers>() [private] |
Map that remembers which server(s) hold a copy of an object identified with a given name.
Definition at line 155 of file JvnCoordImpl.java.
Referenced by jvn.JvnCoordImpl.jvnLookupObject(), and jvn.JvnCoordImpl.jvnRegisterObject().
HashMap<Integer,JvnServers> jvn.JvnCoordImpl.jvnServersOfIdentifiers = new HashMap<Integer, JvnServers>() [private] |
Map that remembers which server(s) hold a copy of an object identified with a given object ID.
Definition at line 163 of file JvnCoordImpl.java.
Referenced by jvn.JvnCoordImpl.jvnLockRead(), jvn.JvnCoordImpl.jvnLockWrite(), jvn.JvnCoordImpl.jvnLookupObject(), jvn.JvnCoordImpl.jvnRegisterObject(), and jvn.JvnCoordImpl.jvnServerTerminated().