1 22 package org.objectweb.petals.kernel.registry; 23 24 import java.net.InetAddress ; 25 import java.util.Collections ; 26 import java.util.Map ; 27 import java.util.TreeMap ; 28 29 import javax.naming.NamingException ; 30 31 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 32 import org.objectweb.fractal.api.control.LifeCycleController; 33 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 34 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 35 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 36 import org.objectweb.fractal.fraclet.annotation.Monolog; 37 import org.objectweb.petals.kernel.configuration.ConfigurationService; 38 import org.objectweb.petals.kernel.registry.msg.request.RegistryRequest; 39 import org.objectweb.petals.kernel.registry.msg.response.RegistryResponse; 40 import org.objectweb.petals.kernel.registry.thread.FullUpdateThread; 41 import org.objectweb.petals.kernel.registry.thread.SocketServerThread; 42 import org.objectweb.petals.kernel.registry.thread.TribeThread; 43 import org.objectweb.petals.util.LoggingUtil; 44 import org.objectweb.petals.util.SystemUtil; 45 import org.objectweb.tribe.channel.AbstractReliableFifoChannel; 46 import org.objectweb.tribe.channel.ReliableGroupChannelWithGms; 47 import org.objectweb.tribe.channel.tcp.TcpChannelPool; 48 import org.objectweb.tribe.common.GroupIdentifier; 49 import org.objectweb.tribe.common.IpAddress; 50 import org.objectweb.tribe.gms.GroupMembershipService; 51 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService; 52 import org.objectweb.util.monolog.api.Logger; 53 54 88 @FractalComponent 89 public class RegistryServer { 90 91 98 protected ReliableGroupChannelWithGms channel; 99 100 103 protected ConfigurationService configurationService; 104 105 108 protected Map <String , Map <String , Object >> data = Collections 109 .synchronizedMap(new TreeMap <String , Map <String , Object >>()); 110 111 114 protected UdpDiscoveryService discovery; 115 116 119 protected FullUpdateThread electMasterThread; 120 121 124 protected boolean fullyUpdated; 125 126 129 protected GroupMembershipService gms; 130 131 134 protected String groupName = SystemUtil.getJoramDomain(); 135 136 139 protected boolean isStarted; 140 141 144 protected LoggingUtil log; 145 146 149 @Monolog(name = "logger") 150 protected Logger logger; 151 152 155 protected RegistryUtil registryUtil; 156 157 160 protected SocketServerThread socketServerThread; 161 162 165 protected long startTime = System.currentTimeMillis(); 166 167 170 protected TribeThread tribeThread; 171 172 public RegistryServer() { 173 super(); 174 try { 175 InetAddress multicastAddress = InetAddress.getByName(SystemUtil 176 .getTribeMulticastAddress()); 177 final int multicastPort = 2288; 178 final IpAddress multicastIP = new IpAddress(multicastAddress, 179 multicastPort); 180 final InetAddress repplyAddress = InetAddress.getByName(SystemUtil 181 .getHost()); 182 int repplyPort = 0; 183 final IpAddress repplyIP = new IpAddress(repplyAddress, repplyPort); 184 185 discovery = new UdpDiscoveryService(multicastIP, repplyIP); 186 gms = new GroupMembershipService(repplyIP, TcpChannelPool 187 .getChannelPool(), discovery); 188 189 channel = new ReliableGroupChannelWithGms(gms); 190 191 data.put("/", new TreeMap <String , Object >()); 192 193 } catch (Exception e) { 194 } 196 registryUtil = new RegistryUtil(this); 197 } 198 199 public ReliableGroupChannelWithGms getChannel() { 200 return channel; 201 } 202 203 public ConfigurationService getConfigurationService() { 204 return configurationService; 205 } 206 207 public Map <String , Map <String , Object >> getData() { 208 return data; 209 } 210 211 public LoggingUtil getLog() { 212 return log; 213 } 214 215 public long getStartTime() { 216 return startTime; 217 } 218 219 public boolean isFullyUpdated() { 220 return fullyUpdated; 221 } 222 223 231 public Object processRequest(RegistryRequest request) 232 throws NamingException { 233 Object output = null; 236 switch (request.getType()) { 237 240 case bind: 241 registryUtil.bind(request.getArg1(), request.getArg2(), request 242 .getArg3()); 243 break; 244 247 case createSubcontext: 248 output = registryUtil.createSubcontext(request.getArg1(), request 249 .getArg2()); 250 break; 251 254 case destroySubcontext: 255 registryUtil 256 .destroySubcontext(request.getArg1(), request.getArg2()); 257 break; 258 261 case fullUpdate: 262 output = data; 263 break; 264 267 case list: 268 output = registryUtil.list(request.getArg1(), request.getArg2()); 269 break; 270 273 case listBindings: 274 output = registryUtil.listBindings(request.getArg1(), request 275 .getArg2()); 276 break; 277 280 case lookup: 281 output = registryUtil.lookup(request.getArg1(), request.getArg2()); 282 break; 283 286 case lookupLink: 287 output = registryUtil.lookupLink(request.getArg1(), request 288 .getArg2()); 289 break; 290 293 case rebind: 294 registryUtil.rebind(request.getArg1(), request.getArg2(), request 295 .getArg3()); 296 break; 297 300 case rename: 301 registryUtil.rename(request.getArg1(), request.getArg2(), request 302 .getArg3()); 303 break; 304 307 case unbind: 308 registryUtil.unbind(request.getArg1(), request.getArg2()); 309 break; 310 default: 311 break; 312 } 313 return output; 315 } 316 317 324 @SuppressWarnings ("unchecked") 325 public Object processResponse(RegistryResponse response) { 326 Object output = null; 329 switch (response.getType()) { 330 333 case fullUpdate: 334 if (!fullyUpdated) { 335 fullyUpdated = true; 336 data = (Map <String , Map <String , Object >>) response.getArg1(); 337 } 338 break; 339 342 case myStartTime: 343 if ((Long ) response.getArg1() > startTime) { 344 output = "i can be a master"; 345 } 346 break; 347 350 case exception: 351 break; 354 default: 355 break; 356 } 357 return output; 359 } 360 361 public void setData(Map <String , Map <String , Object >> data) { 362 this.data = data; 363 } 364 365 public void setFullyUpdated(boolean fullyUpdated) { 366 this.fullyUpdated = fullyUpdated; 367 } 368 369 public void setGroupName(String groupName) { 370 this.groupName = groupName; 371 } 372 373 376 @LifeCycle(on = LifeCycleType.START) 377 public void start() throws IllegalLifeCycleException { 378 log = new LoggingUtil(null); 379 log.call(); 380 configurationService = new ConfigurationService(log); 381 startServer(); 382 } 383 384 388 public void startServer() { 389 isStarted = true; 390 socketServerThread = new SocketServerThread(this); 392 tribeThread = new TribeThread(this); 393 electMasterThread = new FullUpdateThread(this); 394 try { 395 channel.join(new GroupIdentifier(groupName)); 396 Thread.sleep(1000); 398 electMasterThread.start(); 399 tribeThread.start(); 400 electMasterThread.join(); 402 socketServerThread.start(); 403 Thread.sleep(1000); 405 } catch (Exception e) { 406 log.error("Error starting Registry server", e); 407 } 408 } 409 410 413 @LifeCycle(on = LifeCycleType.STOP) 414 public void stop() throws IllegalLifeCycleException { 415 log.start(); 416 stopServer(); 417 log.end(); 418 } 419 420 424 public void stopServer() { 425 isStarted = false; 426 427 tribeThread.terminate(); 428 socketServerThread.terminate(); 429 try { 430 AbstractReliableFifoChannel abstractReliableFifoChannel = TcpChannelPool 431 .getChannelPool().getChannel( 432 channel.getLocalMembership().getAddress()); 433 TcpChannelPool.getChannelPool().removeChannelFromPool( 434 abstractReliableFifoChannel); 435 436 channel.quit(); 437 gms.stop(); 438 discovery.kill(); 439 440 discovery.join(); 441 tribeThread.join(); 442 socketServerThread.join(); 443 gms = null; 444 discovery = null; 445 channel = null; 446 } catch (Exception e) { 447 } 449 } 450 451 } 452 | Popular Tags |