1 46 package org.mr.kernel.world; 47 48 import java.io.File ; 49 import java.io.FileWriter ; 50 import java.io.IOException ; 51 import java.io.Writer ; 52 import java.util.HashMap ; 53 import java.util.HashSet ; 54 import java.util.List ; 55 import java.util.Set ; 56 57 import org.apache.commons.logging.LogFactory; 58 import org.mr.api.jms.MantaConnection; 59 import org.mr.core.net.TransportInfo; 60 import org.mr.kernel.services.MantaService; 61 import org.mr.kernel.services.ServiceFactory; 62 63 74 public class WorldModeler implements WorldModelerMBean{ 75 76 private String myAgentName; 77 78 82 private Set myConsumedServices = new HashSet (); 83 87 private Set myProducedServices = new HashSet (); 88 89 93 private Set myCoordinatedServices = new HashSet (); 94 97 private String defaultDomain; 98 101 private HashMap domains =new HashMap (); 102 103 private static WorldModeler instance = new WorldModeler(); 104 107 private WorldModelerNetListenerMultiplexer netListener = null; 108 111 private WorldModelerLogicListener logicListener = null; 112 115 private long version =0; 116 117 120 public boolean dynamicServiceCreation ; 121 122 126 private WorldModeler() { 127 netListener = new WorldModelerNetListenerMultiplexer(); 128 } 129 130 134 public static WorldModeler getInstance(){ 135 return instance; 136 } 137 138 139 140 141 145 152 public void addAgent(String domain , String agentName , List transportInfo){ 153 154 createDomainIfNeeded(domain).addAgent(agentName).addAll(transportInfo); 155 if(netListener!= null) 156 netListener.handleAgentsTransportsAdded(agentName ,transportInfo); 157 158 } 159 160 161 166 public void removeAgent(String domain, String agentName) { 167 MantaDomain dom = getDomain(domain); 168 dom.removeAgent(agentName); 169 if (netListener != null) { 170 netListener.handleAgentRemoved(agentName); 171 } 172 } 173 174 175 183 public boolean addTransportInfoToAgent(String domain, String agentName, 184 TransportInfo info) { 185 boolean retval = 186 createDomainIfNeeded(domain).addAgentIfNeeded(agentName).add(info); 187 if (netListener!= null && retval == true) { 188 netListener.handleAgentsTransportAdded(agentName , info); 189 } 190 return retval; 191 } 192 193 201 public boolean removeTransportInfoFromAgent(String domain, String agentName, 202 TransportInfo info) 203 { 204 Set transportList = getAgentTransportInfo(domain, agentName); 205 boolean retval = false; 206 if (transportList != null) { 207 retval = transportList.remove(info); 208 if (netListener!= null) 209 netListener.handleAgentsTransportRemoved(agentName , info); 210 } 211 return retval; 212 } 214 215 221 public Set getAgentTransportInfo(String domain , String agentName){ 222 MantaDomain curDomain = getDomain(domain); 223 if(curDomain != null) 224 return curDomain.getAgentTransportInfoList(agentName); 225 return null; 226 } 227 228 229 230 235 public Set getAgents(String domain){ 236 Set result = null; 237 MantaDomain dom = getDomain(domain); 238 if(dom != null) 239 result = dom.getAgents(); 240 if(result == null) 241 result = new HashSet (); 242 return result; 243 } 244 245 246 247 251 254 public Set getServices(String domain){ 255 HashSet result = new HashSet (); 256 MantaDomain dom = getDomain(domain); 257 if(dom != null){ 258 synchronized(dom){ 259 result.addAll(dom.getServices()) ; 260 } 261 } 262 return result; 263 } 264 265 266 273 public synchronized MantaService getService(String domain ,String service, byte serviceType){ 274 MantaDomain dom = getDomain(domain); 275 MantaService result = null; 276 if(dom != null){ 277 278 result = dom.getService(service); 279 if(result == null){ 280 if(dynamicServiceCreation || service.startsWith(MantaConnection.TMP_DESTINATION_PREFIX)){ 281 return createService(domain, service, serviceType); 282 }else{ 283 LogFactory.getLog("WorldModeler").warn("service "+service+" not created dynamicServiceCreation = "+dynamicServiceCreation); 284 } 285 286 } } return result; 289 } 291 298 synchronized MantaService createService(String domain ,String service, byte serviceType){ 299 MantaDomain dom = getDomain(domain); 300 MantaService result = null; 301 if(dom != null){ 302 303 result = dom.getService(service); 304 if(result == null ){ 305 result = ServiceFactory.createService(service, serviceType); 306 addService(domain, result); 307 308 } } return result; 311 } 313 319 public synchronized boolean containsService(String domain ,String service){ 320 MantaDomain dom = getDomain(domain); 321 if(dom != null){ 322 return dom.getService(service) != null; 323 } 324 return false; 325 } 326 327 332 public void addService(String domain ,MantaService service){ 333 MantaDomain dom = createDomainIfNeeded(domain); 334 MantaService ser = dom.getService(service.getServiceName()); 335 if(ser == null) 336 dom.addService(service.getServiceName() , service); 337 338 } 339 340 346 public void removeService(String domain, String serviceName) { 347 MantaDomain dom = getDomain(domain); 348 if(dom!= null){ 349 dom.removeService(serviceName); 350 } 351 352 353 354 } 355 356 public String toString(){ 360 361 return " myAgent="+myAgentName+" defaultDomain="+defaultDomain+" world map:"+domains; 362 } 363 364 365 369 372 public String getDefaultDomainName() { 373 return defaultDomain; 374 } 375 376 379 public void setDefaultDomainName(String defaultDomain) { 380 this.defaultDomain = defaultDomain; 381 } 382 383 386 public String getMyAgentName() { 387 return myAgentName; 388 } 389 390 393 public void setMyAgentName(String myAgentName) { 394 this.myAgentName = myAgentName; 395 } 396 397 401 public MantaDomain addDomain(String domainName){ 402 if(!domains.containsKey(domainName)){ 403 domains.put(domainName , new MantaDomain(this ,domainName)); 404 } 405 return (MantaDomain)domains.get(domainName); 406 } 407 408 public void removeDomain(String domainName){ 409 domains.remove(domainName); 410 } 411 412 413 414 418 public Set getDomains(){ 419 Set result = new HashSet (); 420 421 if(domains != null){ 422 result.addAll(domains.values()) ; 423 } 424 return result; 425 } 426 427 428 public final MantaDomain getDomain(String domainName){ 429 return (MantaDomain)domains.get(domainName); 430 } 431 432 private MantaDomain createDomainIfNeeded(String domainName){ 433 434 return addDomain(domainName); 435 436 } 437 438 445 446 public WorldModelerNetListenerMultiplexer getNetworkListener(){ 447 return netListener; 448 } 449 450 454 public void setLogicListener(WorldModelerLogicListener listener){ 455 logicListener = listener; 456 } 457 458 public WorldModelerLogicListener getLogicListener(){ 459 return logicListener; 460 } 461 462 463 464 465 466 467 468 469 472 public long getVersion() { 473 return version; 474 } 475 476 479 public void setVersion(long version) { 480 this.version = version; 481 } 482 483 486 public synchronized Set getMyConsumedServices() { 487 return new HashSet (myConsumedServices); 488 } 489 490 public synchronized void removeConsumedServices(MantaService service) { 491 myConsumedServices.remove(service); 492 } 493 494 498 public synchronized void addConsumedServices(MantaService service) { 499 myConsumedServices.add(service); 500 } 501 504 public synchronized Set getMyProducedServices() { 505 return new HashSet (myProducedServices); 506 } 507 508 public synchronized void removeProducedService(MantaService service) { 509 myProducedServices.remove(service); 510 } 511 512 513 public synchronized void addProducedService(MantaService service) { 514 myProducedServices.add(service); 515 } 516 517 520 public Set getMyCoordinatedServices() { 521 return new HashSet (myCoordinatedServices); 522 } 523 524 public synchronized void removeCoordinatedService(MantaService service) { 525 myCoordinatedServices.remove(service); 526 } 527 528 529 public synchronized void addCoordinatedService(MantaService service) { 530 myCoordinatedServices.add(service); 531 } 532 533 537 public void save(Writer writer) throws IOException { 538 String worldXML = WorldModelerXMLUtils.fromWorldMapToXML(this); 539 writer.write(worldXML); 540 writer.flush(); 541 } 542 543 546 public void save(File file) throws IOException { 547 FileWriter writer = new FileWriter (file); 548 save(writer); 549 writer.close(); 550 } 551 552 553 protected void setDynamicServiceCreation(boolean dynamicServiceCreation) { 554 this.dynamicServiceCreation = dynamicServiceCreation; 555 } 556 557 protected void setNetListener(WorldModelerNetListenerMultiplexer netListener) { 558 this.netListener = netListener; 559 } 560 } 561 | Popular Tags |