Example #1
0
  /**
   * This is periodically called by the JICPMediatorManager and is used by this NIOMediator to
   * evaluate the elapsed time without the need of a dedicated thread or timer.
   */
  public final void tick(long currentTime) {
    if (active && !connectionDropped) {
      // 1) If there is a blocking read operation in place check the
      // response timeout
      inpManager.checkResponseTime(currentTime);

      // 2) Evaluate the keep alive
      if (keepAliveTime > 0) {
        if ((currentTime - lastReceivedTime) > (keepAliveTime + RESPONSE_TIMEOUT)) {
          // Missing keep-alive.
          // The OUT connection is no longer valid
          if (outManager.isConnected()) {
            myLogger.log(Logger.WARNING, myID + ": Missing keep-alive");
            outManager.resetConnection();
          }
          // Check the INP connection. Since this method must return
          // asap, does it in a separated Thread
          if (inpManager.isConnected()) {
            Thread t =
                new Thread() {

                  public void run() {
                    try {
                      // JICPPacket pkt = new JICPPacket(JICPProtocol.KEEP_ALIVE_TYPE,
                      // JICPProtocol.DEFAULT_INFO, null);
                      // inpManager.dispatch(pkt, false);
                      inpManager.sendServerKeepAlive();
                      if (myLogger.isLoggable(Logger.CONFIG)) {
                        myLogger.log(Logger.CONFIG, myID + ": IC valid");
                      }
                    } catch (Exception e) {
                      // Just do nothing: the INP connection has been reset
                    }
                  }
                };
            t.start();
          }
        }
      }

      // 3) Evaluate the max disconnection time
      if (outManager.checkMaxDisconnectionTime(currentTime)) {
        myLogger.log(Logger.SEVERE, myID + ": Max disconnection time expired.");
        // Consider as if the FrontEnd has terminated spontaneously -->
        // Kill the above container (this will also kill this NIOBEDispatcher).
        kill();
      }
    }
  }
Example #2
0
  /** Handle a connection DROP_DOWN request from the FE. */
  protected void handleDropDown(Connection c, JICPPacket pkt, InetAddress addr, int port) {
    if (myLogger.isLoggable(Logger.INFO)) {
      myLogger.log(Logger.INFO, myID + ": DROP_DOWN request received.");
    }

    try {
      // If the INP connection is down or we have some postponed command
      // to flush, refuse dropping the connection
      if (inpManager.isConnected() && inpManager.isEmpty()) {
        JICPPacket rsp =
            new JICPPacket(JICPProtocol.RESPONSE_TYPE, JICPProtocol.DEFAULT_INFO, null);
        c.writePacket(rsp);

        inpManager.resetConnection();
        outManager.resetConnection();
        connectionDropped = true;
      } else {
        myLogger.log(Logger.WARNING, myID + ": DROP_DOWN request refused.");
        JICPPacket rsp = new JICPPacket(JICPProtocol.ERROR_TYPE, getReconnectInfo(), null);
        c.writePacket(rsp);
      }
    } catch (Exception e) {
      myLogger.log(Logger.WARNING, myID + ": Error writing DROP_DOWN response. " + e);
    }
  }
Example #3
0
 /**
  * Notify this NIOMediator that an error occurred on one of the Connections it was using. This
  * information is important since, unlike normal mediators, a NIOMediator typically does not read
  * packets from connections on its own (the JICPMediatorManager does that in general).
  */
 public void handleConnectionError(Connection c, Exception e) {
   myLogger.log(Level.WARNING, "connection error", e);
   if (active && peerActive) {
     // Try assuming it is the input connection
     try {
       inpManager.checkConnection(c);
       myLogger.log(Logger.WARNING, myID + ": IC Disconnection detected");
       inpManager.resetConnection();
     } catch (ICPException icpe) {
       // Then try assuming it is the output connection
       try {
         outManager.checkConnection(c);
         myLogger.log(Logger.WARNING, myID + ": OC Disconnection detected");
         outManager.resetConnection();
       } catch (ICPException icpe2) {
         // Ignore it
       }
     }
   }
 }
Example #4
0
  /**
   * Overloaded version of the handleJICPPacket() method including the <code>Connection</code> the
   * incoming JICPPacket was received from. This information is important since, unlike normal
   * mediators, a NIOMediator may not read packets from connections on its own (the
   * JICPMediatorManager does that in general).
   */
  public JICPPacket handleJICPPacket(Connection c, JICPPacket pkt, InetAddress addr, int port)
      throws ICPException {
    if (pkt.getType() == JICPProtocol.DROP_DOWN_TYPE) {
      // Note that the return packet is written inside the handleDropDown()
      // method since the connection must be closed after the response has
      // been sent back.
      handleDropDown(c, pkt, addr, port);
      return null;
    }

    checkTerminatedInfo(pkt);

    // Update keep-alive info
    lastReceivedTime = System.currentTimeMillis();

    byte type = pkt.getType();
    if (type == JICPProtocol.COMMAND_TYPE) {
      if (peerActive) {
        return outManager.handleCommand(c, pkt);
      } else {
        // The remote FrontEnd has terminated spontaneously -->
        // Kill the above container (this will also kill this NIOBEDispatcher).
        kill();
        return null;
      }
    } else if (type == JICPProtocol.KEEP_ALIVE_TYPE) {
      if (enableServerKeepAlive) {
        inpManager.sendServerKeepAlive();
      }
      return outManager.handleKeepAlive(c, pkt);
    } /* Asynch-reply
      else if (type == JICPProtocol.RESPONSE_TYPE || type == JICPProtocol.ERROR_TYPE) {
      inpManager.handleResponse(c, pkt);
      return null;
      }*/ else {
      throw new ICPException("Unexpected packet type " + type);
    }
  }
Example #5
0
  /** Make this NIOBEDispatcher terminate. */
  public void shutdown() {
    active = false;
    if (myLogger.isLoggable(Logger.INFO)) {
      myLogger.log(Logger.INFO, myID + ": shutting down");
    }

    // Deregister from the JICPServer
    if (myID != null) {
      myMediatorManager.deregisterMediator(myID);
    }

    inpManager.shutdown();
    outManager.shutdown();
  }
Example #6
0
  public void evalParserOutput(final UnlabeledBracketSetCorpus output, final OutputManager man)
      throws CorpusError, EvalError, IOException {
    final ChunkedCorpus chunked = CorpusUtil.getChunkedCorpusClumps(alpha, output);
    treebankEval.getExperiment("asTrees", output.getTrees()).writeSummary(man.getResultsStream());

    clumpsEval.addExperiment(clumpsEval.newChunkingExperiment("clumps", chunked));
    clumpsEval.writeSummary(evalReportType, man.getResultsStream(), false);

    ubsFromClumpsEval.getExperiment("", output.getTrees()).writeSummary(man.getResultsStream());

    npsEval.addExperiment(npsEval.newChunkingExperiment("NPs", chunked));
    npsEval.writeSummary(evalReportType, man.getResultsStream(), false);

    ubsFromNPsEval.getExperiment("", output.getTrees()).writeSummary(man.getResultsStream());

    ubsFromPPsEval.getExperiment("", output.getTrees()).writeSummary(man.getResultsStream());

    if (!man.isNull()) {
      BufferedWriter bw = Util.bufferedWriter(man.treeOutputFilename());
      output.writeTo(bw, outputText);
      bw = Util.bufferedWriter(man.clumpsOutputFilename());
      chunked.writeTo(bw, outputText);
    }
  }
Example #7
0
  /**
   * Passes to this JICPMediator the connection opened by the mediated entity. This is called by the
   * JICPMediatorManager this Mediator is attached to as soon as the mediated entity (re)connects.
   *
   * @param c the connection to the mediated entity
   * @param pkt the packet that was sent by the mediated entity when opening this connection
   * @param addr the address of the mediated entity
   * @param port the local port used by the mediated entity
   * @return an indication to the JICPMediatorManager to keep the connection open.
   */
  public boolean handleIncomingConnection(
      Connection c, JICPPacket pkt, InetAddress addr, int port) {
    checkTerminatedInfo(pkt);

    if (peerActive) {
      if (connectionDropped) {
        droppedToDisconnected();
      }

      // Update keep-alive info
      lastReceivedTime = System.currentTimeMillis();

      boolean inp = false;
      byte[] data = pkt.getData();
      if (data.length == 1) {
        inp = (data[0] == 1);
      }
      if (inp) {
        inpManager.setConnection((NIOJICPConnection) c);
        if (myLogger.isLoggable(Logger.CONFIG)) {
          myLogger.log(Logger.CONFIG, myID + ": New INP Connection establishd");
        }
      } else {
        outManager.setConnection(c);
        if (myLogger.isLoggable(Logger.CONFIG)) {
          myLogger.log(Logger.CONFIG, myID + ": New OUT Connection establishd");
        }
      }

      return true;
    } else {
      // The remote FrontEnd has terminated spontaneously -->
      // Kill the above container (this will also kill this NIOBEDispatcher).
      kill();
      return false;
    }
  }
Example #8
0
 public boolean isConnected() {
   return inpManager.isConnected() && outManager.isConnected();
 }
Example #9
0
 /**
  * Move from the connectionDropped state to the Disconnected state. This may happen when - a
  * packet must be dispatched to the FE. - an incoming connection is detected
  */
 private void droppedToDisconnected() {
   connectionDropped = false;
   outManager.setExpirationDeadline();
 }