예제 #1
0
  /* (non-Javadoc)
   * @see flap.messaging.IMessageQueue#addMessage(flap.messaging.Message)
   */
  @Override
  public synchronized void addMessage(Message messaggio) {
    if (messaggio == null) return; // skip null messages

    // store the message into the right queue depending on its
    // priority
    logger.debug("[MessageQueue] Storing the message into the right queue");
    deliverMessageIntoTheRightQueue(messaggio);

    // get a thread to handle this message
    logger.debug("[MessageQueue] Asking a thread for processing the message");
    AgentThread thread = AgentThread.getThread();
    thread.handleMessage(this, ownerProxy.getMyOwningAgent());
  }
  @Override
  public void run() {
    try {
      proxySocket = new ServerSocket(proxyPort);

      System.out.println("Proxy server listening on port: " + proxyPort);

      while (true) {
        // wait for new clients (agents)
        Socket clientSocket = proxySocket.accept();

        // remove obsolete agent proxies
        for (int i = agentProxies.size() - 1; i >= 0; i--) {
          if (!agentProxies.get(i).isActive()) {
            agentProxies.remove(i);
          }
        }

        // create new agent proxy
        AgentProxy agentProxy = createAgentProxy(clientSocket);
        if (agentProxy != null) {
          agentProxies.add(agentProxy);
        }
      }
    } catch (IOException e) {
      System.out.println("Proxy server socket closed!");
    }

    proxySocket = null;

    // shutdown and remove all agent proxies
    for (AgentProxy proxy : agentProxies) {
      proxy.stopProxy();
    }
    agentProxies.clear();
  }
 /**
  * Factory method to create agent proxy
  *
  * @param clientSocket the socket the agent proxy works on
  * @return a new instance of agent proxy
  */
 protected AgentProxy createAgentProxy(Socket clientSocket) {
   AgentProxy agentProxy = new AgentProxy(clientSocket, ssHost, ssPort, showMessages);
   agentProxy.start(clientSocket, ssHost, ssPort, showMessages);
   return agentProxy;
 }