📄 oaaconnection.java
字号:
/**
* The contents of this file are subject to the OAA Community Research
* License Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License
* at http://www.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999-2003. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.agt.startit;
import com.sri.oaa2.lib.*;
import com.sri.oaa2.com.*;
import com.sri.oaa2.icl.*;
import com.sri.oaa2.icl.IclUtils.*;
import java.net.InetAddress;
import java.util.*;
public class OaaConnection implements OAAEventListener {
private StartitInfo sinfo;
private LibOaa oaa;
private final String CONNECT_ID = "parent";
private ConnectThread connectThread;
private final Hashtable statusListeners = new Hashtable();
private ConnectListener informWhenConnected;
// private IclTerm oaaConnect;
private final static String oaaName = "startit";
private final static String oaaSolvables =
"[agent_status([Status,Name,Id]), start_app(Appid), kill_app(Appid), start_agent(Oaaname), kill_agent(Oaaname), start_shown_startit_apps, kill_and_exit_startit]";
private final static String Version = "2.1.0";
private IclTerm remote_host, remote_port;
public OaaConnection(StartitInfo si) {
// Create instance of OAA using TCP as the communication protocol
// leave oaa null for now
sinfo = si;
}
public OaaConnection(StartitInfo si, String args[]) {
sinfo = si;
// Create instance of OAA using TCP as the communication protocol
oaa = new LibOaa(new LibCom(new LibComTcpProtocol(), args));
IclTerm remote_address = LibComUtils.oaaResolveVariable("oaa_connect", args);
if (remote_address != null) {
remote_host = remote_address.getTerm(0);
remote_port = remote_address.getTerm(1);
}
else {
// Looks in the setup.pl file
remote_address = LibComUtils.oaaResolveVariable("default_facilitator", args);
if (remote_address != null) {
if (remote_host == null)
remote_host = remote_address.getTerm(0);
if (remote_port == null)
remote_port = remote_address.getTerm(1);
}
}
}
public boolean isConnected() {
if (oaa == null) return false;
return oaa.oaaIsConnected(CONNECT_ID);
}
public void disconnect() {
if (isConnected()) {
oaa.oaaDisconnect(new IclList());
}
}
public boolean connect() {
return connect(remote_host, remote_port);
}
public boolean connect(String host, int port) {
remote_host = new IclStr(host);
remote_port = new IclInt(port);
return connect(remote_host, remote_port);
}
private boolean connect(IclTerm host, IclTerm port) {
if (host == null || port == null) {
return false;
}
IclTerm oaaConnect = new IclStruct("tcp", host, port);
try {
StartitInfo.debugPrint("Trying to connect to: " + oaaConnect);
// First, connects to the facilitator
if (!oaa.getComLib().comConnect(CONNECT_ID, oaaConnect, new IclList())) {
StartitInfo.debugPrint("Couldn't connect to " + oaaConnect);
return false;
}
// Then, once the connection is established, performs handshaking with the facilitator
if (!oaa.oaaRegister(CONNECT_ID, oaaName, IclTerm.fromString(oaaSolvables), new IclList())) {
StartitInfo.errorPrint("Couldn't register agent " + oaaName);
return false;
}
if (connectThread != null) {
connectThread.giveUp(); // in case we were polling and ALSO someone called
} // connect() directly, we don't want to keep polling
// Attach function to default handler for incoming requests
oaa.oaaRegisterCallback("oaa_AppDoEvent", this);
// my own ID
IclTerm me = oaa.oaaPrimaryAddress();
oaa.oaaAddTrigger
(new IclStr("data"),
IclTerm.fromString("agent_data(Id,Type,ready,Sv,Name,Info)"),
IclTerm.fromString("oaa_Solve(agent_status([ready,Name,Id]),[reply(none),address(" +
me.toString() + ")])"),
new IclList(new IclStruct("recurrence", new IclStr("whenever")),
new IclStruct("on", new IclStr("add"))));
oaa.oaaReady(true);
}
catch (Exception e) {
System.err.println("ERROR: (internal) couldn't parse ICL string on connect(): " + e);
return false;
}
return true;
}
public void addStatusListener(StatusListener sl, String name) {
statusListeners.put(name, sl);
}
public void pollToConnect(String host, int port, ConnectListener cl) {
informWhenConnected = cl;
if (//isConnected() ||
(connectThread != null && connectThread.isAlive())) return;
remote_host = new IclStr(host);
remote_port = new IclInt(port);
oaa = new LibOaa(new LibCom(new LibComTcpProtocol(), null));
connectThread = new ConnectThread(host, port);
connectThread.start();
}
public void stopPolling() {
if (connectThread != null) connectThread.giveUp();
connectThread = null;
}
public void sendHalt(String oaaID) {
IclTerm haltCmd = IclTerm.fromString(true, "ev_post_event(" + oaaID + ",ev_halt)");
StartitInfo.debugPrint(haltCmd.toString());
oaa.oaaPostEvent(haltCmd, new IclList());
}
public boolean doOAAEvent(IclTerm goal, IclList params, IclList answers) {
//System.out.println(" GOAL: " + goal);
//System.out.println(" PARAMS: " + params);
//System.out.println("ANSWERS: " + answers);
String goalName = goal.toIdentifyingString();
int numArgs = goal.size();
// agent_status
if (goalName.equals("agent_status")) {
IclTerm list = goal.getTerm(0);
String status = list.getTerm(0).toIdentifyingString();
String agentName = list.getTerm(1).toIdentifyingString();
String oaaID = list.getTerm(2).toString();
if (!agentName.equals(oaaName)) { // if it's not just startit reporting ready
StatusListener sl = (StatusListener) statusListeners.get(agentName);
if (sl == null) {
StartitInfo.warningPrint("Unknown agent reported ready: " + agentName);
}
else {
sl.agentStatusChanged(agentName, status, oaaID);
}
}
return true;
}
// start_agent, start_app, kill_agent, or kill_app
else if (goalName.matches("(start|kill)_(app|agent)")) {
AppInfo app;
String name = goal.getTerm(0).toString();
if (goalName.endsWith("app")) { // find by appid
app = sinfo.findAppByName(name);
}
else {
app = sinfo.findAppByOaaName(name); // find by oaaname
}
if (app == null) {
StartitInfo.debugPrint("No such app named " + name);
return false;
}
if (goalName.startsWith("start")) { // start
app.start();
return true;
}
else { // kill
app.getProcess().kill();
return true;
}
}
// start_shown_startit_apps
else if (goalName.equals("start_shown_startit_apps")) {
sinfo.startShownApps();
return true;
}
// kill_and_exit_startit
else if (goalName.equals("kill_and_exit_startit")) {
sinfo.killAndExit();
return true;
}
// unknown solvable (shouldn't reach here)
else {
return false;
}
}
public String getOAAConnectString() {
return getOAAConnectString(true);
}
public String getOAAConnectString(boolean resolveLocalhost) {
IclTerm host = remote_host;
if (remote_host == null) remote_host = new IclStr("localhost");
if (resolveLocalhost && remote_host.toString().equals("localhost")) {
try {
host = new IclStr(InetAddress.getLocalHost().getHostName());
}
catch (java.net.UnknownHostException e) {
}
}
return new IclStruct("tcp", host, remote_port).toString();
}
private final static long POLL_TIME = 500; //milliseconds
private class ConnectThread extends Thread {
private boolean stopNow;
private IclTerm host, port;
public ConnectThread(String h, int p) {
host = new IclStr(h);
port = new IclInt(p);
}
public void giveUp() {
StartitInfo.debugPrint("stop polling");
stopNow = true;
}
public void run() {
StartitInfo.debugPrint("polling to connect");
stopNow = false;
boolean connected = false;
do {
try {
sleep(POLL_TIME);
}
catch (InterruptedException e) {
}
if (stopNow) break;
StartitInfo.debugPrint("poll: connect?");
connected = connect(host, port);
}
while (!connected);
StartitInfo.debugPrint("poll: connected");
if (connected && informWhenConnected != null) {
informWhenConnected.oaaConnected();
}
}
}
public interface StatusListener {
public void agentStatusChanged(String agentName, String status, String oaaID);
}
public interface ConnectListener {
public void oaaConnected();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -