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.
27 PRT: BANG (command) strength (integer)
30 void caosVM::c_PRT_BANG() {
32 VM_PARAM_INTEGER(strength
)
36 PRT: FRMA (agent) inputport (integer)
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() {
44 VM_PARAM_INTEGER(inputport
)
47 if (targ
->inports
.find(inputport
) != targ
->inports
.end())
48 result
.setAgent(targ
->inports
[inputport
]->source
);
54 PRT: FROM (integer) inputport (integer)
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() {
63 VM_PARAM_INTEGER(inputport
)
66 if (targ
->inports
.find(inputport
) != targ
->inports
.end() && targ
->inports
[inputport
]->source
)
67 result
.setInt(targ
->inports
[inputport
]->sourceid
);
73 PRT: INEW (command) id (integer) name (string) desc (string) x (integer) y (integer) msgnum (integer)
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
80 void caosVM::c_PRT_INEW() {
82 VM_PARAM_INTEGER(msgnum
)
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
));
98 Returns the total number of input ports.
100 void caosVM::v_PRT_ITOT() {
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)
113 Removes the input port with given id.
115 void caosVM::c_PRT_IZAP() {
120 caos_assert(targ
->inports
.find(id
) != targ
->inports
.end());
121 AgentRef src
= targ
->inports
[id
]->source
;
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
) {
133 targ
->inports
.erase(id
);
137 PRT: JOIN (command) source (agent) outputport (integer) dest (agent) inputport (integer)
140 Joins the output port from source to the input port of dest.
142 void caosVM::c_PRT_JOIN() {
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)
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() {
164 VM_PARAM_INTEGER(port
)
165 VM_PARAM_INTEGER(is_outport
)
166 VM_PARAM_VALIDAGENT(agent
)
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
);
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)
193 Returns the name of the specified port. Returns "" if the port doesn't exist.
195 void caosVM::v_PRT_NAME() {
197 VM_PARAM_INTEGER(port
)
198 VM_PARAM_INTEGER(is_outport
)
199 VM_PARAM_VALIDAGENT(agent
)
201 result
.setString("");
203 if (agent
->outports
.find(port
) != agent
->outports
.end())
204 result
.setString(agent
->outports
[port
]->name
);
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)
215 Creates a new output port on targ.
217 void caosVM::c_PRT_ONEW() {
221 VM_PARAM_STRING(desc
)
222 VM_PARAM_STRING(name
)
226 caos_assert(targ
->outports
.find(id
) == targ
->outports
.end());
227 targ
->outports
[id
] = boost::shared_ptr
<OutputPort
>(new OutputPort(x
, y
, name
, desc
));
234 Returns the total number of output ports.
236 void caosVM::v_PRT_OTOT() {
240 result
.setInt(targ
->outports
.size());
244 PRT: OZAP (command) id (integer)
247 Destroys output port with given id.
249 void caosVM::c_PRT_OZAP() {
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)
267 Sends information over targ's output port.
269 void caosVM::c_PRT_SEND() {
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
);
286 i
->first
->queueScript(i
->first
->inports
[i
->second
]->messageno
, targ
, data
);