📄 frmmonitor.java
字号:
// System.out.println("EVENTTRIG: " + goal);
if (btnRecording.getIcon() == imgRecording) {
//evBrowser.addEvent(IclTerm.fromString(true, goal.toString()), true, true, true);
IclTerm safeGoal = removeBinaryData(goal);
evBrowser.addEvent(safeGoal, true, true, true);
}
}
else
// Asynchronous response to requests for information
// ev_solved(GoalId, Requesters, Solvers, Goal, Params, Sols)
if (goalName.equals("ev_solved") && (numArgs == 6)) {
IclTerm solutionList = goal.getTerm(5);
IclTerm pred = goal.getTerm(3);
String predString;
if(pred.isStruct()) {
predString = ((IclStruct)pred).getFunctor();
}
else {
predString = goal.toString();
}
// agent_data changes as new agents connect
// agent_data(Id,Type,ready,Sv,Name,Info)
if (predString.equals("agent_data")) {
loadAgentsContinued(solutionList);
}
else {
if ((predString.equals("agent_host") ||
predString.equals("agent_version"))) {
for (int i = 0; i < solutionList.size(); i++) {
IclTerm data = solutionList.getTerm(i);
// System.out.println("Updating info: " + data);
agtInfo agt = findAgentByIdName(icl_address_to_id(data.getTerm(0)).toString(), null);
if (agt != null) {
if (predString.equals("agent_host")) // agent_host(Id,Name,Host)
agt.host = data.getTerm(2).toString();
else { // agent_version(Id,Lang,Vers)
agt.language = data.getTerm(1).toString();
agt.version = data.getTerm(2).toString();
}
}
else
System.out.println("Error! Agent ID not found in "+pred);
}
}
}
}
// System.out.println("------------> Out DoEvent");
return true;
}
/** Monitor assumes that all events in evtLog, evtLogF can be converted between String
* and IclTerm representation at will. Unfortunately, this does not work for IclDataQ,
* For example, IclTerm.fromString((new IclDataQ(...)).toString()) fails.
* So, as a bit of a hack, we replace all IclDataQ with IclStr('<<BINARY-DATA>>')
* @return a copy of in with binary data replaced by strings, input is not changed
*/
IclTerm removeBinaryData(IclTerm in) {
IclTerm ret = (IclTerm)in.clone();
return removeBinaryData2(ret);
}
/** Helper for removeBinaryData. Destructive replace. */
IclTerm removeBinaryData2(IclTerm in) {
if (in instanceof IclDataQ) {
return new IclStr("<<BINARY-DATA>>");
}
else {
for (int n = 0; n < in.getNumChildren(); n++) {
in.replaceElement(n,removeBinaryData2(in.getTerm(n)));
}
return in;
}
}
//**************************************************************
// OAA Connection / Deconnection managment
//**************************************************************
boolean connectOaa(String inHostName, int inPortNumber) {
System.out.println("Trying to connect to: " + inHostName + "," + inPortNumber);
// First, connects to the facilitator
if (!oaa.getComLib().comConnect("parent",
new IclStruct("tcp", new IclStr(inHostName),
new IclInt(inPortNumber)),
new IclList())) {
System.out.println("Couldn't connect to " + inHostName + "," + inPortNumber);
return false;
}
// Then, once the connection is established, performs handshaking with the facilitator
if (!oaa.oaaRegister("parent", oaaName, IclTerm.fromString(true, oaaSolvables), new IclList())) {
System.out.println("Couldn't register agent.");
return false;
}
// Connection succeeded!
System.out.println("Connected!");
mnuConnect.setText("OAA Disconnect");
oaa.oaaReady(true); // Signify that monitor agent is initialized and ready
lblStatus.setText("Connected to Facilitator on " + inHostName + ", " + new Integer(inPortNumber).toString());
loadAgents();
return true;
}
/**
* File | Connect/Disconnect to OAA
* Uses command line parameters and setup file to find host/port/name
*/
void makeOAAConnection(boolean onlyIfAutoconnect) {
// If connected, disconnect
if (oaa.oaaIsConnected("parent")) {
oaa.oaaDisconnect("parent", new IclList());
cleanupAfterDisconnect();
mnuConnect.setText("OAA Connect");
lblStatus.setText("Disconnected from Facilitator");
} else { // Connect
// Since we provide a dialog box for the machine to connect
// to, we are looking for the address where the facilitator
// lives.
IclTerm remote_host = null;
IclTerm remote_port = null;
IclTerm remote_address = LibComUtils.oaaResolveVariable("oaa_connect", mArgs);
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", mArgs);
if (remote_address != null) {
if (remote_host==null)
remote_host = remote_address.getTerm(0);
if (remote_port==null)
remote_port = remote_address.getTerm(1);
}
}
// Checks for autoconnection
IclTerm localAutoConnect = LibComUtils.oaaResolveVariable("oaa", mArgs);
OaaConnectDialog connectDialog = new OaaConnectDialog(this, "OAA Connect", true);
if ((remote_host != null)&&(remote_port != null)) {
String host = remote_host.toString();
int port = ToInt.getInstance().from(remote_port, -1);
connectDialog.setHostName(host);
connectDialog.setPortNumber(port);
if ((localAutoConnect != null)&&(onlyIfAutoconnect)) // Connect immediately
if (connectOaa(host, port))
return;
} else { // Couldn't find setup file, use blank values
connectDialog.setHostName("unknown");
connectDialog.setPortNumber(0);
}
if (!onlyIfAutoconnect) {
// Centers and shows the dialog box
Dimension dlgSize = connectDialog.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
connectDialog.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
connectDialog.setVisible(true);
if (connectDialog.mOk)
connectOaa(connectDialog.getHostName(), connectDialog.getPortNumber());
}
}
}
/**
* As the Information Panel is resized, the solvable list expands accordingly
*/
void pnlInfo_componentResized(ComponentEvent e) {
lblAgentName.setSize(pnlInfo.getWidth() - 20, lblAgentName.getHeight());
cboSolvables.setSize(pnlInfo.getWidth() - 20, cboSolvables.getHeight());
}
/**
* If agents exist, these toolbar buttons should be enabled.
*/
void enableAgentFunctions(boolean agentsExist) {
btnRecording.setEnabled(agentsExist);
btnSmallerRadius.setEnabled(agentsExist);
btnBiggerRadius.setEnabled(agentsExist);
}
/**
* When the monitor panel is resized, the facilitator button is centered, and
* all agents are arranged in a circle around it.
* minH and minW contain the preferred size of the panel
*/
static final IclTerm FACID = new IclStr("0");
void pnlMonitor_componentResized(ComponentEvent e) {
int w = btnFacilitator.getWidth() / 2;
int h = btnFacilitator.getHeight() / 2;
int i, numAgents = agtList.size() - 1; // Remove Facilitator from count
int count = 0;
Double step, x, y;
Rectangle bf = new Rectangle();
agtInfo agt;
// center the facilitator in the middle of the monitor panel.
btnFacilitator.setLocation(pnlMonitor.getWidth() /2 - w,
pnlMonitor.getHeight()/2 - h);
btnFacilitator.getBounds(bf);
// for each agent, arrange it in a circle around the facilitator
if (numAgents > 0) {
int radius;
radius = numAgents * 15;
if (radius < 115)
radius = 115;
step = new Double(2.0 * Math.PI / numAgents);
// Goes to numAgents + 1 because numAgents doesn't count Facilitator
for (i = 0; i < numAgents + 1; i++) {
agt = (agtInfo)agtList.elementAt(i);
if (!agt.shortId.equals(FACID.toString())) { // Don't make Facilitator agent visible like other client agents
x = new Double(Math.cos(count * step.doubleValue()) * radius * radiusRatio +
bf.x + w - (agt.getWidth() / 2));
y = new Double(Math.sin(count * step.doubleValue()) * radius * radiusRatio +
bf.y + h - (agt.getHeight() / 2));
agt.setVisible(true);
agt.setLocation(x.intValue(), y.intValue());
// Upper half of agents add their labels on TOP of the icon
if (agt.agtLabel != null) {
FontMetrics fm = agt.agtLabel.getFontMetrics(agt.agtLabel.getFont());
agt.agtLabel.setVisible(true);
if (Math.sin(count * step.doubleValue()) < -0.01)
agt.agtLabel.setBounds(x.intValue() + (agt.getWidth()/2) -
agt.agtLabel.getWidth()/2,
y.intValue() - agt.agtLabel.getHeight() - 3,
fm.stringWidth(agt.agtLabel.getText()),fm.getHeight());
else
agt.agtLabel.setBounds(x.intValue() + (agt.getWidth()/2) -
agt.agtLabel.getWidth()/2,
y.intValue() + agt.getHeight() + 3,
fm.stringWidth(agt.agtLabel.getText()),fm.getHeight());
}
count = count + 1;
}
}
// Set the optimal size for the monitor panel
x = new Double(2.0 * (radius * radiusRatio + 45.0));
minW = x.intValue();
minH = minW;
pnlMonitor.setPreferredSize(new Dimension(minW, minH));
}
}
/**
* given infoList in the form: <br>
* infoList = [Id, Status, Solvables, Name] <br>
* adds an icon for the new agent and stores its
* information in agtList.
* Pictures for icons are looked for in the local
* directory using the agent's id, and if not found,
* the "agent.gif" image is used.
*/
// Example
// agent_data(addr(tcp('130.107.64.126',4200),5),client,open,
// [solvable(m_data(_10212,_10213),[],[]),
// solvable(m_ev(_10212,_10222,_10223),[],[])],
// oaa_monitor,[])
void AddAgentInfo(IclTerm info, boolean trigger) {
String shortId, status, name;
IclTerm id, solvables;
agtInfo agt;
ImageIcon icon = null;
int p;
//DEBUG
// System.out.println("---> info");
// System.out.println("---> trigger " + trigger);
// System.out.println(info);
// extract data from infoList
id = info.getTerm(0);
shortId = icl_address_to_id(id).toString();
status = info.getTerm(2).toString();
solvables = info.getTerm(3);
name = info.getTerm(4).toString();
// if name is in form "name.root", chop the ".root" part
p = name.indexOf(".");
if (p >= 0)
name = name.substring(0, p);
// see if the agent has already been added. This can happen
// if an agent updates its solvable list for instance.
agt = findAgentByIdName(shortId, name);
// If agent already exists, just update status and solvable fields
if (agt != null) {
agt.solvables = solvables;
agt.status = status;
}
// Otherwise, create a new agent icon
else {
JLabel agtLabel = new JLabel();
agt = new agtInfo(id.toString(), shortId, status, solvables, name, agtLabel);
setStatus("Agent added: "+agt.name + " (" + shortId + ")", false);
agt.setToolTipText(name);
// add into list
agtList.addElement(agt);
// load picture
try {
URL url = getAgentIconURL(name);
if (url != null) icon = new ImageIcon(getAgentIconURL(name));
if (icon != null) {
agt.setIcon(icon);
agt.setSize(icon.getIconWidth() + 4, icon.getIconHeight() + 4);
}
else {
agt.setText(name);
agt.setSize(40,40);
}
}
catch (Exception e) {
e.printStackTrace();
}
// Add event handler for "click" event of each agent
agt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
agentClicked(e);
}
});
agt.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// check for right-mouse click.
if (e.getModifiers() == Event.META_MASK) {
popupAgent = (agtInfo) e.getComponent();
jAgentPopupMenu.show(e.getComponent(),e.getX(), e.getY());
}
}
});
jMenuPing.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
popupAgent = null;
}
});
jMenuKill.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
if (popupAgent !=null) {
oaa.oaaPostEvent(IclTerm.fromString(true, "ev_post_event("+popupAgent.id + ",ev_halt)"), new IclList());
popupAgent = null;
}
}
});
// Add label for agent
agtLabel.setText(agt.name);
FontMetrics fm = agtLabel.getFontMetrics(agtLabel.getFont());
// Make invisible until resized and placed in the right spot
agtLabel.setVisible(false);
agtLabel.setBounds(10, 50, fm.stringWidth(agtLabel.getText()),fm.getHeight());
pnlMonitor.add(agtLabel);
enableAgentFunctions(true);
// Add agent itself
// Make invisible until resized and placed in the right spot
agt.setVisible(false);
// agt.setSize(40, 40);
pnlMonitor.add(agt);
if (trigger) {
// get all information about agent from Facilitator
// agent_host(ID, Name, Host) is a data published by the facilitator
oaa.oaaSolve(IclTerm.fromString(true, "agent_host(" + agt.id + ",Name,Host)"),
new IclList(IclTerm.fromString(true, "block(false)")),
null);
oaa.oaaSolve(IclTerm.fromString(true, "agent_version(" + agt.id + ",Lang,Vers)"),
new IclList(IclTerm.fromString(true, "block(false)")),
null);
}
// Update profiling chart
if (chartWindow.isVisible())
chartWindow.histo.addSeries(" " + agt.name + " (" + agt.shortId + ") ", Color.green);
// resize display accordingly
pnlMonitor_componentResized(null);
repaint();
pack();
// this.setSize(1000,600);
// validate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -