📄 mafagentsystem_agletsimpl.java
字号:
if (agletsPublicAliases != null) { for (int i = 0; i < agletsPublicAliases.length; i++) { String ali_name = agletsPublicAliases[i][0]; String ali_path = agletsPublicAliases[i][1]; if (local_file.startsWith(ali_name)) { local_file = local_file.substring(ali_name.length()); return ali_path + local_file; } } } // if (local_file.startsWith(File.separator) && local_file.length() > 1) { // local_file = local_file.substring(1); // } return agletsPublicRoot + local_file; } public Name[] list_all_agents() { return null; } public Name[] list_all_agents_of_authority(byte[] authority) { return null; } public String[] list_all_places() { AgletContext[] contexts = _runtime.getAgletContexts(); String list[] = new String[contexts.length]; for (int i = 0; i < list.length; i++) { list[i] = contexts[i].getName(); } return list; } private byte[] readData(String filename) throws IOException { final String fFilename = filename; final File fFile = new File(filename); try { return (byte[])AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws IOException { if (fFile.exists() == false) { throw new IOException("FileNotFound: " + fFile); } if (fFile.canRead() == false) { throw new IOException("FileNotReadable: " + fFile); } // -- Get length of byte code. int length = (int)fFile.length(); byte[] bytecode = new byte[length]; // -- get byte code FileInputStream in = new FileInputStream(fFilename); int offset = 0; while (length > 0) { int read = in.read(bytecode, offset, length); offset += read; length -= read; } in.close(); return bytecode; } } ); } catch (PrivilegedActionException ex) { throw (IOException)ex.getException(); } } public void receive_agent(Name agent_name, AgentProfile agent_profile, byte[] agent, String place_name, ClassName[] class_names, String code_base, MAFAgentSystem sender) throws ClassUnknown, DeserializationFailed, MAFExtendedException { checkProfile(agent_profile); final AgletContextImpl impl = (AgletContextImpl)_runtime.getAgletContext(place_name); if (impl == null) { System.out.println("Place not found " + place_name); throw new MAFExtendedException("Place not found"); } final Name an = agent_name; final ClassName[] cn = class_names; final String cb = code_base; final byte[] a = agent; final String sa = sender.getAddress(); try { AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws Exception { impl.receiveAglet(an, cn, cb, a, sa); return null; } }); } catch (PrivilegedActionException excpt) { Exception ex = excpt.getException(); if (ex instanceof ClassNotFoundException) { throw new ClassUnknown("Class Not Found:" + ex.getMessage()); } else if (ex instanceof AgletException) { throw new MAFExtendedException(""); } } } public long receive_future_message(Name agent_name, byte[] raw_msg, MAFAgentSystem message_sender) throws AgentNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { LocalAgletRef ref = getLocalAgletRef(agent_name); long return_id = System.currentTimeMillis(); try { Message message = (Message)MessageInputStream.toObject(ref.resourceManager, raw_msg); FutureReplyImpl future = new RemoteFutureReplyImpl(message_sender, ref.resourceManager, return_id); ref.sendFutureMessage(message, future); } catch (OptionalDataException ex) { throw new DeserializationFailed(toMessage(ex)); } catch (IOException ex) { throw new DeserializationFailed(toMessage(ex)); } catch (ClassNotFoundException ex) { throw new ClassUnknown(toMessage(ex)); } catch (InvalidAgletException ex) { throw new AgentNotFound(toMessage(ex)); } return return_id; } public void receive_future_reply(long return_id, byte[] reply) throws EntryNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { Long l = new Long(return_id); ResourceManager rm = MessageBroker.getResourceManager(l); if (rm == null) { throw new EntryNotFound("Invalid return id"); } FutureReplyImpl future = MessageBroker.getFutureReply(l); try { Object obj[] = (Object[])MessageInputStream.toObject(rm, reply); switch (((Integer)obj[0]).intValue()) { case 0: // HANDLED future.sendReplyIfNeeded(obj[1]); break; case 1: // MESSAGE_EXCEPTION MessageException ex = (MessageException)obj[1]; future.sendExceptionIfNeeded(ex.getException()); break; case 2: // NOT_HANDLED future.cancel((String)obj[1]); break; } } catch (OptionalDataException ex) { throw new DeserializationFailed(toMessage(ex)); } catch (IOException ex) { ex.printStackTrace(); throw new DeserializationFailed(toMessage(ex)); } catch (ClassNotFoundException ex) { throw new ClassUnknown(toMessage(ex)); } } /** * Messaging */ public byte[] receive_message(Name agent_name, byte[] raw_msg) throws AgentNotFound, NotHandled, MessageEx, ClassUnknown, DeserializationFailed, MAFExtendedException { logCategory.debug("receive_message()++"); try { // context only supports only synchronized message. AgletContextImpl cxt = getAgletContext(agent_name); // not so good! if (cxt != null) { ResourceManagerFactory rmf = cxt.getResourceManagerFactory(); ResourceManager rm = rmf.getCurrentResourceManager(); Message message = (Message)MessageInputStream.toObject(rm, raw_msg); Object ret = cxt.handleMessage(message); return MessageOutputStream.toByteArray(rm, ret); } else { LocalAgletRef ref = getLocalAgletRef(agent_name); Message message = (Message)MessageInputStream.toObject(ref.resourceManager, raw_msg); Object ret; // REMIND: ad hoc. if (message instanceof com.ibm.awb.misc.CGIMessage) { ByteArrayOutputStream o = new ByteArrayOutputStream(); message.setArg("cgi-response", o); ret = ref.sendMessage(message); return o.toByteArray(); } else if (message.sameKind("_getAgletInfo")) { ret = ref.getAgletInfo(); } else { ret = ref.sendMessage(message); } return MessageOutputStream.toByteArray(ref.resourceManager, ret); } } catch (MessageException ex) { logCategory.error(ex); throw new MessageEx(ex.getMessage(), ex.getException()); } catch (NotHandledException ex) { logCategory.error(ex); throw new NotHandled(toMessage(ex)); } catch (OptionalDataException ex) { logCategory.error(ex); throw new DeserializationFailed(toMessage(ex)); } catch (IOException ex) { logCategory.error(ex); ex.printStackTrace(); throw new DeserializationFailed(toMessage(ex)); } catch (ClassNotFoundException ex) { logCategory.error(ex); throw new ClassUnknown(toMessage(ex)); } catch (InvalidAgletException ex) { logCategory.error(ex); throw new AgentNotFound(toMessage(ex)); } } public void receive_oneway_message(Name agent_name, byte[] raw_msg) throws AgentNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { LocalAgletRef ref = getLocalAgletRef(agent_name); try { Message msg = (Message)MessageInputStream.toObject(ref.resourceManager, raw_msg); ref.sendOnewayMessage(msg); } catch (OptionalDataException ex) { logCategory.error(ex); throw new DeserializationFailed(toMessage(ex)); } catch (IOException ex) { logCategory.error(ex); throw new DeserializationFailed(toMessage(ex)); } catch (ClassNotFoundException ex) { logCategory.error(ex); throw new ClassUnknown(toMessage(ex)); } catch (InvalidAgletException ex) { logCategory.error(ex); throw new AgentNotFound(toMessage(ex)); } } public void resume_agent(Name agent_name) throws AgentNotFound, ResumeFailed, AgentIsRunning { return; } public byte[] retract_agent(Name agent_name) throws AgentNotFound, MAFExtendedException { LocalAgletRef ref = getLocalAgletRef(agent_name); if (ref == null) { throw new AgentNotFound("NotFound"); } return ref.retract(); } synchronized public void setAddress(String name) { if (_address != null) { throw new IllegalArgumentException("Address already set"); } _address = name; } public void suspend_agent(Name agent_name) throws AgentNotFound, SuspendFailed, AgentIsSuspended { return; } public void terminate_agent(Name agent_name) throws AgentNotFound, TerminateFailed { return; } static String toMessage(Exception ex) { return ex.getClass().getName() + ':' + ex.getMessage(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -