Package jade.imtp.leap.JICP

Examples of jade.imtp.leap.JICP.JICPPacket


  /**
   * Handle an incoming JICP packet received by the MediatorManager
   */
  public JICPPacket handleJICPPacket(Connection c, JICPPacket pkt, InetAddress addr, int port) throws ICPException {
    JICPPacket response = null;
    if ((status == ACTIVE) && (frontEndStatus != TERMINATED)) {
      if (myLogger.isLoggable(Logger.FINE)) {
        myLogger.log(Logger.FINE, myID+" - Incoming packet. Type = "+pkt.getType()+", SID = "+pkt.getSessionID()+", terminated-info = "+((pkt.getInfo() & JICPProtocol.TERMINATED_INFO) != 0));
      }
      String from = " [" + addr + ":" + port + "]";
      if (pkt.getType() == JICPProtocol.COMMAND_TYPE) {
        // COMMAND
        if ((pkt.getInfo() & JICPProtocol.TERMINATED_INFO) != 0) {
          // PEER TERMINATION NOTIFICATION
          // The remote FrontEnd terminated spontaneously --> Terminate and notify up.
          myLogger.log(Logger.INFO, myID+" - Peer termination notification received");
          handlePeerSelfTermination();
          // Since we return null the MediatorManager would keep the connection open --> close it explicitly
          return createTerminationNotificationAck();
        } else {
          // NORMAL COMMAND
          // Serve the incoming command and send back the response
          byte sid = pkt.getSessionID();
          if (sid == lastIncomingCommandSid) {
            myLogger.log(Logger.WARNING, myID+" - Duplicated command received. SID = " + sid);
            response = lastResponse;
          } else {
            if (myLogger.isLoggable(Logger.FINE)) {
              myLogger.log(Logger.FINE, myID+" - Incoming command received. SID = " + sid);
            }
 
            byte[] rspData = mySkel.handleCommand(pkt.getData());
            if (myLogger.isLoggable(Logger.FINE)) {
              myLogger.log(Logger.FINE, myID+" - Incoming command served. SID = " + sid);
            }
            response = new JICPPacket(JICPProtocol.RESPONSE_TYPE, JICPProtocol.DEFAULT_INFO, rspData);
            response.setSessionID(sid);
            lastIncomingCommandSid = sid;
            lastResponse = response;
          }
        }
      } else {
View Full Code Here


 
        // Wait for the connection to deliver outgoing commands to be ready
        Connection c = getOutgoingCommandsConnection();
 
        // Send the command to the front-end
        JICPPacket cmd = new JICPPacket(JICPProtocol.COMMAND_TYPE, JICPProtocol.DEFAULT_INFO, payload);
        int sid = nextOutgoingCommandSid;
        nextOutgoingCommandSid = increment(nextOutgoingCommandSid);
        if (myLogger.isLoggable(Logger.FINE)) {
          myLogger.log(Logger.FINE, myID+" - Delivering outgoing command to front-end. SID = " + sid);
        }
        cmd.setSessionID((byte) sid);
        boolean deliverOK = false;
        try {
          c.writePacket(cmd);
          close(c);
          // Wait for the response
          JICPPacket response = getResponse(RESPONSE_TIMEOUT + RESPONSE_TIMEOUT_INCREMENT * (cmd.getLength() / 1024));
          deliverOK = true;
          if (myLogger.isLoggable(Logger.FINE)) {
            myLogger.log(Logger.FINE, myID+" - Response got. SID = " + sid);
          }
          if (response.getType() == JICPProtocol.ERROR_TYPE) {
            // Communication OK, but there was a JICP error on the peer
            throw new ICPException(new String(response.getData()));
          }
          return response.getData();
        }
        catch (IOException ioe) {
          setFrontEndDisconnected();
          throw new ICPException("Error delivering outgoing command to front-end. ", ioe);
        }
View Full Code Here

   
          // Send the command to the front-end
          if (myLogger.isLoggable(Logger.FINER)) {
            myLogger.log(Logger.FINER, myID+" - Delivering keep-alive to front-end");
          }
          JICPPacket cmd = new JICPPacket(JICPProtocol.KEEP_ALIVE_TYPE, JICPProtocol.DEFAULT_INFO, null);
          c.writePacket(cmd);
          close(c);
          // Wait for the response
          JICPPacket response = getResponse(RESPONSE_TIMEOUT + RESPONSE_TIMEOUT_INCREMENT * (cmd.getLength() / 1024));
          if (isKeepAliveResponse(response)) {
            if (myLogger.isLoggable(Logger.FINER)) {
              myLogger.log(Logger.FINER, myID+" - Keep-alive response got");
            }
          }
View Full Code Here

              setFrontEndDisconnected();
              throw new ICPException("Response timeout");
            }
          }
        }
        JICPPacket response = responseToLastOutgoingCommand;
        responseToLastOutgoingCommand = null;
        return response;
      }
    }
    catch (InterruptedException ie) {
View Full Code Here

      outgoingCommandsConnectionLock.notifyAll();
    }
  }
 
  private JICPPacket createTerminationNotificationAck() {
    return new JICPPacket(JICPProtocol.COMMAND_TYPE, JICPProtocol.TERMINATED_INFO, null);
  }
View Full Code Here

    checkTerminatedInfo(pkt);
   
    // Update keep-alive info
    lastReceivedTime = System.currentTimeMillis();
   
    JICPPacket reply = null;
    byte type = pkt.getType();
    switch (type) {
    case JICPProtocol.DROP_DOWN_TYPE:
      System.out.println("DROP_DOWN received: "+pkt.getSessionID());
      // 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);
      break;
    case JICPProtocol.COMMAND_TYPE:
      System.out.println("COMMAND received: "+pkt.getSessionID());
      if (peerActive) {
        reply = outManager.handleCommand(pkt);
      }
      else {
        // The remote FrontEnd has terminated spontaneously -->
        // Kill the above container (this will also kill this NIOBEDispatcher).
        kill();
      }
      break;
    case JICPProtocol.KEEP_ALIVE_TYPE:
      System.out.println("KEEP_ALIVE received: "+pkt.getSessionID());
      reply = outManager.handleKeepAlive(pkt);
      break;
    case JICPProtocol.RESPONSE_TYPE:
    case JICPProtocol.ERROR_TYPE:
      System.out.println("RESPONSE/ERROR received: "+pkt.getSessionID());
      inpManager.notifyIncomingResponseReceived(pkt);
      break;
    default:
      throw new ICPException("Unexpected packet type "+type);
    }
   
    if (reply != null) {
      try {
        writePacket(myConnection, reply);
        System.out.println("RESPONSE sent back: "+reply.getSessionID());
      }
      catch (IOException ioe) {
            myLogger.log(Logger.WARNING, myID+": Communication error sending back response. "+ioe);       
      }
    }
View Full Code Here

      droppedToDisconnected();
      throw new ICPException("Connection dropped");
    }
    else {
      // Normal dispatch
      JICPPacket pkt = new JICPPacket(JICPProtocol.COMMAND_TYPE, JICPProtocol.DEFAULT_INFO, payload);
      pkt = inpManager.dispatch(pkt, flush);
      return pkt.getData();
    }
  }
View Full Code Here

      myLogger.log(Logger.INFO,  myID+": DROP_DOWN request received.");
    }
   
    try {
      if (inpManager.isEmpty()) {
        JICPPacket rsp = new JICPPacket(JICPProtocol.RESPONSE_TYPE, JICPProtocol.DEFAULT_INFO, null);
        writePacket(c, rsp);
       
        myConnection = null;
        updateConnectedState();
        inpManager.resetConnection();
        connectionDropped = true;
      }
      else {
        // If we have some postponed command to flush, refuse dropping the connection
        myLogger.log(Logger.WARNING,  myID+": DROP_DOWN request refused.");
        JICPPacket rsp = new JICPPacket(JICPProtocol.ERROR_TYPE, JICPProtocol.DEFAULT_INFO, null);
        writePacket(c, rsp);
      }
    }
    catch (Exception e) {
      myLogger.log(Logger.WARNING,  myID+": Error writing DROP_DOWN response. "+e);
View Full Code Here

   
    void shutdown() {
    }
   
    final JICPPacket handleCommand(JICPPacket cmd) throws ICPException {
      JICPPacket reply = null;
      byte sid = cmd.getSessionID();
      if (sid == lastSid) {
        myLogger.log(Logger.WARNING,myID+": Duplicated packet from FE: pkt-type="+cmd.getType()+" info="+cmd.getInfo()+" SID="+sid);
        reply = lastResponse;
      }
      else {
        if(myLogger.isLoggable(Logger.FINE)) {
          myLogger.log(Logger.FINE, myID+": Received command "+sid+" from FE");
        }
       
        byte[] rspData = mySkel.handleCommand(cmd.getData());
        if(myLogger.isLoggable(Logger.FINER)) {
          myLogger.log(Logger.FINER, myID+": Command "+sid+" from FE served ");
        }
       
        reply = new JICPPacket(JICPProtocol.RESPONSE_TYPE, JICPProtocol.DEFAULT_INFO, rspData);
        reply.setSessionID(sid);
        lastSid = sid;
        lastResponse = reply;
      }
      return reply;
    }
View Full Code Here

   
    final JICPPacket handleKeepAlive(JICPPacket command) throws ICPException {
      if(myLogger.isLoggable(Logger.FINEST)) {
        myLogger.log(Logger.FINEST,myID+": Keep-alive received");
      }
      return new JICPPacket(JICPProtocol.RESPONSE_TYPE, JICPProtocol.DEFAULT_INFO, null);
    }
View Full Code Here

TOP

Related Classes of jade.imtp.leap.JICP.JICPPacket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.