public static void shutdown() {
    logger_.info("Shutting down ...");
    synchronized (MessagingService.class) {
      /* Stop listening on any socket */
      for (SelectionKey skey : listenSockets_.values()) {
        SelectorManager.getSelectorManager().cancel(skey);
      }
      listenSockets_.clear();

      /* Shutdown the threads in the EventQueue's */
      messageDeserializationExecutor_.shutdownNow();
      messageSerializerExecutor_.shutdownNow();
      messageDeserializerExecutor_.shutdownNow();
      streamExecutor_.shutdownNow();

      /* shut down the cachetables */
      taskCompletionMap_.shutdown();
      callbackMap_.shutdown();

      /* Interrupt the selector manager thread */
      SelectorManager.getSelectorManager().interrupt();

      poolTable_.clear();
      verbHandlers_.clear();
      bShutdown_ = true;
    }
    logger_.debug("Shutdown invocation complete.");
  }
  private synchronized String allocName(String type, String namePattern) {
    Map<String, Integer> patterns = nameTypes.get(type);
    if (patterns == null) {
      patterns = new HashMap<String, Integer>();
      nameTypes.put(type, patterns);
    }

    Integer id = patterns.get(namePattern);
    if (id == null) id = 0;

    id++;
    patterns.put(namePattern, id);
    String agentName = namePattern.replaceFirst("#", id.toString());
    if (agentName.equals(namePattern))
      Log.warn("AllocName: missing '#' in name pattern '" + namePattern + "'");
    else
      Log.debug(
          "AllocName: for type="
              + type
              + " assigned '"
              + agentName
              + "' from pattern '"
              + namePattern
              + "'");
    return agentName;
  }
Example #3
0
  /**
   * Retrieves information about the set of alerts that this generator may produce. The map returned
   * should be between the notification type for a particular notification and the human-readable
   * description for that notification. This alert generator must not generate any alerts with types
   * that are not contained in this list.
   *
   * @return Information about the set of alerts that this generator may produce.
   */
  @Override
  public Map<String, String> getAlerts() {
    Map<String, String> alerts = new LinkedHashMap<>();

    alerts.put(
        ALERT_TYPE_LDAP_CONNECTION_HANDLER_CONSECUTIVE_FAILURES,
        ALERT_DESCRIPTION_LDAP_CONNECTION_HANDLER_CONSECUTIVE_FAILURES);
    alerts.put(
        ALERT_TYPE_LDAP_CONNECTION_HANDLER_UNCAUGHT_ERROR,
        ALERT_DESCRIPTION_LDAP_CONNECTION_HANDLER_UNCAUGHT_ERROR);

    return alerts;
  }
 static Set<RDPConnection> getAllConnections() {
   lock.lock();
   try {
     Set<RDPConnection> allCon = new HashSet<RDPConnection>();
     Iterator<Map<ConnectionInfo, RDPConnection>> iter = allConMap.values().iterator();
     while (iter.hasNext()) {
       Map<ConnectionInfo, RDPConnection> dcMap = iter.next();
       allCon.addAll(dcMap.values());
     }
     return allCon;
   } finally {
     lock.unlock();
   }
 }
  /**
   * returns the RDPConnection that is registered for the given datagram channel and is connected to
   * the host/port in ConnectionInfo returns null if there is no matching registered rdpconnection
   */
  static RDPConnection getConnection(DatagramChannel dc, ConnectionInfo conInfo) {
    lock.lock();
    try {
      Map<ConnectionInfo, RDPConnection> dcConMap = allConMap.get(dc);

      if (dcConMap == null) {
        // there isnt even a datagram associated
        if (Log.loggingNet) Log.net("RDPServer.getConnection: could not find datagram");
        return null;
      }
      return dcConMap.get(conInfo);
    } finally {
      lock.unlock();
    }
  }
  /**
   * Helper function to get value from map.
   *
   * @param map Map to take value from.
   * @param key Key to search in map.
   * @param ifNull Default value if {@code null} was returned by map.
   * @param <K> Key type.
   * @param <V> Value type.
   * @return Value from map or default value if map return {@code null}.
   */
  public static <K, V> V getOrElse(Map<K, V> map, K key, V ifNull) {
    assert map != null;

    V res = map.get(key);

    return res != null ? res : ifNull;
  }
  protected MessagingService() {
    for (ReservedVerbs_ verbs : ReservedVerbs_.values()) {
      reservedVerbs_.put(verbs.toString(), verbs.toString());
    }
    verbHandlers_ = new HashMap<String, IVerbHandler>();
    endPoints_ = new HashSet<EndPoint>();
    /*
     * Leave callbacks in the cachetable long enough that any related messages will arrive
     * before the callback is evicted from the table. The concurrency level is set at 128
     * which is the sum of the threads in the pool that adds shit into the table and the
     * pool that retrives the callback from here.
     */
    int maxSize = MessagingConfig.getMessagingThreadCount();
    callbackMap_ = new Cachetable<String, IAsyncCallback>(2 * DatabaseDescriptor.getRpcTimeout());
    taskCompletionMap_ =
        new Cachetable<String, IAsyncResult>(2 * DatabaseDescriptor.getRpcTimeout());

    messageDeserializationExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGING-SERVICE-POOL"));

    messageSerializerExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-SERIALIZER-POOL"));

    messageDeserializerExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-DESERIALIZER-POOL"));

    streamExecutor_ =
        new DebuggableThreadPoolExecutor(
            1,
            1,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-STREAMING-POOL"));

    protocol_ = hash(HashingSchemes.MD5, "FB-MESSAGING".getBytes());
    /* register the response verb handler */
    registerVerbHandlers(MessagingService.responseVerbHandler_, new ResponseVerbHandler());
    /* register stage for response */
    StageManager.registerStage(
        MessagingService.responseStage_, new MultiThreadedStage("RESPONSE-STAGE", maxSize));
  }
  /**
   * 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();
    }
  }
Example #9
0
 public void setAttribute(String name, Object value) {
   if (name == null) {
     throw new NullPointerException("null name parameter");
   }
   if (attributes == null) {
     attributes = getHttpContext().getAttributes();
   }
   attributes.put(name, value);
 }
Example #10
0
 protected void registeredClient(SocketChannel sc) throws IOException {
   ClientInfo ci = new ClientInfo();
   ci.channel = sc;
   ci.outBuf.clear();
   ci.outBuf.put(TypeServerConstants.WELCOME);
   ci.outBuf.flip();
   allClients.put(sc, ci);
   send(sc, ci);
 }
Example #11
0
 protected void handleClient(SelectionKey key) throws IOException {
   SocketChannel sc = (SocketChannel) key.channel();
   ClientInfo ci = (ClientInfo) allClients.get(sc);
   if (ci == null) throw new IllegalStateException("Unknown client");
   if (key.isWritable()) send(sc, ci);
   if (key.isReadable())
     // 从一个客户端读进所有的可用数据
     recv(sc, ci);
 }
Example #12
0
 public Object getAttribute(String name) {
   if (name == null) {
     throw new NullPointerException("null name parameter");
   }
   if (attributes == null) {
     attributes = getHttpContext().getAttributes();
   }
   return attributes.get(name);
 }
Example #13
0
  public static String getNickFromSocket(SocketChannel sc) {
    for (Entry<String, SocketChannel> client : clients.entrySet()) {
      if (client.getValue().equals(sc)) {
        return client.getKey();
      }
    }

    return null;
  }
Example #14
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());
    }
  }
Example #15
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");
  }
  public void deregisterAllVerbHandlers(EndPoint localEndPoint) {
    Iterator keys = verbHandlers_.keySet().iterator();
    String key = null;

    /*
     * endpoint specific verbhandlers can be distinguished because
     * their key's contain the name of the endpoint.
     */
    while (keys.hasNext()) {
      key = (String) keys.next();
      if (key.contains(localEndPoint.toString())) keys.remove();
    }
  }
  /** the conn data should already be set (remote addr, etc) */
  static void registerConnection(RDPConnection con, DatagramChannel dc) {
    lock.lock();
    try {
      if (Log.loggingNet) Log.net("RDPServer.registerConnection: registering con " + con);

      // first we get the set of connections attached to the given dc
      Map<ConnectionInfo, RDPConnection> dcConMap = allConMap.get(dc);
      if (dcConMap == null) {
        dcConMap = new HashMap<ConnectionInfo, RDPConnection>();
      }

      // add this connection to the map
      int localPort = con.getLocalPort();
      int remotePort = con.getRemotePort();
      InetAddress remoteAddr = con.getRemoteAddr();
      ConnectionInfo conInfo = new ConnectionInfo(remoteAddr, remotePort, localPort);
      dcConMap.put(conInfo, con);
      allConMap.put(dc, dcConMap);
    } finally {
      lock.unlock();
    }
  }
 public static TcpConnectionManager getConnectionPool(EndPoint from, EndPoint to) {
   String key = from + ":" + to;
   TcpConnectionManager cp = poolTable_.get(key);
   if (cp == null) {
     lock_.lock();
     try {
       cp = poolTable_.get(key);
       if (cp == null) {
         cp =
             new TcpConnectionManager(
                 MessagingConfig.getConnectionPoolInitialSize(),
                 MessagingConfig.getConnectionPoolGrowthFactor(),
                 MessagingConfig.getConnectionPoolMaxSize(),
                 from,
                 to);
         poolTable_.put(key, cp);
       }
     } finally {
       lock_.unlock();
     }
   }
   return cp;
 }
Example #19
0
  public static void stop() {
    try {
      if (port[0] != -1) {

        for (Entry<String, SocketChannel> client : clients.entrySet()) {
          if (client.getValue().isConnected()) {
            sendMessage(client.getValue(), MessageUtils.bye());
            Utils.tryClose(client.getValue());
          }
        }

        Utils.tryClose(serverSocket);
        clients.clear();
        port[0] = -1;

      } else {
        System.err.println("stop: already stopped");
      }

    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }
 public static ConnectionStatistics[] getPoolStatistics() {
   Set<ConnectionStatistics> stats = new HashSet<ConnectionStatistics>();
   Iterator<TcpConnectionManager> it = poolTable_.values().iterator();
   while (it.hasNext()) {
     TcpConnectionManager cp = it.next();
     ConnectionStatistics cs =
         new ConnectionStatistics(
             cp.getLocalEndPoint(),
             cp.getRemoteEndPoint(),
             cp.getPoolSize(),
             cp.getConnectionsInUse());
     stats.add(cs);
   }
   return stats.toArray(new ConnectionStatistics[0]);
 }
  public void listen(EndPoint localEp, boolean isHttp) throws IOException {
    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    ServerSocket ss = serverChannel.socket();
    ss.bind(localEp.getInetAddress());
    serverChannel.configureBlocking(false);

    SelectionKeyHandler handler = null;
    if (isHttp) {
      handler = new HttpConnectionHandler();
    } else {
      handler = new TcpConnectionHandler(localEp);
    }

    SelectionKey key =
        SelectorManager.getSelectorManager()
            .register(serverChannel, handler, SelectionKey.OP_ACCEPT);
    endPoints_.add(localEp);
    listenSockets_.put(localEp, key);
  }
  /**
   * Sets configuration properties from the map.
   *
   * @param endpointCfg Map of properties.
   * @throws IgniteCheckedException If invalid property name or value.
   */
  public void setupConfiguration(Map<String, String> endpointCfg) throws IgniteCheckedException {
    for (Map.Entry<String, String> e : endpointCfg.entrySet()) {
      try {
        switch (e.getKey()) {
          case "type":
          case "host":
          case "management":
            // Ignore these properties
            break;

          case "port":
            setPort(Integer.parseInt(e.getValue()));
            break;

          case "size":
            setSize(Integer.parseInt(e.getValue()));
            break;

          case "tokenDirectoryPath":
            setTokenDirectoryPath(e.getValue());
            break;

          default:
            throw new IgniteCheckedException(
                "Invalid property '" + e.getKey() + "' of " + getClass().getSimpleName());
        }
      } catch (Throwable t) {
        if (t instanceof IgniteCheckedException || t instanceof Error) throw t;

        throw new IgniteCheckedException(
            "Invalid value '"
                + e.getValue()
                + "' of the property '"
                + e.getKey()
                + "' in "
                + getClass().getSimpleName(),
            t);
      }
    }
  }
 public IVerbHandler getVerbHandler(String type) {
   IVerbHandler handler = (IVerbHandler) verbHandlers_.get(type);
   return handler;
 }
 public void deregisterVerbHandlers(String type) {
   verbHandlers_.remove(type);
 }
Example #25
0
 public static void notifyAllClients(String message) {
   for (Entry<String, SocketChannel> client : clients.entrySet()) {
     sendMessage(client.getValue(), MessageUtils.message("<server>", message));
   }
 }
 private void checkForReservedVerb(String type) {
   if (reservedVerbs_.get(type) != null && verbHandlers_.get(type) != null) {
     throw new IllegalArgumentException(type + " is a reserved verb handler. Scram!");
   }
 }
 public void registerVerbHandlers(String type, IVerbHandler verbHandler) {
   checkForReservedVerb(type);
   verbHandlers_.put(type, verbHandler);
 }
Example #28
0
  public static void checkClients(List<SocketChannel> anonymousClients) {
    try {

      if (selector.selectNow() == 0) {
        return;
      }

      Set<SelectionKey> keys = selector.selectedKeys();
      for (SelectionKey key : keys) {

        if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
          SocketChannel clientSocket = serverSocket.accept();
          if (clientSocket == null) {
            System.err.println("Error: can not accept");
            continue;
          }
          anonymousClients.add(clientSocket);
          clientSocket.configureBlocking(false);
          clientSocket.register(selector, SelectionKey.OP_READ);
        } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
          SocketChannel clientSocket = (SocketChannel) key.channel();
          ByteBuffer bytes = ByteBuffer.allocate(4096);

          if (clientSocket.read(bytes) != -1) {
            byte[] message = bytes.array();
            switch (message[0]) {
              case 1:
                {
                  String nick = MessageUtils.parseMessage(message).first;
                  if (!checkMessage(nick, "", clientSocket)) {
                    continue;
                  }

                  if (clients.containsKey(nick) || nick.length() > 32) {
                    if (nick.length() > 32) {
                      sendMessage(
                          clientSocket, MessageUtils.error("<server>: your nick is too long"));
                    } else {
                      sendMessage(
                          clientSocket,
                          MessageUtils.error("<server>: another user is using this nick"));
                    }
                    sendMessage(clientSocket, MessageUtils.bye());
                    anonymousClients.remove(clientSocket);
                    Utils.tryClose(clientSocket);
                  } else {
                    System.out.println(nick + " enter this chatroom");
                    notifyAllClients(nick + " enter this chatroom");

                    sendMessage(
                        clientSocket,
                        MessageUtils.message("<server>", "Wellcome, your nick is " + nick));
                    anonymousClients.remove(clientSocket);
                    clients.put(nick, clientSocket);
                  }
                  break;
                }

              case 2:
                {
                  String nick = getNickFromSocket(clientSocket);
                  if (nick == null) {
                    Utils.printErrorAndExit("Error: it could not happen");
                  }

                  Utils.Pair<String, String> pair = MessageUtils.parseMessage(message);
                  if (!checkMessage(pair.first, pair.second, clientSocket)) {
                    continue;
                  }

                  if (!pair.first.equals(nick)) {
                    System.out.println("Cheater: " + nick);
                    sendMessage(clientSocket, MessageUtils.message("<server>", "do not cheat"));
                  }

                  for (Entry<String, SocketChannel> client : clients.entrySet()) {
                    if (!clientSocket.equals(client.getValue())) {
                      sendMessage(client.getValue(), MessageUtils.message(nick, pair.second));
                    }
                  }
                  break;
                }

              case 3:
                disconnectClient(clientSocket);
                break;

              case 127:
                {
                  String nick = getNickFromSocket(clientSocket);

                  Utils.Pair<String, String> pair = MessageUtils.parseMessage(message);
                  if (!checkMessage(pair.first, pair.second, clientSocket)) {
                    continue;
                  }

                  if (!pair.first.equals(nick)) {
                    System.out.println("Cheater: " + nick);
                    sendMessage(clientSocket, MessageUtils.message("<server>", "do not cheat"));
                  }
                  System.out.println(nick + ": " + pair.second);
                  break;
                }

              default:
                System.out.println("Bad message from " + getNickFromSocket(clientSocket));
                sendMessage(clientSocket, MessageUtils.message("<server>", "bad message"));
                break;
            }
          } else {
            disconnectClient(clientSocket);
          }
        }
      }
      keys.clear();
    } catch (Exception expt) {
      expt.printStackTrace();
      Utils.printErrorAndExit(expt.getMessage());
    }
  }
Example #29
0
  public static void readCommand() {
    try {

      if (!reader.ready()) {
        return;
      }

      StringTokenizer tokens = new StringTokenizer(reader.readLine());

      if (tokens.hasMoreTokens()) {

        switch (tokens.nextToken()) {
          case "/listen":
            listen(tokens);
            break;

          case "/stop":
            stop();
            break;

          case "/list":
            if (port[0] == -1) {
              System.err.println("Error: start listening before");
            }

            for (Entry<String, SocketChannel> client : clients.entrySet()) {
              System.out.println(client.getKey());
            }

            if (clients.entrySet().isEmpty()) {
              System.out.println("list: no clients online");
            }
            break;

          case "/send":
            if (tokens.hasMoreTokens()) {
              String name = tokens.nextToken();
              if (clients.containsKey(name)) {
                if (tokens.hasMoreTokens()) {
                  sendMessage(
                      clients.get(name),
                      MessageUtils.message("<server>", getMessageFromTokens(tokens)));
                } else {
                  System.err.println("send: message is empty");
                }
              } else {
                System.err.println("send: no such client");
              }
            } else {
              System.err.println("send: no client name");
            }
            break;

          case "/sendall":
            if (tokens.hasMoreTokens()) {
              notifyAllClients(getMessageFromTokens(tokens));
            } else {
              System.err.println("sendall: message is empty");
            }
            break;

          case "/kill":
            kill(tokens);
            break;

          case "/exit":
            if (port[0] != -1) {
              stop();
            }
            Utils.tryClose(selector);
            System.exit(0);
            break;

          default:
            System.err.print("Error: unknown command");
            break;
        }

      } else {
        System.err.println("Error: empty input");
      }

    } catch (Exception expt) {
      Utils.printErrorAndExit(expt.getMessage());
    }
  }