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

📄 oaamapperagent.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
// $CALOLSI$
/*
 * #=========================================================================
 * # Copyright 2004 SRI International.  All rights reserved.
 * #
 * # The material contained in this file is confidential and proprietary to SRI
 * # International and may not be reproduced, published, or disclosed to others
 * # without authorization from SRI International.
 * #
 * # DISCLAIMER OF WARRANTIES
 * #
 * # SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * # SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * # LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * # PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
 * # LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * # OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
 * #=========================================================================
 * Author : evans
 * Date: May 28, 2004
 * Time: 8:51:47 AM
 */
package com.sri.oaa2.mapper;

import java.io.IOException;

import com.sri.oaa2.com.LibCom;
import com.sri.oaa2.com.LibComTcpProtocol;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclVar;
import com.sri.oaa2.lib.LibOaa;

import org.apache.log4j.Logger;

/**
 * OAAMapperAgent is a simple OAA agent that registers with a local facilitator, advertises the solvables listed
 * in a an XML OAAMapper file, and passes solvable requests to a specified OAAMapper.  It can be subclassed in order
 * to add additional functionality, or the LibOaa object can be accessed directly.
 */
public class OAAMapperAgent
{
    private Logger logger = Logger.getLogger(super.getClass());
    private LibOaa oaaFacilitator;
    private OAAMapper oaaMapper;
    private String agentName;

    /**
     * Creates a new OAAMapperAgent with the specified OAAMapper and agent name.
     *
     * @param oaaMapper
     * @param agentName
     */
    public OAAMapperAgent(OAAMapper oaaMapper, String agentName)
    {
        this.oaaMapper = oaaMapper;
        this.agentName = agentName;
    }

    /**
     * Connects to a local facilitator.
     *
     * @throws IOException
     */
    public void connect()
        throws IOException
    {
        logger.info("Agent " + agentName + " connecting to facilitator");

        // extract solvables from the mapping:
        IclList solvables = new IclList();
        for(int i = 0; i < oaaMapper.getIclMapping().getFunctionMappings().length; i++)
        {
            FunctionMapping functionMapping = oaaMapper.getIclMapping().getFunctionMappings()[i];
            solvables.add(functionMapping.getIcl());
        }

        // connect:
        oaaFacilitator = new LibOaa(new LibCom(new LibComTcpProtocol(), null));
        boolean connected = oaaFacilitator.getComLib().comConnect("parent", new IclStruct("tcp", new IclVar("Host"), new IclVar("Port")), new IclList());
        if(!connected) throw new IOException("Failed to connect to facilitator");
        boolean registered = oaaFacilitator.oaaRegister("parent", agentName, solvables, new IclList());
        if(!registered) throw new IOException("Failed to register with facilitator");
        oaaMapper.setLibOaa(oaaFacilitator);
        oaaFacilitator.oaaRegisterCallback("app_do_event", oaaMapper);
        oaaFacilitator.oaaReady(true);
        logger.info("OAA connection for agent " + agentName + " successful");
    }

    /**
     * Disconnects from the facilitator.
     */
    public void disconnect()
    {
        oaaMapper.setLibOaa(null);
        if(oaaFacilitator == null) return;
        logger.info("Agent " + agentName + " disconnecting");
        oaaFacilitator.oaaReady(false);
        oaaFacilitator.oaaDisconnect(new IclList());
    }

    public boolean isConnected()
    {
        if(oaaFacilitator == null) return false;
        return oaaFacilitator.oaaIsConnected("parent");
    }

    public LibOaa getOaaFacilitator()
    {
        return oaaFacilitator;
    }

    public OAAMapper getOaaMapper()
    {
        return oaaMapper;
    }

    public String getAgentName()
    {
        return agentName;
    }
}

⌨️ 快捷键说明

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