public void exceptionCaught(IoSession session, Throwable cause) throws Exception { System.out.println(cause.getMessage()); CmdListener cmdListener = (CmdListener) session.getAttribute("CmdListener"); cmdListener.setClose(); session.close(true); cmdListener.onException(cause); }
public void sessionIdle(IoSession session, IdleStatus status) throws Exception { CmdListener cmdListener = (CmdListener) session.getAttribute("CmdListener"); cmdListener.setClose(); session.close(true); if (status.equals(IdleStatus.WRITER_IDLE)) { cmdListener.onException(new Exception("发送超时")); } else if (status == IdleStatus.READER_IDLE) { cmdListener.onException(new Exception("接收超时")); } else { cmdListener.onException(new Exception("超时:" + status)); } }
public void messageSent(IoSession session, Object message) throws Exception { CmdListener cmdListener = (CmdListener) session.getAttribute("CmdListener"); session.getConfig().setIdleTime(IdleStatus.WRITER_IDLE, 0); session.getConfig().setIdleTime(IdleStatus.READER_IDLE, cmdListener.getReadTimeout()); }
public void sessionClosed(IoSession session) throws Exception { CmdListener cmdListener = (CmdListener) session.getAttribute("CmdListener"); if (!cmdListener.isClose()) { cmdListener.onException(new Exception("通讯未完成")); } }
public static void main(String args[]) { CmdListener dlCmdListener = new CmdListener() { public CmdRequest cmdRequest; boolean closeFlag = false; public int getWriteTimeout() { return 10; }; public int getReadTimeout() { return 30; }; public boolean isClose() { return closeFlag; }; public void setClose() { closeFlag = true; }; public void onException(Throwable throwable) { System.err.println("出现异常"); throwable.printStackTrace(); } public void onResponse(CmdResponse resp) { com.smartcoms.web.action.version.msg.GeneralResponse gr = (com.smartcoms.web.action.version.msg.GeneralResponse) resp; System.out.println("respcode=" + gr.getRespcode()); System.out.println("respmsg=" + gr.getRespmsg()); } public void setCmdResuest(CmdRequest cmdRequest) { this.cmdRequest = cmdRequest; } public CmdRequest getCmdResuest() { return cmdRequest; } }; com.smartcoms.web.action.version.msg.DlRequest req = new com.smartcoms.web.action.version.msg.DlRequest(); req.getMessageHead().setAtmno("ATM_0002"); req.setFlag(false); req.setOffset(2048); req.setLength(47); req.setData(new byte[47]); req.setVersion("V050206"); dlCmdListener.setCmdResuest(req); CmdClient cmdClient = new CmdClient(1000000L); cmdClient.connect("127.0.0.1", 6789, dlCmdListener); System.out.println("已经发起连接,准备发送下载请求"); final com.smartcoms.web.action.version.msg.UpRequest req2 = new com.smartcoms.web.action.version.msg.UpRequest(); req2.getMessageHead().setAtmno("ATM_0003"); req2.setOffset(4096); req2.setName("Dev_Log.20090504 "); req2.setLength(33); CmdListener upCmdListener = new CmdListener() { public CmdRequest cmdRequest = req2; boolean closeFlag = false; public int getWriteTimeout() { return 10; }; public int getReadTimeout() { return 30; }; public boolean isClose() { return closeFlag; }; public void setClose() { closeFlag = true; }; public void onException(Throwable throwable) { System.err.println("出现异常"); throwable.printStackTrace(); } public void onResponse(CmdResponse resp) { com.smartcoms.web.action.version.msg.GeneralResponse gr = (com.smartcoms.web.action.version.msg.GeneralResponse) resp; System.out.println("respcode=" + gr.getRespcode()); System.out.println("respmsg=" + gr.getRespmsg()); } public void setCmdResuest(CmdRequest cmdRequest) { this.cmdRequest = cmdRequest; } public CmdRequest getCmdResuest() { return cmdRequest; } }; cmdClient.connect("127.0.0.1", 6789, upCmdListener); System.out.println("已经发起连接,准备发送上送请求"); }
public void messageReceived(IoSession session, Object message) throws Exception { CmdListener cmdListener = (CmdListener) session.getAttribute("CmdListener"); cmdListener.setClose(); session.close(true); cmdListener.onResponse((CmdResponse) message); }