public synchronized boolean removeService(int port) { Integer key = new Integer(port); int maxConn =10; final Listener listener = (Listener) services.get(key); if (listener == null) { log.println("Service " + " isn′t started on port " + port); return false; }
services.remove(key); listener.pleaseStop(); freeConn+=listener.maxConn; log.println("Close " + listener.service + " on port " + port); return true; }
public synchronized void displayStatus(PrintWriter out) { Iterator keys = services.keySet().iterator(); while (keys.hasNext()) { Integer port = (Integer) keys.next(); Listener listener = (Listener) services.get(port); out.println("服务" + listener.service + "运行" + port + " "); }
out.println("连接限制为" + maxConnections);
Iterator conns = connections.iterator(); while (conns.hasNext()) { Socket s = (Socket) conns.next(); int sport = s.getLocalPort(); Listener listen = (Listener) services.get(new Integer(sport)); String servicename = listen.service; out.println(servicename + "响应请求在" + s.getInetAddress().getHostAddress() + "的" + sport + "端口上"); }
private synchronized int getfreeConnections(int maxConn) { int num = -1; if (freeConn >= maxConn) { freeConn-=maxConn; num = freeConn; } return num; }
public synchronized int getConnections() { return currentConn; }
public synchronized int addConnections(Socket s) { connections.add(s); return currentConn++; }
public synchronized int removeConnections(Socket s) { connections.remove(s); try { s.close(); } catch (Exception e) { System.out.println(e.getMessage()); } return currentConn--; }
public synchronized int getConnections(int connections) { int num = 0; if ((num=freeConn-connections) >= 0) { freeConn = num; } else num = -1; return num; }
private synchronized int getFreeConn() { return freeConn; } }
int len = line.indexOf("Remote Console>"); line = line.substring(len+1,line.length()); String printStr; try { StringTokenizer st = new StringTokenizer(line); int count = st.countTokens(); if (!st.hasMoreElements()) continue;
String first = st.nextToken().toLowerCase(); if (first.equals("password")) { String pwd = st.nextToken(); if (pwd.equals(this.password)) { to_client.println("OK"); authorized = true; } else to_client.println("密码无效,请重试! ");
} else if (first.equals("add")) { if(!authorized) to_client.println("请登陆! "); else { count--; String servicename; int Port; boolean flag = true; if (count>0) { servicename = st.nextToken(); Port = Integer.parseInt(st.nextToken()); server.addService(loadClass(servicename1), Port); to_client.println("服务" + servicename + "已经加载 "); flag = false; } if (flag) to_client.println("系统不能启动非法服务:" + servicename); else {to_client.println("请输入服务名! ");} } } else if (first.equals("remove")) { if(!authorized) to_client.println("请登陆! "); else { count--; if (count>0) { int port = Integer.parseInt(st.nextToken()); boolean bl = server.removeService(port); if (bl) to_client.println("端口: " + port +"上的服务已经卸载 "); else to_client.println("端口: "+ port +"上无任何服务运行,卸载操作失败! "); } else to_client.println("请输入端口名! "); } } else if(first.equals("status")) { if(!authorized) to_client.println("请登陆! "); else server.displayStatus(to_client); } else if(first.equals("help")) { if(!authorized) to_client.println("请登陆! "); else printHelp(to_client); } else if(first.equals("quit")) break; else to_client.println("命令不能识别! "); } catch(Exception e) {to_client.println("系统后台出错" + e.getMessage() +" "); printHelp(to_client); } to_client.println("Remote Console>"); to_client.flush(); } to_client.flush(); authorized = false; server.removeControlFlag(); to_client.close(); from_client.close(); }