1 46 50 package org.mr.kernel.world; 51 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.HashMap ; 55 import java.util.HashSet ; 56 import java.util.Set ; 57 58 import org.mr.kernel.services.MantaService; 59 60 67 public class MantaDomain{ 68 WorldModeler world = null; 69 String domainName; 70 String desc = ""; 71 72 public MantaDomain(WorldModeler myworld , String domainName){ 73 world = myworld; 74 this.domainName = domainName; 75 } 76 private HashMap agentToTransportInfo = new HashMap (); 77 private HashMap serviceNameToService = new HashMap (); 78 79 80 83 84 public Set getAgentTransportInfoList(String agentName){ 85 return (Set )agentToTransportInfo.get(agentName); 86 } 87 88 89 protected Set addAgent(String agentName){ 90 Set result = new HashSet (); 91 agentToTransportInfo.put(agentName ,result); 92 if(world.getNetworkListener()!= null){ 94 world.getNetworkListener().handleAgentAdded(agentName); 95 } 96 97 return result; 98 } 99 100 protected void removeAgent(String agentName){ 101 agentToTransportInfo.remove(agentName); 102 if(world.getNetworkListener()!= null){ 103 world.getNetworkListener().handleAgentRemoved(agentName); 104 } 105 } 106 107 protected Set addAgentIfNeeded(String agentName){ 108 Set result =null; 109 result = getAgentTransportInfoList(agentName); 110 if(result == null){ 111 result =addAgent(agentName); 112 } 113 114 return result; 115 } 117 121 public Set getAgents(){ 122 return agentToTransportInfo.keySet(); 123 } 124 127 public synchronized Collection getServices(){ 128 return serviceNameToService.values(); 129 } 130 131 132 135 136 protected synchronized void addService(String serviceName ,MantaService service){ 137 serviceNameToService.put(serviceName , service); 138 if(world.getLogicListener() != null) 139 world.getLogicListener().handleServiceUp(serviceName,service.getServiceType(),domainName); 140 } 141 142 protected synchronized void removeService(String serviceName){ 143 MantaService service=(MantaService) serviceNameToService.remove(serviceName); 144 if(world.getLogicListener() != null) 145 world.getLogicListener().handleServiceDown(serviceName,service.getServiceType(),domainName); 146 } 147 148 protected MantaService getService(String serviceName){ 149 MantaService result = (MantaService)serviceNameToService.get(serviceName); 150 return result; 151 } 152 153 154 155 156 public String toString(){ 157 StringBuffer buff = new StringBuffer (); 158 buff.append("domain :"); 159 buff.append("net map :"); 160 buff.append(agentToTransportInfo); 161 162 buff.append("logical map :"); 163 buff.append(serviceNameToService); 164 165 166 167 return buff.toString(); 168 } 169 170 171 174 public String getDomainName() { 175 return domainName; 176 177 } 178 182 protected void clear(){ 183 184 ArrayList toBeRemoved = new ArrayList (serviceNameToService.keySet()); 185 for (int i = 0; i < toBeRemoved.size(); i++) { 186 removeService((String )toBeRemoved.get(i)); 187 } 188 serviceNameToService.clear(); 189 190 toBeRemoved = new ArrayList (agentToTransportInfo.keySet()); 191 for (int i = 0; i < toBeRemoved.size(); i++) { 192 removeAgent((String )toBeRemoved.get(i)); 193 } 194 agentToTransportInfo.clear(); 195 196 } 197 198 public boolean equals(Object other){ 199 if(other == this ) 200 return true; 201 if(!(other instanceof MantaDomain)) 202 return false; 203 return(((MantaDomain)other).getDomainName().equals(this.getDomainName())); 204 } 205 206 209 public int hashCode(){ 210 return this.getDomainName().hashCode(); 211 } 212 213 217 public String getDesc() { 218 return desc; 219 } 220 221 225 public void setDesc(String desc) { 226 this.desc = desc; 227 } 228 229 } 231 232 233 234 235 236 237 | Popular Tags |