1 26 27 28 package org.objectweb.mobilitools.smi; 29 30 31 import org.omg.CORBA.*; 32 import org.omg.CfMAF.*; 33 import java.lang.reflect.*; 34 import java.lang.Object ; 35 import java.io.ByteArrayInputStream ; 36 import java.io.IOException ; 37 import java.io.ObjectInputStream ; 38 39 import org.objectweb.mobilitools.smi.api.*; 40 import org.objectweb.mobilitools.smi.idl.*; 41 import org.objectweb.mobilitools.smi.lib.ClassCache; 42 43 44 50 public class MAFAgentSystem_impl extends _MAFAgentSystemComplementImplBase 51 implements MAFAgentSystemOperations, MAFAgentSystemComplementOperations 52 { 53 Agency my_agency; 54 boolean trace; 55 56 57 62 public MAFAgentSystem_impl(Agency agency) 63 { 64 this(agency, false); 65 } 66 67 68 74 public MAFAgentSystem_impl(Agency agency, boolean traceOn) 75 { 76 my_agency = agency; 77 trace = traceOn; 78 } 79 80 81 85 86 89 public org.omg.CfMAF.Name create_agent( 90 org.omg.CfMAF.Name agent_name, 91 AgentProfile agent_profile, 92 byte[] agent, 93 String place_name, 94 byte[] arguments, 95 ClassName[] class_names, 96 String code_base, 97 MAFAgentSystem class_provider) 98 throws ClassUnknown, ArgumentInvalid, DeserializationFailed, MAFExtendedException 99 { 100 if (trace) 101 { 102 System.out.println("MAFAgentSystem::create_agent()"); 103 } 104 if (agent_profile.agent_system_type != my_agency.getAgentSystemInfo().agent_system_type) 105 { 106 throw new MAFExtendedException(); 107 } 108 try 109 { 110 ObjectInputStream deserializer = new ObjectInputStream ( 111 new ByteArrayInputStream (arguments)); 112 java.lang.Object argObj = deserializer.readObject(); 113 return my_agency.createLocalAgent( 114 class_names[0].name, 115 new org.objectweb.mobilitools.smi.api.Name(agent_name), 116 agent_profile, 117 place_name, 118 code_base, 119 agent, 120 argObj, 121 class_provider).getName().getmafname(); 122 } 123 catch (IOException e) 124 { 125 throw new DeserializationFailed(); 126 } 127 catch (ClassNotFoundException e) 128 { 129 throw new ClassUnknown(); 130 } 131 catch (BadOperation ex) 132 { 133 System.err.println(ex.getMessage()); 134 switch (ex.getReason()) 135 { 136 case BadOperation.CLASSFAULT: 137 throw new ClassUnknown(); 138 case BadOperation.SERIALIZATION: 139 throw new DeserializationFailed(); 140 case BadOperation.REJECTED: 141 throw new ArgumentInvalid(); 142 default: 143 throw new MAFExtendedException(); 144 } 145 } 146 } 147 148 149 152 public byte[][] fetch_class( 153 ClassName[] class_name_list, 154 String code_base, 155 AgentProfile agent_profile) 156 throws ClassUnknown, MAFExtendedException 157 { 158 if (trace) 159 { 160 System.out.println("MAFAgentSystem::fetch_class()"); 161 } 162 byte[][] code = new byte[class_name_list.length][]; 163 ClassCache cache = ClassCache.getCache(code_base); 164 synchronized(cache) 165 { 166 for (int i=0 ; i<code.length ; ++i) 167 { 168 code[i] = (byte[])cache.get(class_name_list[i].name); 169 if (code[i] == null) 170 { 171 System.err.println("unknown class " + class_name_list[i].name + " for codebase " + code_base); 172 System.out.println(cache); 173 throw new ClassUnknown(); 174 } 175 } 176 } 177 return code; 178 } 179 180 181 184 public String find_nearby_agent_system_of_profile( 185 AgentProfile agent_profile) 186 throws EntryNotFound, NO_IMPLEMENT 187 { 188 if (trace) 189 { 190 System.out.println("MAFAgentSystem::find_nearby_agent_system_of_profile() - NOT IMPLEMENTED !"); 191 } 192 throw new NO_IMPLEMENT("Not implemented"); 193 } 194 195 196 202 public AgentStatus get_agent_status( 203 org.omg.CfMAF.Name agent_name) 204 throws AgentNotFound 205 { 206 if (trace) 207 { 208 System.out.println("MAFAgentSystem::get_agent_status()"); 209 } 210 try 211 { 212 if (my_agency.localAgentIsRunning(new org.objectweb.mobilitools.smi.api.Name(agent_name))) 213 { 214 return AgentStatus.CfMAFRunning; 215 } 216 else 217 { 218 return AgentStatus.CfMAFSuspended; 219 } 220 } 221 catch (BadOperation ex) 222 { 223 throw new AgentNotFound(); 224 } 225 } 226 227 228 231 public AgentSystemInfo get_agent_system_info() 232 { 233 if (trace) 234 { 235 System.out.println("MAFAgentSystem::get_agent_system_info()"); 236 } 237 return my_agency.getAgentSystemInfo(); 238 } 239 240 241 244 public AuthInfo get_authinfo( 245 org.omg.CfMAF.Name agent_name) 246 throws AgentNotFound, NO_IMPLEMENT 247 { 248 if (trace) 249 { 250 System.out.println("MAFAgentSystem::get_authinfo() - NOT IMPLEMENTED !"); 251 } 252 throw new NO_IMPLEMENT("Not implemented"); 253 } 254 255 256 260 public MAFFinder get_MAFFinder() 261 throws FinderNotFound 262 { 263 if (trace) 264 { 265 System.out.println("MAFAgentSystem::get_MAFFinder()"); 266 } 267 MAFFinder result = my_agency.getMAFFinder(); 268 if (result != null) 269 { 270 return result; 271 } 272 else 273 { 274 throw new FinderNotFound(); 275 } 276 } 277 278 279 282 public org.omg.CfMAF.Name[] list_all_agents() 283 { 284 if (trace) 285 { 286 System.out.println("MAFAgentSystem::list_all_agents()"); 287 } 288 return my_agency.listAgents((AgentProfile)null); 289 } 290 291 292 295 public org.omg.CfMAF.Name[] list_all_agents_of_authority( 296 byte[] authority) 297 { 298 if (trace) 299 { 300 System.out.println("MAFAgentSystem::list_all_agents_of_authority()"); 301 } 302 return my_agency.listAgents(authority); 303 } 304 305 306 309 public String [] list_all_places() 310 throws NO_IMPLEMENT 311 { 312 if (trace) 313 { 314 System.out.println("MAFAgentSystem::list_all_places() - NOT IMPLEMENTED !"); 315 } 316 throw new NO_IMPLEMENT("Not implemented"); 317 } 318 319 320 323 public void receive_agent( 324 org.omg.CfMAF.Name agent_name, 325 AgentProfile agent_profile, 326 byte[] agent, 327 String place_name, 328 ClassName[] class_names, 329 String code_base, 330 MAFAgentSystem sender) 331 throws ClassUnknown, DeserializationFailed, MAFExtendedException 332 { 333 if (trace) 334 { 335 System.out.println("MAFAgentSystem::receive_agent()"); 336 } 337 try 338 { 339 my_agency.receiveAgent( 340 class_names[0].name, 341 agent, 342 agent_name, 343 agent_profile, 344 place_name, 345 code_base, 346 sender); 347 } 348 catch (ClassUnknown e) 349 { 350 throw e; 351 } 352 catch (DeserializationFailed e) 353 { 354 throw e; 355 } 356 catch (MAFExtendedException e) 357 { 358 throw e; 359 } 360 catch (Throwable e) 361 { 362 throw new BAD_OPERATION(e.toString()); 363 } 364 } 365 366 367 370 public void resume_agent( 371 org.omg.CfMAF.Name agent_name) 372 throws AgentNotFound, ResumeFailed, AgentIsRunning 373 { 374 if (trace) 375 { 376 System.out.println("MAFAgentSystem::resume_agent()"); 377 } 378 try 379 { 380 my_agency.resumeLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name)); 381 } 382 catch (BadOperation ex) 383 { 384 switch (ex.getReason()) 385 { 386 case BadOperation.RUNNING: 387 throw new AgentIsRunning(); 388 case BadOperation.UNKNOWNAGENT: 389 throw new AgentNotFound(); 390 default: 391 throw new ResumeFailed(); 392 } 393 } 394 } 395 396 397 400 public void suspend_agent( 401 org.omg.CfMAF.Name agent_name) 402 throws AgentNotFound, SuspendFailed, AgentIsSuspended 403 { 404 if (trace) 405 { 406 System.out.println("MAFAgentSystem::suspend_agent()"); 407 } 408 try 409 { 410 my_agency.suspendLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name)); 411 } 412 catch (BadOperation ex) 413 { 414 switch (ex.getReason()) 415 { 416 case BadOperation.SUSPENDED: 417 throw new AgentIsSuspended(); 418 case BadOperation.UNKNOWNAGENT: 419 throw new AgentNotFound(); 420 default: 421 throw new SuspendFailed(); 422 } 423 } 424 } 425 426 427 430 public void terminate_agent( 431 org.omg.CfMAF.Name agent_name) 432 throws AgentNotFound, TerminateFailed 433 { 434 if (trace) 435 { 436 System.out.println("MAFAgentSystem::terminate_agent()"); 437 } 438 try 439 { 440 my_agency.terminateLocalAgent(new org.objectweb.mobilitools.smi.api.Name(agent_name)); 441 } 442 catch (BadOperation ex) 443 { 444 System.err.println(ex.getMessage()); 445 if (ex.getReason() == BadOperation.UNKNOWNAGENT) 446 { 447 throw new AgentNotFound(); 448 } 449 } 450 } 451 452 453 456 public void terminate_agent_system() 457 throws TerminateFailed 458 { 459 if (trace) 460 { 461 System.out.println("MAFAgentSystem::terminate_agent_system()"); 462 } 463 my_agency.terminate(); 464 } 465 466 467 471 472 public org.omg.CfMAF.Name[] list_agents(AgentProfile profile) 473 { 474 if (trace) 475 { 476 System.out.println("MAFAgentSystemComplement::list_agents()"); 477 } 478 return my_agency.listAgents(profile); 479 } 480 481 482 public void move_agent(org.omg.CfMAF.Name name, String destination, String place) 483 throws 484 AgentNotFound, 485 ClassUnknown, 486 DeserializationFailed, 487 AgentSystemNotFound, 488 ArgumentInvalid, 489 AgentRefusedToMove, 490 MAFExtendedException 491 { 492 if (trace) 493 { 494 System.out.println("MAFAgentSystemComplement::move_agent()"); 495 } 496 try 497 { 498 my_agency.moveLocalAgent( 499 new org.objectweb.mobilitools.smi.api.Name(name), 500 new Location(destination), 501 place); 502 } 503 catch (BadOperation e) 504 { 505 switch (e.getReason()) 506 { 507 case BadOperation.REJECTED: 508 throw new AgentRefusedToMove(); 509 case BadOperation.DESTINATION: 510 throw new AgentSystemNotFound(); 511 case BadOperation.CLASSFAULT: 512 throw new ClassUnknown(); 513 case BadOperation.UNKNOWNAGENT: 514 throw new AgentNotFound(); 515 case BadOperation.SERIALIZATION: 516 throw new DeserializationFailed(); 517 case BadOperation.INFRASTRUCTURE: 518 throw new MAFExtendedException(); 519 } 520 } 521 } 522 } 523 | Popular Tags |