⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppoaa.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return IclUtils.getParamValue("address", null, ppServerParams);
      }
    }
    return null;
  }

 /****************************************************************************
  * MISC FUNCTIONS
  ****************************************************************************/

 /****************************************************************************
  * Name : getLocalHostName
  ***************************************************************************/
  private String getLocalHostName() {
    try {
      return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException HostE) {
      return null;
    }
  }

}

class ppOaaConnection implements ppListener{

  boolean mServer;
  boolean mBinary;
  ppListener mListener;
  String mCurrentMessage = "";
  String mName;                   // For server connections only, not used for clients yet
  int mSocketId;

  public ppOaaConnection(ppListener inListener, boolean inIsServer) {
    mListener = inListener;
    mServer = inIsServer;
    mBinary = true;
  };

  private final static String wrapperStart = "term(";
  private final static String wrapperEnd = ").";

  /******** OLD VERSION **********

  public String buildTerm(byte inMessage[], int inSize) {
    // Convert the incoming bytes into a Java String
    String incomingPart = ppUtils.ppFromBytesToString(inMessage, inSize);

    for (int index = 0; index < inSize; index++) {
      // Ignores the <CR> and <NL> characters
      int charAsInt = new Integer(incomingPart.charAt(index)).intValue();
      if ((charAsInt != 13) && (charAsInt != 10)) {
        // Expands the message with incoming characters
        mCurrentMessage = mCurrentMessage + incomingPart.charAt(index);

        // Check if a full term was received
        int lastWrapperEnd = mCurrentMessage.indexOf(wrapperEnd);

        //System.out.println("Current : " + mCurrentMessage + " last end wrapper " + lastWrapperEnd);

        if (mCurrentMessage.startsWith(wrapperStart) &&
          (lastWrapperEnd!=-1) &&
          (parenthesisCount(mCurrentMessage)==0)) {
          //Extracts the new incoming term to be like : term(Term)
          String inTermAsString = mCurrentMessage.substring
            (0, mCurrentMessage.indexOf(wrapperEnd)+wrapperEnd.length());
          // Updates the mCurrentMessage with the rest
          mCurrentMessage = "";
          //mCurrentMessage.substring(inTermAsString.length()+1);

          //System.out.println("Sent : " + inTermAsString);
          //System.out.println("Remains : " + mCurrentMessage);

          return unwrapTerm(inTermAsString);
        }
      }
    }
    return null;
  }

  ******** OLD VERSION **********/

  int parCount = 0;
  public String buildTerm(byte inByte) {

    // Convert the incoming bytes into a char
    char inChar = (char)inByte;

    // Updates the parenthesis count
    if (inChar=='(')
      parCount++;
    if (inChar==')')
      parCount--;

    // Ignores the <CR> and <NL> characters
    int charAsInt = new Integer(inByte).intValue();
    if ((charAsInt != 13) && (charAsInt != 10)) {
      // Expands the message with incoming characters
      mCurrentMessage = mCurrentMessage + String.valueOf(inChar);

      // Check if a full term was received
      int lastWrapperEnd = mCurrentMessage.indexOf(wrapperEnd);

      //System.out.println("Current : " + mCurrentMessage + " last end wrapper " + lastWrapperEnd);

      if (mCurrentMessage.startsWith(wrapperStart) &&
        (lastWrapperEnd!=-1) &&
        (parCount==0)) {
        //Extracts the new incoming term to be like : term(Term)
        String inTermAsString = mCurrentMessage.substring
          (0, mCurrentMessage.indexOf(wrapperEnd)+wrapperEnd.length());
        // Updates the mCurrentMessage with the rest
        mCurrentMessage = "";
        //mCurrentMessage.substring(inTermAsString.length()+1);

        //System.out.println("Sent : " + inTermAsString);
        //System.out.println("Remains : " + mCurrentMessage);

        return unwrapTerm(inTermAsString);
      }
    }
    return null;
  }

/*
  public String buildTerm(byte inByte) {
    byte localArray[] = new byte[1];
    localArray[0] = inByte;
    return buildTerm(localArray,1);
  }
*/

  private String unwrapTerm(String inString) {
    if (inString.startsWith(wrapperStart) && inString.endsWith(wrapperEnd))
      return (inString.substring(wrapperStart.length(),inString.length() - wrapperEnd.length()));
    return inString;
  }

  private int parenthesisCount(String inString)
  {
    int parCount = 0;
    for (int i = 0; i<inString.length(); i++) {
      if (inString.charAt(i)=='(')
        parCount++;
      if (inString.charAt(i)==')')
        parCount--;
    }
    return parCount;
  }

 /****************************************************************************
  * Name : ppHandleMessage
  ***************************************************************************/
  synchronized public void ppHandleMessage(ppEvent inMessage) {
    //System.out.println("ppOaa >>> Received " + inMessage.mMessageType);
    switch(inMessage.mMessageType) {
    case (ppEvent.PP_MSG_RECEIVED):
      //System.out.println(" PPMessage received in ppOaa " + inMessage.mSize + " binary " + mBinary);
      //mBinary=true;
      if (!mBinary) {
        //System.out.println("Before built term");
        for (int index = 0; index < inMessage.mSize; index ++) {
          String newFullTerm = buildTerm(inMessage.mMessage[index]);

          // Debug
          int problem = mCurrentMessage.lastIndexOf(wrapperStart);
          if (problem >0) {
            System.out.println("PROBLEM");
            System.out.println(mCurrentMessage);
            System.out.println("Received");
            System.out.println(ppUtils.ppFromBytesToString(inMessage.mMessage, inMessage.mSize));
            System.exit(0);
          }
          if (newFullTerm != null) {
//            System.out.println("Full term");
            mListener.ppHandleMessage(new ppEvent(ppEvent.PP_MSG_RECEIVED, inMessage.mSocketId, ppUtils.ppFromStringToBytes(newFullTerm), newFullTerm.length()));
          }
        }




        //System.out.println("After built term ");
        //System.out.println(" Last wrapper " + mCurrentMessage.indexOf(wrapperEnd));
        //System.out.println(" Start " + mCurrentMessage.indexOf(wrapperStart));
        //System.out.println(" Par count " + parCount);
        //System.out.println("--- " + mCurrentMessage + "---");
      }else {
        mListener.ppHandleMessage(inMessage);
      }
    break;
    case (ppEvent.PP_MSG_DISCONNECT):
    case (ppEvent.PP_MSG_CLIENT_CONNECT):
      mListener.ppHandleMessage(inMessage);
    break;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -