📄 mafagentsystem_atpclient.java
字号:
out.writeInt(class_names[i].descriminator.length); out.write(class_names[i].descriminator); } // Agent out.writeInt(agent.length); out.write(agent); connection.sendRequest(); if (connection.getStatusCode() != OKAY && connection.getStatusCode() != MOVED) { System.out.println("code = " + connection.getStatusCode()); throw new MAFExtendedException(connection.getReasonPhase()); } if (content_type.equalsIgnoreCase(connection.getContentType()) == false) { throw new MAFExtendedException(connection.getReasonPhase()); } DataInput in = new DataInputStream(connection.getInputStream()); // REMIND : this data must be used to represent the // remote proxy. int content_length = connection.getContentLength(); if (content_length > 0) { byte[] content = new byte[connection.getContentLength()]; in.readFully(content); } } catch (SocketException ex) { throw new MAFExtendedException("SocketException: " + _url_address); } catch (IOException ex) { ex.printStackTrace(); throw new MAFExtendedException("IOException: " + _url_address); } finally { if (connection != null) { try { connection.close(); } catch (IOException ex) {} } } } public long receive_future_message(Name agent_name, byte[] msg, MAFAgentSystem sender) throws AgentNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { AtpConnectionImpl connection = send_message_internal(agent_name, msg, FUTURE, sender); long l = System.currentTimeMillis(); new WaitThread(sender, connection, l).start(); return l; } synchronized public void receive_future_reply(long return_id, byte[] reply) throws EntryNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { Long id = new Long(return_id); ConnectionHandler handler = (ConnectionHandler)handlers.get(id); while (handler == null) { try { wait(); } catch (InterruptedException ex) {} handler = (ConnectionHandler)handlers.get(id); } handler.sendFutureReply(reply); } /** * Messaging */ public byte[] receive_message(Name agent_name, byte[] msg) throws AgentNotFound, NotHandled, MessageEx, ClassUnknown, DeserializationFailed, MAFExtendedException { AtpConnectionImpl connection = send_message_internal(agent_name, msg, SYNC, null); try { InputStream in = connection.getInputStream(); byte type = (byte)in.read(); switch (type) { case HANDLED: int length = connection.getContentLength() - 1; byte b[] = new byte[length]; new DataInputStream(in).readFully(b); return b; case NOT_HANDLED: throw new NotHandled(); case EXCEPTION: throw MessageEx .read(new DataInputStream(connection.getInputStream())); default: throw new MAFExtendedException("Unkonown Return Type"); } } catch (IOException ex) { ex.printStackTrace(); throw new MAFExtendedException("Error in receiving reply"); } finally { try { connection.close(); } catch (IOException ex) {} ; } } public void receive_oneway_message(Name agent_name, byte[] msg) throws AgentNotFound, ClassUnknown, DeserializationFailed, MAFExtendedException { try { send_message_internal(agent_name, msg, ONEWAY, null).close(); } catch (IOException ex) { throw new MAFExtendedException("Unexpected Exception " + ex); } } byte[] receive_reply_internal(AtpConnectionImpl connection) throws IOException { try { int length = connection.getContentLength(); InputStream in = connection.getInputStream(); if (length <= 0) { length = in.available(); } // System.out.println("length = " + length); if (length > 0) { byte b[] = new byte[length]; DataInputStream din = new DataInputStream(in); din.readFully(b); return b; } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); int i; while ((i = in.read()) >= 0) { out.write(i); } return out.toByteArray(); } } finally { // Just to make sure if (connection != null) { connection.close(); } } } synchronized public void registerFutureReply(ConnectionHandler handler, long id) { handlers.put(new Long(id), handler); notify(); } public void resume_agent(Name agent_name) throws AgentNotFound, ResumeFailed, AgentIsRunning {} public byte[] retract_agent(Name agent_name) throws AgentNotFound, MAFExtendedException { try { final Name fAgentName = agent_name; return (byte[])AccessController .doPrivileged(new PrivilegedExceptionAction() { public Object run() throws AgentNotFound, MAFExtendedException { return retract_agent0(fAgentName); } }); } catch (PrivilegedActionException ex) { Exception e = ex.getException(); if (e instanceof AgentNotFound) { throw (AgentNotFound)e; } else if (e instanceof MAFExtendedException) { throw (MAFExtendedException)e; } else { ex.printStackTrace(); return null; } } } private byte[] retract_agent0(Name agent_name) throws AgentNotFound, MAFExtendedException { AtpConnectionImpl connection = null; try { connection = new AtpConnectionImpl(_url_address); connection.setRequestType(RETRACT); connection.setAgentName(agent_name); // connection.setAgentProfile(agent_profile); // connection.setCodeBase(code_base); // connection.setSender(class_sender.getAddress()); connection.connect(); connection.sendRequest(); if (connection.getStatusCode() != OKAY && connection.getStatusCode() != MOVED) { System.out.println("code = " + connection.getStatusCode()); throw new MAFExtendedException(connection.getReasonPhase()); } if (content_type.equalsIgnoreCase(connection.getContentType()) == false) { throw new MAFExtendedException(connection.getReasonPhase()); } InputStream is = connection.getInputStream(); DataInput in = new DataInputStream(is); // REMIND : this data must be used to represent the // remote proxy. int content_length = connection.getContentLength(); if (content_length <= 0) { content_length = is.available(); } if (content_length > 0) { byte[] content = new byte[connection.getContentLength()]; in.readFully(content); return content; } else { throw new AgentNotFound(agent_name.toString()); } } catch (SocketException ex) { throw new MAFExtendedException("SocketException: " + _url_address); } catch (IOException ex) { throw new MAFExtendedException("IOException: " + _url_address); } finally { if (connection != null) { try { connection.close(); } catch (IOException ex) {} } } } // // Utilities // private AtpConnectionImpl send_message_internal(Name agent_name, byte[] msg, int type, MAFAgentSystem sender) throws AgentNotFound, MAFExtendedException { try { final Name fAgentName = agent_name; final byte[] fMsg = msg; final int fType = type; final MAFAgentSystem fSender = sender; return (AtpConnectionImpl)AccessController .doPrivileged(new PrivilegedExceptionAction() { public Object run() throws AgentNotFound, MAFExtendedException { return send_message_internal0(fAgentName, fMsg, fType, fSender); } }); } catch (PrivilegedActionException ex) { Exception e = ex.getException(); if (e instanceof AgentNotFound) { throw (AgentNotFound)e; } else if (e instanceof MAFExtendedException) { throw (MAFExtendedException)e; } else { ex.printStackTrace(); return null; } } } // // Utilities // private AtpConnectionImpl send_message_internal0(Name agent_name, byte[] msg, int type, MAFAgentSystem sender) throws AgentNotFound, MAFExtendedException { AtpConnectionImpl connection = null; try { connection = new AtpConnectionImpl(_url_address); connection.setRequestType(MESSAGE); connection.setAgentName(agent_name); if (sender != null) { connection.setSender(sender.getAddress()); } // connection.setAgentProfile(agent_profile); // connection.setContentType(content_type); connection.connect(); OutputStream out = connection.getOutputStream(); out.write(type & 0xFF); // byte out.write(msg); connection.sendRequest(); if (connection.getStatusCode() != AtpConstants.OKAY) { System.out.println("code = " + connection.getStatusCode()); throw new AgentNotFound(connection.getReasonPhase()); } if (content_type.equalsIgnoreCase(connection.getContentType()) == false) { throw new MAFExtendedException(connection.getReasonPhase()); } return connection; } catch (IOException ex) { ex.printStackTrace(); throw new AgentNotFound(connection != null ? connection.getReasonPhase() : agent_name.toString()); } } public void setAddress(String name) { throw new NoSuchMethodError(); } public void suspend_agent(Name agent_name) throws AgentNotFound, SuspendFailed, AgentIsSuspended {} public void terminate_agent(Name agent_name) throws AgentNotFound, TerminateFailed {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -