(redirected from Main.UDPSocket)
See udp-socket for user-level documentation.
Below are notes about the module.
Implementation details
Important notes
- This accessor is still under development, now it only supports listening to a port.
- There is a working demo under $PTII/ptolemy/actor/lib/jjs/modules/udpSocket/demo.
Resources for the demo
- You can forward incoming UDP packets on the specified port of SwarmBox to your local machine (to the specified IP address) using the following JavaScript source code. (udpforward.js) (modified code from https://nodejs.org/api/dgram.html)
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("error", function (err) {
console.log("server error:\n" + err.stack);
server.close();
});
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " +
rinfo.address + ":" + rinfo.port + ' length: ' + msg.length);
client.send(msg, 0, msg.length, 22000, "192.168.1.213");
});
server.on("listening", function () {
var address = server.address();
console.log("server listening " +
address.address + ":" + address.port);
});
server.bind(22000);
Running an example in Cape Code and Node
- Install the Cape Code Host
- Run the model:
$PTII/bin/vergil -capecode $PTII/ptolemy/actor/lib/jjs/modules/udpSocket/test/auto/TestUDPSocketSelf.xml
- Click on the AccessorCodeGenerator and generate code and run the Composite Accessor. The following output will appear in the code generator:
// Starting ptolemy.cg.kernel.generic.accessor.AccessorCodeGenerator {.TestUDPSocketSelf.AccessorCodeGenerator} code generation.
Writing TestUDPSocketSelf.js in file:/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}/ (5241 characters)
In "/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}", about to execute:
node
nodeHostInvoke.js
-timeout
15000
{$HOSTS_NODE}/TestUDPSocketSelf
Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}/TestUDPSocketSelf.js
Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/net/UDPSocketListener.js
Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/test/TestDisplay.js
Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/net/UDPSocketSender.js
Reading accessor at: /Users/cxh/ptII/org/terraswarm/accessor/accessors/web/test/TrainableTest.js
Instantiated accessor TestUDPSocketSelf with class {$HOSTS_NODE}/TestUDPSocketSelf
// Code generation complete.
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
JavaScriptStop: about to call stop().
nodeHost.js: TestUDPSocketSelf.JavaScriptStop: stop() invoked
nodeHost.js: stop(): about to call wrapup on TestUDPSocketSelf.UDPSocketListener
nodeHost.js: stop(): about to call wrapup on JavaScript
nodeHost.js: stop(): about to call wrapup on TestUDPSocketSelf.TestDisplay
nodeHost.js: stop(): about to call wrapup on TestUDPSocketSelf.UDPSocketSender
nodeHost.js: stop(): about to call wrapup on TestUDPSocketSelf.TrainableTest
TrainableTest.js: wrapup() finished: TestUDPSocketSelf.TestUDPSocketSelf.TrainableTest
nodeHost.js: stop(): about to call wrapup on JavaScriptStop
JavaScriptStop.wrapup()
JavaScriptStop: done calling stop() on container
nodeHost.js: About to invoke wrapup().
nodeHost.js: invoking wrapup() for accessor: TestUDPSocketSelf.UDPSocketListener
nodeHost.js: invoking wrapup() for accessor: JavaScript
nodeHost.js: invoking wrapup() for accessor: TestUDPSocketSelf.TestDisplay
nodeHost.js: invoking wrapup() for accessor: TestUDPSocketSelf.UDPSocketSender
Warning: udpSocket.js: close() failed? See https://github.com/nodejs/node/issues/7061. Ignored exception was Error: Not running
Error: Not running
at Socket._healthCheck (dgram.js:530:11)
at Socket.close (dgram.js:424:8)
at exports.Socket.close (/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}/node_modules/udpSocket/udpSocket.js:188:14)
at Accessor.exports.wrapup (eval at Accessor (/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_COMMON}/commonHost.js:444:19), <anonymous>:107:10)
at Accessor.wrapup (/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_COMMON}/commonHost.js:571:23)
at exitHandler (/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}/nodeHost.js:373:17)
at emitOne (events.js:96:13)
at process.emit (events.js:188:7)
at process.exit (internal/process.js:146:15)
at Timeout._onTimeout (/Users/cxh/ptII/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}/nodeHost.js:236:15)
nodeHost.js: invoking wrapup() for accessor: TestUDPSocketSelf.TrainableTest
TrainableTest.js: wrapup() finished: TestUDPSocketSelf.TestUDPSocketSelf.TrainableTest
nodeHost.js: invoking wrapup() for accessor: JavaScriptStop
JavaScriptStop.wrapup()
All Done.
- To run by hand:
cd $PTI/org/terraswarm/accessor/accessors/web/{$HOSTS_NODE}
node nodeHostInvoke.js -timeout 15000 {$HOSTS_NODE}/TestUDPSocketSelf
Running an example in Node by hand
Below is another way to run the example.
An example is available in the accessors repo at: /accessors/web/net/test/auto/TestUdpSocketSelf.js
The example is a composite accessor that sends and receives strings, which has been generated from the Cape Code model in the Ptolemy II repo at:
$PTII/ptolemy/actor/lib/jjs/modules/udpSocket/test/auto/TestUDPSocketSelf.xml
To run in node, open a command window, change directory to /accessors/web/hosts/node
and start up the nodeHostShell:
In the shell, instantiate the example accessor:
var a = instantiate('test', 'net/test/auto/TestUdpSocketSelf');
Then, initialize the accessor:
You should see the following output:
undefined
nsh> a1
a2
a3
a4
a5
a6
a7
a8
a9
a10
See Also
- UDPSocketListener Accessor
- UDPSocketSender Accessor
- Watches, which could use this accessor
$PTII/org/terraswarm/accessor/test/auto/{UDPSocketInt.xml.UDPSocketString.xml}
- two Cape Code Host tests
$PTII/ptolemy/actor/lib/jjs/modules/udpSocket/test/auto/TestUDPSocketSelf.xml
- A Cape Code Host test that can generate a CompositeAccessor for the Node Host
accessors/web/net/test/TestUdpSocketSelf.js
- A Composite Accessor version of the Cape Code Host that can be run by the Node Host
- Modules - Other Modules