📄 controlsocket.java
字号:
* @exception IOException If there was a stream or socket error. * @exception ClickException If there was some other error * accessing the handler (e.g., the ControlSocket returned an unknown * unknown error code, or the response could otherwise not be understood). * @see #read * @see #write * @see #getConfigElementNames * @see #getElementHandlers */ public boolean checkHandler(String elementName, String handlerName, boolean writeHandler) throws ClickException, IOException { String handler = handlerName; if (elementName != null) handler = elementName + "." + handlerName; _out.write((writeHandler ? "CHECKWRITE " : "CHECKREAD ") + handler + "\n"); _out.flush(); // make sure we read all the response lines... String response = ""; String lastLine = null; do { lastLine = _in.readLine(); if (lastLine == null) throw new IOException("ControlSocket stream closed unexpectedly"); if (lastLine.length() < 4) throw new ClickException("Bad response line from ControlSocket"); response = response + lastLine.substring(4); } while (lastLine.charAt(3) == '-'); int code = getResponseCode(lastLine); switch (getResponseCode(lastLine)) { case CODE_OK: case CODE_OK_WARN: return true; case CODE_NO_ELEMENT: case CODE_NO_HANDLER: case CODE_HANDLER_ERR: case CODE_PERMISSION: return false; case CODE_UNIMPLEMENTED: if (elementName == null) handleErrCode(code, elementName, handlerName, response); return checkHandlerWorkaround(elementName, handlerName, writeHandler); default: handleErrCode(code, elementName, handlerName, response); return false; } } private boolean checkHandlerWorkaround(String elementName, String handlerName, boolean writeHandler) throws ClickException, IOException { // If talking to an old ControlSocket, try the "handlers" handler // instead. String s = readString(elementName, "handlers"); int pos = 0; // Find handler with same name. while (true) { pos = s.indexOf(handlerName, pos); if (pos < 0) // no such handler return false; if ((pos == 0 || s.charAt(pos - 1) == '\n') && Character.isWhitespace(s.charAt(pos + handlerName.length()))) break; pos++; } // we have a matching handler: will it be read/write suitable? char wantType = (writeHandler ? 'w' : 'r'); for (pos += handlerName.length(); pos < s.length() && Character.isWhitespace(s.charAt(pos)); pos++) /* nada */; for (; pos < s.length(); pos++) { char c = s.charAt(pos); if (Character.toLowerCase(c) == wantType) return true; else if (Character.isWhitespace(c)) break; } return false; } /** * Returns the result of reading an element's handler. * * @param elementName The element name. * @param handlerName The handler name. * @return Char array containing the data. * @exception NoSuchHandlerException If there is no such read handler. * @exception NoSuchElementException If there is no such element in the current configuration. * @exception HandlerErrorException If the handler returned an error. * @exception PermissionDeniedException If the router would not let us access the handler. * @exception IOException If there was a stream or socket error. * @exception ClickException If there was some other error * accessing the handler (e.g., the ControlSocket returned an unknown * unknown error code, or the response could otherwise not be understood). * @see #checkHandler * @see #write * @see #getConfigElementNames * @see #getElementHandlers */ public char[] read(String elementName, String handlerName) throws ClickException, IOException { String handler = handlerName; if (elementName != null) handler = elementName + "." + handlerName; _out.write("READ " + handler + "\n"); _out.flush(); // make sure we read all the response lines... String response = ""; String lastLine = null; do { lastLine = _in.readLine(); if (lastLine == null) throw new IOException("ControlSocket stream closed unexpectedly"); if (lastLine.length() < 4) throw new ClickException("Bad response line from ControlSocket"); response = response + lastLine.substring(4); } while (lastLine.charAt(3) == '-'); int code = getResponseCode(lastLine); if (code != CODE_OK && code != CODE_OK_WARN) handleErrCode(code, elementName, handlerName, response); response = _in.readLine(); if (response == null) throw new IOException("ControlSocket stream closed unexpectedly"); int num_bytes = getDataLength(response); if (num_bytes < 0) throw new ClickException("Bad length returned from ControlSocket"); if (num_bytes == 0) return new char[0]; // sometimes, read will return without completely filling the // buffer (e.g. on win32 JDK) char data[] = new char[num_bytes]; int bytes_left = num_bytes; while (bytes_left > 0) { int bytes_read = _in.read(data, num_bytes - bytes_left, bytes_left); bytes_left -= bytes_read; } return data; } public String readString(String el, String handler) throws ClickException, IOException { return new String(read(el, handler)); } public String readString(HandlerInfo hinfo) throws ClickException, IOException { return new String(read(hinfo.elementName, hinfo.handlerName)); } private void handleWriteResponse(String elementName, String handlerName) throws ClickException, IOException { String response = ""; String lastLine = null; do { lastLine = _in.readLine(); if (lastLine == null) throw new IOException("ControlSocket stream closed unexpectedly"); if (lastLine.length() < 4) throw new ClickException("Bad response line from ControlSocket"); response = response + lastLine.substring(4) + "\n"; } while (lastLine.charAt(3) == '-'); int code = getResponseCode(lastLine); if (code != CODE_OK && code != CODE_OK_WARN) handleErrCode(code, elementName, handlerName, response); } /** * Writes data to an element's handler. * * @param elementName The element name. * @param handlerName The handler name. * @param data Char array containing the data. * @exception NoSuchHandlerException If there is no such write handler. * @exception NoSuchElementException If there is no such element in the current configuration. * @exception HandlerErrorException If the handler returned an error. * @exception PermissionDeniedException If the router would not let us access the handler. * @exception IOException If there was a stream or socket error. * @exception ClickException If there was some other error * accessing the handler (e.g., the ControlSocket returned an unknown * unknown error code, or the response could otherwise not be understood). * @see #checkHandler * @see #write * @see #getConfigElementNames * @see #getElementHandlers */ public void write(String elementName, String handlerName, char[] data) throws ClickException, IOException { String handler = handlerName; if (elementName != null) handler = elementName + "." + handlerName; _out.write("WRITEDATA " + handler + " " + data.length + "\n"); _out.write(data, 0, data.length); _out.flush(); handleWriteResponse(elementName, handlerName); } public void write(String elementName, String handlerName, String data) throws ClickException, IOException { String handler = handlerName; if (elementName != null) handler = elementName + "." + handlerName; _out.write("WRITEDATA " + handler + " " + data.length() + "\n"); _out.write(data); _out.flush(); handleWriteResponse(elementName, handlerName); } public void write(HandlerInfo info, char[] data) throws ClickException, IOException { write(info.elementName, info.handlerName, data); } public void write(HandlerInfo info, String data) throws ClickException, IOException { write(info.elementName, info.handlerName, data); } /** * Close the ControlSocket. */ public void close() { try { _sock.close(); } catch (IOException ex) { } } private int getResponseCode(String s) { String code_str = s.substring(0, 3); try { return Integer.parseInt(code_str); } catch (NumberFormatException ex) { return -1; } } private int getDataLength(String s) { int i; for (i = 0; i < s.length() && !Character.isDigit(s.charAt(i)); i++) ; // do it if (i == s.length()) return -1; String len_str = s.substring(i); try { return Integer.parseInt(len_str); } catch (NumberFormatException ex) { return -1; } } private void handleErrCode(int code, String elementName, String handlerName, String response) throws ClickException { String hid = handlerName; if (elementName != null) hid = elementName + "." + handlerName; switch (code) { case CODE_SYNTAX_ERR: throw new ClickException("Syntax error calling handler `" + hid + "'"); case CODE_UNIMPLEMENTED: throw new ClickException("Unimplemented ControlSocket command"); case CODE_NO_ELEMENT: throw new ClickException.NoSuchElementException(elementName); case CODE_NO_HANDLER: throw new ClickException.NoSuchHandlerException(hid); case CODE_HANDLER_ERR: throw new ClickException.HandlerErrorException(hid, response); case CODE_PERMISSION: throw new ClickException.PermissionDeniedException(hid); default: throw new ClickException("Unknown ControlSocket error code " + code); } } /* * test driver */ public static void main(String args[]) { if (args.length == 0 || args.length > 3) { System.out.println("to list router info, `java ControlSocket'"); System.out.println("to list handlers, `java ControlSocket <element>'"); System.out.println("to read, `java ControlSocket <element> <handler>'"); System.out.println("to write, `java ControlSocket <element> <handler> <data>'"); System.out.println("router info follows"); } InetAddress localhost = null; try { // localhost = InetAddress.getLocalHost(); localhost = InetAddress.getByName("bermuda.lcs.mit.edu"); } catch (UnknownHostException ex) { System.out.println("Can't get localhost address"); System.exit(-1); } try { ControlSocket cs = new ControlSocket(localhost, 7777); if (args.length == 2) { char data[] = cs.read(args[0], args[1]); System.out.println(data); } else if (args.length == 3) { cs.write(args[0], args[1], args[2].toCharArray()); } else if (args.length == 1) { // dump element handler info Vector v = cs.getElementHandlers(args[0]); for (int i = 0; i < v.size(); i++) { ControlSocket.HandlerInfo hi = (ControlSocket.HandlerInfo) v.elementAt(i); System.out.print(hi.handlerName + "\t"); if (hi.canRead) System.out.print("r"); if (hi.canWrite) System.out.print("w"); System.out.println(); } } else { // dump router info System.out.println("Click version: " + cs.getRouterVersion().trim()); System.out.print("Router classes: "); Vector v = cs.getRouterClasses(); for (int i = 0; i < v.size(); i++) System.out.print(v.elementAt(i) + " "); System.out.println(); System.out.println("Router config:"); System.out.print(cs.getRouterConfig()); System.out.print("Config element names: "); v = cs.getConfigElementNames(); for (int i = 0; i < v.size(); i++) System.out.print(v.elementAt(i) + " "); System.out.println(); System.out.print("Router packages: "); v = cs.getRouterPackages(); for (int i = 0; i < v.size(); i++) System.out.print(v.elementAt(i) + " "); System.out.println(); System.out.print("Config requirements: "); v = cs.getConfigRequirements(); for (int i = 0; i < v.size(); i++) System.out.print(v.elementAt(i) + " "); System.out.println(); } } catch (IOException ex) { System.out.println("I/O error calling ControlSocket: " + ex.getMessage()); System.exit(1); } catch (ClickException ex) { System.out.println(ex.getMessage()); System.exit(1); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -