read Blackboard data correctly from the SFC file, and construct a BlackboardPart...
[openc2e.git] / caosVM_ports.cpp
blob99282bf32ad9a8d0fdfc1931e3cfeac93dbc33bb
1 /*
2 * caosVM_vehicles.cpp
3 * openc2e
5 * Created by Alyssa Milburn on 02/02/2005.
6 * Copyright 2005 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "caosVM.h"
21 #include <iostream>
22 #include "openc2e.h"
23 #include "Agent.h"
24 #include "Port.h"
26 /**
27 PRT: BANG (command) strength (integer)
28 %status stub
30 void caosVM::c_PRT_BANG() {
31 VM_VERIFY_SIZE(1)
32 VM_PARAM_INTEGER(strength)
35 /**
36 PRT: FRMA (agent) inputport (integer)
37 %status maybe
39 Returns agent to which the specified input port is connected, NULL if not
40 connected or the port doesn't exist.
42 void caosVM::v_PRT_FRMA() {
43 VM_VERIFY_SIZE(1)
44 VM_PARAM_INTEGER(inputport)
46 valid_agent(targ);
47 if (targ->inports.find(inputport) != targ->inports.end())
48 result.setAgent(targ->inports[inputport]->source);
49 else
50 result.setAgent(0);
53 /**
54 PRT: FROM (integer) inputport (integer)
55 %status maybe
57 Returns the output port id on the source agent connected to the specified
58 input port. Returns a negative value if the port is not connected or if the
59 source agent does not exist.
61 void caosVM::v_PRT_FROM() {
62 VM_VERIFY_SIZE(1)
63 VM_PARAM_INTEGER(inputport)
65 valid_agent(targ);
66 if (targ->inports.find(inputport) != targ->inports.end() && targ->inports[inputport]->source)
67 result.setInt(targ->inports[inputport]->sourceid);
68 else
69 result.setInt(-1);
72 /**
73 PRT: INEW (command) id (integer) name (string) desc (string) x (integer) y (integer) msgnum (integer)
74 %status maybe
76 Creates a new input port on targ. The message msgnum will be sent to the agent
77 when a signal arrives through the port. _P1_ of that message will be the data
78 of the signal.
80 void caosVM::c_PRT_INEW() {
81 VM_VERIFY_SIZE(6)
82 VM_PARAM_INTEGER(msgnum)
83 VM_PARAM_INTEGER(y)
84 VM_PARAM_INTEGER(x)
85 VM_PARAM_STRING(desc)
86 VM_PARAM_STRING(name)
87 VM_PARAM_INTEGER(id)
89 valid_agent(targ);
90 caos_assert(targ->inports.find(id) == targ->inports.end()); // TODO: multiple PRT: INEWs with the same id allowed?
91 targ->inports[id] = boost::shared_ptr<InputPort>(new InputPort(x, y, name, desc, msgnum));
94 /**
95 PRT: ITOT (integer)
96 %status maybe
98 Returns the total number of input ports.
100 void caosVM::v_PRT_ITOT() {
101 VM_VERIFY_SIZE(0)
103 valid_agent(targ);
105 // TODO: not strictly correct, I think; the CAOS docs are vague (surprise!)
106 result.setInt(targ->inports.size());
110 PRT: IZAP (command) id (integer)
111 %status maybe
113 Removes the input port with given id.
115 void caosVM::c_PRT_IZAP() {
116 VM_VERIFY_SIZE(1)
117 VM_PARAM_INTEGER(id)
119 valid_agent(targ);
120 caos_assert(targ->inports.find(id) != targ->inports.end());
121 AgentRef src = targ->inports[id]->source;
122 if (src) {
123 PortConnectionList &dests = src->outports[targ->inports[id]->sourceid]->dests;
124 PortConnectionList::iterator i = dests.begin();
125 while (i != dests.end()) {
126 if (i->first == targ && i->second == id) {
127 dests.erase(i);
128 break;
130 i++;
133 targ->inports.erase(id);
137 PRT: JOIN (command) source (agent) outputport (integer) dest (agent) inputport (integer)
138 %status maybe
140 Joins the output port from source to the input port of dest.
142 void caosVM::c_PRT_JOIN() {
143 VM_VERIFY_SIZE(4)
144 VM_PARAM_INTEGER(inputport)
145 VM_PARAM_VALIDAGENT(dest)
146 VM_PARAM_INTEGER(outputport)
147 VM_PARAM_VALIDAGENT(source)
149 caos_assert(source->outports.find(outputport) != source->outports.end());
150 caos_assert(dest->inports.find(inputport) != dest->inports.end());
152 source->join(outputport, dest, inputport);
156 PRT: KRAK (command) agent (agent) is_outport (integer) port (integer)
157 %status maybe
159 Breaks a connection on agent. If is_outport, kill all connections connected to
160 the port. Else, kill the connection to the inport.
162 void caosVM::c_PRT_KRAK() {
163 VM_VERIFY_SIZE(3)
164 VM_PARAM_INTEGER(port)
165 VM_PARAM_INTEGER(is_outport)
166 VM_PARAM_VALIDAGENT(agent)
168 if (is_outport) {
169 if (agent->outports.find(port) == agent->outports.end()) return;
170 PortConnectionList::iterator i = agent->outports[port]->dests.begin();
171 while (i != agent->outports[port]->dests.end()) {
172 PortConnectionList::iterator next = i; next++;
173 if (i->first && i->first->inports.find(i->second) != i->first->inports.end()) {
174 i->first->inports[i->second]->source.clear();
176 agent->outports[port]->dests.erase(i);
177 i = next;
179 } else {
180 if (agent->inports.find(port) == agent->inports.end()) return;
181 AgentRef src = agent->inports[port]->source;
182 if (src && src->outports.find(agent->inports[port]->sourceid) != src->outports.end()) {
183 src->outports[agent->inports[port]->sourceid]->dests.remove(std::pair<AgentRef, unsigned int>(agent, port));
185 agent->inports[port]->source.clear();
190 PRT: NAME (string) agent (agent) is_outport (integer) port (integer)
191 %status maybe
193 Returns the name of the specified port. Returns "" if the port doesn't exist.
195 void caosVM::v_PRT_NAME() {
196 VM_VERIFY_SIZE(0)
197 VM_PARAM_INTEGER(port)
198 VM_PARAM_INTEGER(is_outport)
199 VM_PARAM_VALIDAGENT(agent)
201 result.setString("");
202 if (is_outport) {
203 if (agent->outports.find(port) != agent->outports.end())
204 result.setString(agent->outports[port]->name);
205 } else {
206 if (agent->inports.find(port) != agent->inports.end())
207 result.setString(agent->inports[port]->name);
212 PRT: ONEW (command) id (integer) name (string) desc (string) x (integer) y (integer)
213 %status maybe
215 Creates a new output port on targ.
217 void caosVM::c_PRT_ONEW() {
218 VM_VERIFY_SIZE(5)
219 VM_PARAM_INTEGER(y)
220 VM_PARAM_INTEGER(x)
221 VM_PARAM_STRING(desc)
222 VM_PARAM_STRING(name)
223 VM_PARAM_INTEGER(id)
225 valid_agent(targ);
226 caos_assert(targ->outports.find(id) == targ->outports.end());
227 targ->outports[id] = boost::shared_ptr<OutputPort>(new OutputPort(x, y, name, desc));
231 PRT: OTOT (integer)
232 %status maybe
234 Returns the total number of output ports.
236 void caosVM::v_PRT_OTOT() {
237 VM_VERIFY_SIZE(0)
239 valid_agent(targ);
240 result.setInt(targ->outports.size());
244 PRT: OZAP (command) id (integer)
245 %status maybe
247 Destroys output port with given id.
249 void caosVM::c_PRT_OZAP() {
250 VM_VERIFY_SIZE(1)
251 VM_PARAM_INTEGER(id)
253 valid_agent(targ);
254 caos_assert(targ->outports.find(id) != targ->outports.end());
255 for (PortConnectionList::iterator i = targ->outports[id]->dests.begin(); i != targ->outports[id]->dests.end(); i++) {
256 if (!i->first) continue;
257 if (i->first->inports.find(i->second) == i->first->inports.end()) continue;
258 i->first->inports[i->second]->source.clear();
260 targ->outports.erase(id);
264 PRT: SEND (command) id (integer) data (anything)
265 %status maybe
267 Sends information over targ's output port.
269 void caosVM::c_PRT_SEND() {
270 VM_VERIFY_SIZE(2)
271 VM_PARAM_VALUE(data)
272 VM_PARAM_INTEGER(id)
274 valid_agent(targ);
276 caos_assert(targ->outports.find(id) != targ->outports.end());
278 PortConnectionList::iterator i = targ->outports[id]->dests.begin();
279 while (i != targ->outports[id]->dests.end()) {
280 PortConnectionList::iterator next = i; next++;
281 if (!i->first || i->first->inports.find(i->second) == i->first->inports.end()) {
282 targ->outports[id]->dests.erase(i);
283 i = next;
284 continue;
286 i->first->queueScript(i->first->inports[i->second]->messageno, targ, data);
287 i = next;
291 /* vim: set noet: */