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

📄 ppoaa.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


package com.sri.oaa2.pp;

import java.net.*;
import com.sri.oaa2.lib.*;
import com.sri.oaa2.icl.*;

public class ppOaa {

  ppServices mPPServices = new ppServices();

  // Refrence to an OAA connection, so writeBB's can be performed
  LibOaa mOaaLib;

  public ppOaa(LibOaa inOaaLib) {
    mOaaLib = inOaaLib;
  }
  
  public boolean ppInit() {
    if (mOaaLib.oaaIsConnected("parent")) {
      // look up my own ID
      IclTerm me = mOaaLib.oaaPrimaryAddress();

      // We install a trigger on the facilitator, asking to be notified
      // everytime a pp server agent joins the community
      mOaaLib.oaaAddTrigger(new IclStr("data"), 
        IclTerm.fromString(true, "data(ppServer,service(Name,Params))"),
        IclTerm.fromString(true, "oaa_Solve(inform_pp(Op,Name,Params),[reply(none),address("+me.toString()+")])"),
          new IclList(new IclStruct("recurrence", new IclStr("whenever")),
                      new IclStruct("on", new IclVar("Op"))));

      // We dynamically expand the set of solvables of the agent so it 
      // will react to inform_pp/3 events
      mOaaLib.oaaDeclare(
        IclTerm.fromString(true, "inform_pp(Op,Name,Params)"),
        new IclList(),
        new IclList(),
        new IclList() );
      return true;
    }
    return false;
  }


 /****************************************************************************
  * Name : setupPpServer
  * In : String inAddress, address of the server to run as an ICL list of params.
  *                        Currently [port(Port), name(Name)]
  *                        Port : Where the server will be waiting for incoming 
  *                               clients connections.
  * In : String inParams, parameters of the connection as an ICL list of params.
  *                       The list may contain the following members :
  *                       binary(true/false) : 
  *                         true : Data to be sent is sent "as is" (Default)
  *                         false : Data sent as ICL terms.
  ***************************************************************************/
  
  public int setupPPServer(IclList inAddress, ppListener inListener, IclList inParams) {
    IclTerm portNumberAsTerm = IclUtils.getParamValue("port", null, inAddress);
    if (portNumberAsTerm!=null) {
      String portNumber = portNumberAsTerm.toString();
      ppOaaConnection newServerConnection = new ppOaaConnection(inListener, true);
      if (IclUtils.getParamValue("binary_data", null, inParams)!=null)
        newServerConnection.mBinary = true;
      // Extracts the name of the server
      IclTerm serverNameAsTerm = IclUtils.getParamValue("name", null, inAddress);
      if (serverNameAsTerm == null)
        newServerConnection.mName = "unnamed";
      else
        newServerConnection.mName = serverNameAsTerm.toString();
        
      // Uses the OAA connection to query the facilitator about available servers
      // to perform the writeBB about the server's information
      if (mOaaLib.oaaIsConnected("parent")) {
        mOaaLib.oaaAddData(IclTerm.fromString(true, "data(ppServer,service("+newServerConnection.mName+",["+
                                         "address(tcp("+getLocalHostName()+","+portNumber+"))]))"),
                           new IclList());
      }
      int newServerId = mPPServices.ppSetServer(new Integer(portNumber).intValue(), newServerConnection);
      if (newServerId != -1) {
        newServerConnection.mSocketId = newServerId;
      }
      return newServerId;
    }
    return -1;
  }

 /****************************************************************************
  * Name : setupPpClient
  * In : IclTerm inAddress, address of the server to contact.
  *                        Currently [service(Name), tcp(Host, Port)]
  * In : IclTerm inParams, parameters of the connection as an ICL list of params.
  *                        The list may contain the following members :
  *                        binary(true/false) : Data to be sent will be wrapped into
  *                                             a term structure.(Default)
  * Note : In the incoming address, name can be used as address because the
  *        facilitator holds information about available servers.
  *        The information stored in the facilitator is :
  *          data(ppServer, service(Name, Params))
  *        Params : [address(tcp(Host, Port)), binary(T/F)]
  ***************************************************************************/
  public int setupPPClient(IclList inAddress, ppListener inListener, IclList inParams, int inPriority) {
    String portNumber = null;
    String hostName = null;
    String localName = null;

    IclTerm addressAsTerm = IclUtils.getParamValue("tcp", null, inAddress);
    if (addressAsTerm!=null){      
      portNumber = addressAsTerm.getTerm(1).toString();
      hostName = addressAsTerm.getTerm(0).toString();
    }
    IclTerm localNameAsTerm = IclUtils.getParamValue("name", null, inAddress);
    if (localNameAsTerm!=null)
      localName = localNameAsTerm.toString();   
    int newClientId = -1;

    // DEBUG
    // System.out.println("Name " + localName);
    // System.out.println("Host " + hostName);
    // System.out.println("Port " + portNumber);

    // Not enough information about the server
    if (((portNumber==null)||(hostName==null)) && (localName==null))
      return newClientId;
    else {
      ppOaaConnection newClientConnection = new ppOaaConnection(inListener, false);

      if (IclUtils.getParamValue("binary_data", null, inParams)!=null)
        newClientConnection.mBinary = true;

      if ((portNumber!=null)&&(hostName!=null)) {
        System.out.println("Address found directly : On " + hostName + " at " + portNumber);
        newClientId = mPPServices.ppSetClient(hostName, new Integer(portNumber).intValue(), inPriority, newClientConnection);
      } else
      if (localName != null) {
        // If only the name of the service was given, we
        // use the OAA connection to query the facilitator about available servers
        // If more the one server match the name, we return the first one.
        IclTerm serverAddress = oaaGetBBInfo(localName);
        // DEBUG
        System.out.println("From oaaGetBBInfo " + serverAddress);
        // serverAddres should be : tcp(Host, Port)
        if (serverAddress!=null) {
          newClientId = mPPServices.ppSetClient(
            serverAddress.getTerm(0).toString(), 
            new Integer(serverAddress.getTerm(1).toString()).intValue(), 
            inPriority, newClientConnection);
        }
      }
      // Will be removed in case of unsuccessful connection
      if (newClientId != -1) {
        newClientConnection.mSocketId = newClientId;
      }
      return newClientId;
    }
  }

  public int setupPPClient(IclList inAddress, ppListener inListener, IclList inParams) {
    return setupPPClient(inAddress, inListener, inParams, Thread.NORM_PRIORITY);
  }

 /****************************************************************************
  * Name : ppSendData
  ***************************************************************************/
  public void ppSendData(int inSocketId, byte inData[], int inSize) {
    mPPServices.ppSendData(inSocketId, inData, inSize);
  }

 /****************************************************************************
  * Name : ppShutDownClient
  * In : int socketId
  ***************************************************************************/

 public void ppShutDownClient(int inSocketId) {
   mPPServices.ppDisconnect(inSocketId);
 }

 /****************************************************************************
  * Name : oaaGetBBInfo
  * In : String inName : The name of  the server whose address is needed.
  * Return : IclTerm : reurns what the addres for this service is. Reads is
  *          from the data publisjed by the ppServers as "address(X)", return
  *          X. Currently only address(tcp(Host, Port)) is supported.
  * Note : Reads data from the facilitator to find out what servers are
  *        available.
  ***************************************************************************/
  private IclTerm oaaGetBBInfo(String inServerName) {
    if (mOaaLib.oaaIsConnected("parent")) {
      IclList available_servers = new IclList();
      //[ppServer([name(firstPPServer),port(3200),host(elm)]), ppServer([name(firstPPServer2),port(3200),host(elm)])]
      boolean solveOk = mOaaLib.oaaSolve(
        IclTerm.fromString(true, "data(ppServer,service("+inServerName+",Params))"),
        new IclList(IclTerm.fromString(true, "block(true)")), available_servers);
      // DEBUG  
      // System.out.println("From data(A,B) : " + available_servers);
      // [data(ppServer,service(ppTestServer,[address(tcp(pine,3200))]))]
      if (solveOk) {
        IclList ppServerParams = (IclList)available_servers.getTerm(0).getTerm(1).getTerm(1);

⌨️ 快捷键说明

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