1 55 56 package org.apache.bsf.debug.clientImpl; 57 58 import java.util.*; 59 import java.net.*; 60 import java.io.*; 61 62 import org.apache.bsf.debug.*; 63 import org.apache.bsf.debug.util.*; 64 import org.apache.bsf.debug.meta.*; 65 66 public class ClientConnection extends SocketConnection implements Runnable { 67 68 int CommandIdGenerator = 1; 69 Socket fSocket; 70 Thread fThread; 71 72 private static final int SOCKET_TIMEOUT = 100; 73 74 Dispatcher m_dispatchers[]; 75 DebugManagerStub m_debugManager; 76 77 public ClientConnection(String host, int port) throws IOException { 78 int proto_result = -1; 79 80 fStubs = new ClientStubTable(this); 81 82 m_dispatchers = new Dispatcher[2]; 83 m_dispatchers[0] = new DebuggerDispatcher(this); 84 m_dispatchers[1] = new JsCallbacksDispatcher(this); 85 86 fSocket = new Socket(host, port); 87 88 fInputStream = fSocket.getInputStream(); 89 fDataInputStream = new DataInputStream(fInputStream); 90 fOutputStream = fSocket.getOutputStream(); 91 fDataOutputStream = new DataOutputStream(fOutputStream); 92 93 fDataOutputStream.writeInt(DebugConstants.BSF_DEBUG_PROTOCOL_MAJOR); 94 fDataOutputStream.writeInt(DebugConstants.BSF_DEBUG_PROTOCOL_MINOR); 95 proto_result = fDataInputStream.readInt(); 96 97 if (proto_result == DebugConstants.BSF_DEBUG_PROTOCOL_REJECT) { 98 fSocket.close(); 99 fSocket = null; 100 throw new ProtocolException("Protocol version mismatch."); 101 } 102 else { 103 fThread = new Thread (this, "Socket Listener"); 104 fThread.start(); 105 106 m_debugManager = new DebugManagerStub(this); 107 } 108 } 109 110 public DebugManagerStub getDebugManager() { 111 return m_debugManager; 112 } 113 114 public void run() { 115 listen(); 116 } 117 118 protected void dispatchInvocation(ResultCell rcell) throws Exception { 119 Exception ex; 120 switch(rcell.classId) { 121 case DebugConstants.BSF_DEBUGGER_TID: 122 m_dispatchers[0].dispatch(rcell); 123 break; 124 case DebugConstants.JS_CALLBACKS_TID: 125 m_dispatchers[1].dispatch(rcell); 126 break; 127 default: 128 throw new Error ("Can't parse the invocation!"); 129 } 130 } 131 132 public void breakConnection() { 133 try { 134 m_debugManager.sendQuitNotice(); 135 } 136 catch (Exception ex) { 137 } 139 stopListening(); 140 try { 141 fSocket.setSoLinger(false, 0); 144 fSocket.shutdownInput(); 145 fSocket.close(); 146 fSocket = null; 147 } 148 catch (IOException ioe) { 149 } 151 } 152 153 protected void wireExceptionNotify(Exception ex) { 154 155 DebugLog.stdoutPrintln("Disconnected", DebugLog.BSF_LOG_L1); 156 157 m_debugManager.disconnectNotify(ex); 158 159 super.wireExceptionNotify(ex); 160 } 161 } 162 | Popular Tags |