示例#1
0
  public static void disconnectClient(SocketChannel sc) {
    String clientName = getNickFromSocket(sc);
    if (clientName == null) {
      Utils.printErrorAndExit("Error: it could not happen");
    }

    sendMessage(sc, MessageUtils.bye());
    clients.remove(clientName);
    Utils.tryClose(sc);

    notifyAllClients(clientName + " leave this chatroom");
    System.out.println(clientName + " leave this chatroom");
  }
  /**
   * removes this connection from the connections map the datagram channel still sticks around in
   * case it needs to be reused
   */
  static void removeConnection(RDPConnection con) {
    lock.lock();
    try {
      if (Log.loggingNet) Log.net("RDPServer.removeConnection: removing con " + con);
      con.setState(RDPConnection.CLOSED);

      DatagramChannel dc = con.getDatagramChannel();

      // first we get the set of connections attached to the given dc
      Map<ConnectionInfo, RDPConnection> dcConMap = allConMap.get(dc);
      if (dcConMap == null) {
        throw new MVRuntimeException("RDPServer.removeConnection: cannot find dc");
      }

      int localPort = con.getLocalPort();
      int remotePort = con.getRemotePort();
      InetAddress remoteAddr = con.getRemoteAddr();
      ConnectionInfo conInfo = new ConnectionInfo(remoteAddr, remotePort, localPort);
      Object rv = dcConMap.remove(conInfo);
      if (rv == null) {
        throw new MVRuntimeException("RDPServer.removeConnection: could not find the connection");
      }

      // close the datagramchannel if needed
      // conditions: no other connections on this datagramchannel
      // no socket listening on this datagramchannel
      if (dcConMap.isEmpty()) {
        Log.net("RDPServer.removeConnection: no other connections for this datagramchannel (port)");
        // there are no more connections on this datagram channel
        // check if there is a serversocket listening
        if (getRDPSocket(dc) == null) {
          Log.net("RDPServer.removeConnection: no socket listening on this port - closing");
          // no socket either, close the datagramchannel
          dc.socket().close();
          channelMap.remove(localPort);
          Log.net("RDPServer.removeConnection: closed and removed datagramchannel/socket");
        } else {
          Log.net("RDPServer.removeConnection: there is a socket listening on this port");
        }
      } else {
        Log.net("RDPServer.removeConnection: there are other connections on this port");
      }
    } finally {
      lock.unlock();
    }
  }
示例#3
0
  public static void kill(StringTokenizer st) {
    try {
      if (st.hasMoreTokens()) {
        String clientName = st.nextToken();

        if (clients.containsKey(clientName)) {
          SocketChannel clientChannel = clients.get(clientName);
          clients.remove(clientName);
          sendMessage(clientChannel, MessageUtils.bye());
          notifyAllClients(clientName + " leave this chatroom");
          System.out.println(clientName + " leave this chatroom");
          Utils.tryClose(clientChannel);
        } else {
          System.err.println("kill: no such client");
        }
      } else {
        System.err.println("kill: no client name");
      }
    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }
 public void deregisterVerbHandlers(String type) {
   verbHandlers_.remove(type);
 }