From e4033f9d323e9b5374851f87185817aea32a717b Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 11 May 2011 23:21:32 +0100 Subject: [PATCH] Fix CRLF Following instructions here: http://help.github.com/dealing-with-lineendings/ --- .gitignore | 1 + INSTALL.md | 28 +- build.bat | 50 +- haxelib.xml | 16 +- org/zeromq/ZMQException.hx | 112 ++-- org/zeromq/ZMQPoller.hx | 364 ++++++------ org/zeromq/ZMQSocket.hx | 702 ++++++++++++------------ org/zeromq/guide/HelloWorldClient.hx | 124 ++--- org/zeromq/guide/HelloWorldServer.hx | 136 ++--- org/zeromq/guide/Interrupt.hx | 138 ++--- org/zeromq/guide/MTServer.hx | 310 +++++------ org/zeromq/guide/Run.hx | 210 +++---- org/zeromq/guide/TaskSink.hx | 144 ++--- org/zeromq/guide/TaskVent.hx | 162 +++--- org/zeromq/guide/TaskWork.hx | 140 ++--- org/zeromq/guide/WUClient.hx | 176 +++--- org/zeromq/guide/WUServer.hx | 114 ++-- org/zeromq/test/BaseTest.hx | 318 +++++------ org/zeromq/test/TestAll.hx | 90 +-- org/zeromq/test/TestContext.hx | 226 ++++---- org/zeromq/test/TestError.hx | 106 ++-- org/zeromq/test/TestInterrupt.hx | 108 ++-- org/zeromq/test/TestMultiPartMessage.hx | 126 ++--- org/zeromq/test/TestPoller.hx | 384 ++++++------- org/zeromq/test/TestPubSub.hx | 180 +++--- org/zeromq/test/TestReqRep.hx | 248 ++++----- org/zeromq/test/TestSocket.hx | 378 ++++++------- org/zeromq/test/TestVersion.hx | 110 ++-- src/Poller.cpp | 200 +++---- src/socket.h | 48 +- 30 files changed, 2725 insertions(+), 2724 deletions(-) diff --git a/.gitignore b/.gitignore index c0f94fa..fa2ba9b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ lib/ obj/ all_objs vc90.pdb +hxzmq.hxproj diff --git a/INSTALL.md b/INSTALL.md index 45ee9ae..bd6e2a3 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,14 +1,14 @@ -hxzmq Installation Instructions -=============================== - -## haxelib Installation - -To be able to just use hxzmq to build 0MQ messaging into new haXe applications, without modifying the or re-building hxzmq from source, you need: - -1. Install the current hxzmq package using the haXe haxelib tool - haxelib install hxzmq -2. To build your application including the hxzmq project, include the hxzmq library in your hxml haXe compilation file: - -lib hxzmq -3. To run your target executable, your system will need to reach the hxzmq.ndll library file and the libzmq.dll 0MQ library file. Add paths to both to your PATH environment variable, or copy both files into the same folder as your newly-minted haXe executable (e.g. either a neko .n or cpp executable). - - +hxzmq Installation Instructions +=============================== + +## haxelib Installation + +To be able to just use hxzmq to build 0MQ messaging into new haXe applications, without modifying the or re-building hxzmq from source, you need: + +1. Install the current hxzmq package using the haXe haxelib tool + haxelib install hxzmq +2. To build your application including the hxzmq project, include the hxzmq library in your hxml haXe compilation file: + -lib hxzmq +3. To run your target executable, your system will need to reach the hxzmq.ndll library file and the libzmq.dll 0MQ library file. Add paths to both to your PATH environment variable, or copy both files into the same folder as your newly-minted haXe executable (e.g. either a neko .n or cpp executable). + + diff --git a/build.bat b/build.bat index 24f88f3..07e8512 100644 --- a/build.bat +++ b/build.bat @@ -1,26 +1,26 @@ -echo "** Build hxzmq.ndll on Windows:" -haxelib run hxcpp build.xml - -echo "** Build Haxe Unit Tests:" -cd test -haxe buildWindows.hxml - -echo "** Build Haxe ZeroMQ Guide programs:" -cd ../guide -haxe buildWindows.hxml - -echo "** Copy hxzmq.ndll:" -cd .. -copy /Y out\ndll\Windows\hxzmq.ndll test\out-cpp\Windows -copy /Y out\ndll\Windows\hxzmq.ndll test\out-neko\Windows -copy /Y out\ndll\Windows\hxzmq.ndll guide\out-cpp\Windows -copy /Y out\ndll\Windows\hxzmq.ndll guide\out-neko\Windows - - -rem echo "** Run CPP unit tests:" -rem cd test/out-cpp/Windows -rem TestAll-debug.exe - -rem echo "** Run Neko unit tests:" -rem cd test/out-neko/Windows +echo "** Build hxzmq.ndll on Windows:" +haxelib run hxcpp build.xml + +echo "** Build Haxe Unit Tests:" +cd test +haxe buildWindows.hxml + +echo "** Build Haxe ZeroMQ Guide programs:" +cd ../guide +haxe buildWindows.hxml + +echo "** Copy hxzmq.ndll:" +cd .. +copy /Y out\ndll\Windows\hxzmq.ndll test\out-cpp\Windows +copy /Y out\ndll\Windows\hxzmq.ndll test\out-neko\Windows +copy /Y out\ndll\Windows\hxzmq.ndll guide\out-cpp\Windows +copy /Y out\ndll\Windows\hxzmq.ndll guide\out-neko\Windows + + +rem echo "** Run CPP unit tests:" +rem cd test/out-cpp/Windows +rem TestAll-debug.exe + +rem echo "** Run Neko unit tests:" +rem cd test/out-neko/Windows rem neko TestAll.n \ No newline at end of file diff --git a/haxelib.xml b/haxelib.xml index 1a55086..dbd0fef 100644 --- a/haxelib.xml +++ b/haxelib.xml @@ -1,9 +1,9 @@ - - - - - - - Haxe language binding for the ZeroMQ socket library - Initial upload, compatable with libzmq 2.1.6+ + + + + + + + Haxe language binding for the ZeroMQ socket library + Initial upload, compatable with libzmq 2.1.6+ \ No newline at end of file diff --git a/org/zeromq/ZMQException.hx b/org/zeromq/ZMQException.hx index 28403f4..9bc3d0f 100644 --- a/org/zeromq/ZMQException.hx +++ b/org/zeromq/ZMQException.hx @@ -1,57 +1,57 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq; - -import org.zeromq.ZMQ; - -/** - * Encapsulates ZMQ Errors - * Provides the ZMQ - specified errno and a human - readable description - */ -class ZMQException { - - public var err(default, null):ErrorType; - - public var errNo(default,null):Int; - - public function new(e:ErrorType) { - this.err = e; - this.errNo = ZMQ.errorTypeToErrNo(err); - } - - /** - * Returns ZMQ - specified human-readable error description - * @return - */ - public function str():String { - return ZMQ.strError(errNo); - } - - public function toString():String { - var b:StringBuf = new StringBuf(); - - b.add("errNo:" + errNo + ", str:" + str()); - - return b.toString(); - } - - //private static var _hx_zmq_strerror = neko.Lib.load("hxzmq", "hx_zmq_strerror", 1); - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq; + +import org.zeromq.ZMQ; + +/** + * Encapsulates ZMQ Errors + * Provides the ZMQ - specified errno and a human - readable description + */ +class ZMQException { + + public var err(default, null):ErrorType; + + public var errNo(default,null):Int; + + public function new(e:ErrorType) { + this.err = e; + this.errNo = ZMQ.errorTypeToErrNo(err); + } + + /** + * Returns ZMQ - specified human-readable error description + * @return + */ + public function str():String { + return ZMQ.strError(errNo); + } + + public function toString():String { + var b:StringBuf = new StringBuf(); + + b.add("errNo:" + errNo + ", str:" + str()); + + return b.toString(); + } + + //private static var _hx_zmq_strerror = neko.Lib.load("hxzmq", "hx_zmq_strerror", 1); + } \ No newline at end of file diff --git a/org/zeromq/ZMQPoller.hx b/org/zeromq/ZMQPoller.hx index 786c042..68a93dd 100644 --- a/org/zeromq/ZMQPoller.hx +++ b/org/zeromq/ZMQPoller.hx @@ -1,183 +1,183 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq; - -import neko.Lib; -import org.zeromq.ZMQ; -import org.zeromq.ZMQSocket; - -/** - * Encapsulates ZMQ Poller functions. - * - * Statefull class, maintaining a set of sockets to poll, events to poll for - */ -class ZMQPoller -{ - /** - * Provides the last-polled set of rececived events - */ - public var revents(default,null):Array; - - private var pollItems:List; - - /** - * Constructor - */ - public function new() { - - pollItems = new List(); - revents = new Array(); - - } - - public function getSize():Int { - return pollItems.length; - } - - /** - * Adds a socket to the internak list of polled sockets - * @param socket A ZMQScxket object - * @param event Bitmasked Int for polled events (ZMQ_POLLIN, ZMQ_POLLOUT) - */ - public function registerSocket(socket:ZMQSocket, event:Int) - { - - if (socket == null || event == null) { - throw new ZMQException(EINVAL); - return; - } - - pollItems.add( { _socket:socket, _event:event } ); - - } - - /** - * Removes a previously registered socket - * @param socket - * @return - */ - public function unregisterSocket(socket:ZMQSocket):Bool { - - if (socket == null) { - throw new ZMQException(EINVAL); - return null; - } - - // Find first matching socket object, then remove it - for (pi in pollItems) { - if (pi._socket == socket) { - pollItems.remove(pi); - return true; - } - } - - return false; - } - - /** - * Removes all current registered sockets - */ - public function unregisterAllSockets() { - pollItems.clear(); - } - - /** - * Poll a set of 0MQ sockets, - * @param ?timeout Timeout in microseconds, or 0 to return immediately, or -1 to block indefintely (default) - * @return how many objects signalled, or 0 if none, or -1 if failure - */ - public function poll(?timeout:Int = -1):Int - { - - // Split pollItems array into 2 separate arrays to pass to the native layer - var sArray:Array = new Array(); - var eArray:Array = new Array(); - - revents = null; // Clear out revents array ready for next set of results - revents = new Array(); - for (p in pollItems) { - sArray.push(p._socket._socketHandle); - eArray.push(p._event); - revents.push(0); // initialise revents array - } - - try { - var r:PollResult = _hx_zmq_poll(sArray, eArray, timeout); - if (r == null) { - return -1; - } - revents = Lib.nekoToHaxe(r._revents).copy(); - return r._ret; - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - return -1; - } catch (e:Dynamic) { - return -1; - } - } - - /** - * Test if the s'th registered socket has a registered POLLIN event. - * Call this after a poll() method call to test the results. - * - * @param s Valid s parameter range from 1 to revents.length - * @return True if specified registered socket has a current POLLIN event, else False - */ - public function pollin(s: Int):Bool { - if (revents == null || revents.length == 0) return false; - if (s > revents.length) return false; - - return (revents[s - 1] & ZMQ.ZMQ_POLLIN()) == ZMQ.ZMQ_POLLIN(); - } - - /** - * Test if the s'th registered socket has a registered POLLOUT event. - * Call this after a poll() method call to test the results. - * - * @param s Valid s parameter range from 1 to revents.length - * @return True if specified registered socket has a current POLLOUT event, else False - */ - public function pollout(s: Int):Bool { - if (revents == null || revents.length == 0) return false; - if (s > revents.length) return false; - - return (revents[s - 1] & ZMQ.ZMQ_POLLOUT()) == ZMQ.ZMQ_POLLOUT(); - } - - /** - * Test of the s'th registered socket has no received polled events - * @param s Valid s parameter range from 1 to revents.length - * @return True if no received polled events on the socket - */ - public function noevents(s:Int):Bool { - return (!pollin(s) && !pollout(s)); - } - - private static var _hx_zmq_poll = Lib.load("hxzmq", "hx_zmq_poll", 3); -} - -typedef PollSocketEventTuple = { - _socket:ZMQSocket, - _event:Int }; - -typedef PollResult = { - _revents:Array, - _ret:Int +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq; + +import neko.Lib; +import org.zeromq.ZMQ; +import org.zeromq.ZMQSocket; + +/** + * Encapsulates ZMQ Poller functions. + * + * Statefull class, maintaining a set of sockets to poll, events to poll for + */ +class ZMQPoller +{ + /** + * Provides the last-polled set of rececived events + */ + public var revents(default,null):Array; + + private var pollItems:List; + + /** + * Constructor + */ + public function new() { + + pollItems = new List(); + revents = new Array(); + + } + + public function getSize():Int { + return pollItems.length; + } + + /** + * Adds a socket to the internak list of polled sockets + * @param socket A ZMQScxket object + * @param event Bitmasked Int for polled events (ZMQ_POLLIN, ZMQ_POLLOUT) + */ + public function registerSocket(socket:ZMQSocket, event:Int) + { + + if (socket == null || event == null) { + throw new ZMQException(EINVAL); + return; + } + + pollItems.add( { _socket:socket, _event:event } ); + + } + + /** + * Removes a previously registered socket + * @param socket + * @return + */ + public function unregisterSocket(socket:ZMQSocket):Bool { + + if (socket == null) { + throw new ZMQException(EINVAL); + return null; + } + + // Find first matching socket object, then remove it + for (pi in pollItems) { + if (pi._socket == socket) { + pollItems.remove(pi); + return true; + } + } + + return false; + } + + /** + * Removes all current registered sockets + */ + public function unregisterAllSockets() { + pollItems.clear(); + } + + /** + * Poll a set of 0MQ sockets, + * @param ?timeout Timeout in microseconds, or 0 to return immediately, or -1 to block indefintely (default) + * @return how many objects signalled, or 0 if none, or -1 if failure + */ + public function poll(?timeout:Int = -1):Int + { + + // Split pollItems array into 2 separate arrays to pass to the native layer + var sArray:Array = new Array(); + var eArray:Array = new Array(); + + revents = null; // Clear out revents array ready for next set of results + revents = new Array(); + for (p in pollItems) { + sArray.push(p._socket._socketHandle); + eArray.push(p._event); + revents.push(0); // initialise revents array + } + + try { + var r:PollResult = _hx_zmq_poll(sArray, eArray, timeout); + if (r == null) { + return -1; + } + revents = Lib.nekoToHaxe(r._revents).copy(); + return r._ret; + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + return -1; + } catch (e:Dynamic) { + return -1; + } + } + + /** + * Test if the s'th registered socket has a registered POLLIN event. + * Call this after a poll() method call to test the results. + * + * @param s Valid s parameter range from 1 to revents.length + * @return True if specified registered socket has a current POLLIN event, else False + */ + public function pollin(s: Int):Bool { + if (revents == null || revents.length == 0) return false; + if (s > revents.length) return false; + + return (revents[s - 1] & ZMQ.ZMQ_POLLIN()) == ZMQ.ZMQ_POLLIN(); + } + + /** + * Test if the s'th registered socket has a registered POLLOUT event. + * Call this after a poll() method call to test the results. + * + * @param s Valid s parameter range from 1 to revents.length + * @return True if specified registered socket has a current POLLOUT event, else False + */ + public function pollout(s: Int):Bool { + if (revents == null || revents.length == 0) return false; + if (s > revents.length) return false; + + return (revents[s - 1] & ZMQ.ZMQ_POLLOUT()) == ZMQ.ZMQ_POLLOUT(); + } + + /** + * Test of the s'th registered socket has no received polled events + * @param s Valid s parameter range from 1 to revents.length + * @return True if no received polled events on the socket + */ + public function noevents(s:Int):Bool { + return (!pollin(s) && !pollout(s)); + } + + private static var _hx_zmq_poll = Lib.load("hxzmq", "hx_zmq_poll", 3); +} + +typedef PollSocketEventTuple = { + _socket:ZMQSocket, + _event:Int }; + +typedef PollResult = { + _revents:Array, + _ret:Int }; \ No newline at end of file diff --git a/org/zeromq/ZMQSocket.hx b/org/zeromq/ZMQSocket.hx index 395f4f3..6a3fa92 100644 --- a/org/zeromq/ZMQSocket.hx +++ b/org/zeromq/ZMQSocket.hx @@ -1,351 +1,351 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq; - -import haxe.io.Bytes; -import haxe.io.BytesData; -import neko.Lib; -import neko.Sys; - -import org.zeromq.ZMQ; - -/** - * A 0MQ socket - * - * These objects will generally be created via the socket() method of a ZMQContext object. - * - * Class based on code from pyzmq project - * See: https://github.com/zeromq/pyzmq/blob/master/zmq/core/socket.pyx - */ -class ZMQSocket -{ - - /** Records if socket has been closed */ - public var closed(default,null):Bool; - - /** - * Hold reference to context associated with socket, to stop it being garbage collected - */ - public var context(default,null):ZMQContext; - - /** Opaque data used by hxzmq driver */ - public var _socketHandle(default,null):Dynamic; - - /** - * Constructor. - * - * Creates a new ZMQ socket - * @param context A ZMQ Context previously created - * @param type A ZMQ socket type - */ - public function new(context:ZMQContext, type:SocketType) - { - closed = true; - this.context = context; - try { - _socketHandle = _hx_zmq_construct_socket(context.contextHandle, ZMQ.socketTypeNo(type)); - - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - closed = false; - } - - /** - * Close the socket - * - * This can be called to close the socket by hand. If this is not - * called, the socket will automatically be closed when it is - * garbage collected. - */ - public function close() - { - if (_socketHandle != null && !closed) { - try { - _hx_zmq_close(_socketHandle); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - closed = true; - _socketHandle = null; - } - } - - /** - * Bind a socket to an address - * - * This causes the socket to listen on a network port. Sockets on the - * other side of this connection will use ``Socket.connect(addr)`` to - * connect to this socket. - * - * @param addr The address string. - * This has the form 'protocol://interface:port', - * for example 'tcp://127.0.0.1:5555'. Protocols supported are - * tcp, upd, pgm, inproc and ipc. If the address is unicode, it is - * encoded to utf-8 first. - */ - public function bind(addr:String) - { - if (closed) - throw new ZMQException(ENOTSUP); - - try { - _hx_zmq_bind(_socketHandle, Lib.haxeToNeko(addr)); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - } - - /** - * Connect to a remote ZMQ socket - * - * @param addr The address string - * This has the form 'protocol://interface:port', - * for example 'tcp://127.0.0.1:5555'. Protocols supported are - * tcp, upd, pgm, inproc and ipc. If the address is unicode, it is - * encoded to utf-8 first. - */ - public function connect(addr:String) - { - if (closed) - throw new ZMQException(ENOTSUP); - - try { - _hx_zmq_connect(_socketHandle, Lib.haxeToNeko(addr)); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - } - - /** - * Set socket options. - * - * See the ZMQ documentation for details on specific options: - * http://api.zeromq.org/master:zmq-setsockopt - * - * - * @param option SocketOptionsType (defined in ZMQ.hx) - * @param optval Either Int or String or Bytes - */ - public function setsockopt(option:SocketOptionsType, optval:Dynamic):Void { - - if (closed) - throw new ZMQException(ENOTSUP); - - // Handle 32 bit int options - if (Lambda.exists(ZMQ.intSocketOptionTypes, - function(so) { return so == option; } )) - { - if (!Std.is(optval,Int)) - throw new String("Expected Int, got " + optval); - - try { - _hx_zmq_setintsockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), optval); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - // Handle 64 bit int options - } else if (Lambda.exists(ZMQ.int64SocketOptionTypes, - function(so) { return so == option; } )) - { - var _hi = Reflect.field(optval, "hi"); - var _lo = Reflect.field(optval, "lo"); - if (_hi == null || _lo == null) { - throw new String("Expected ZMQInt64Type, got " + optval); - return null; - } - - try { - _hx_zmq_setint64sockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), _hi, _lo); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - // Handle bytes options - } else if (Lambda.exists(ZMQ.bytesSocketOptionTypes, - function(so) { return so == option; } )) - { - if (!Std.is(optval, Bytes)) { - throw new String("Expected Bytes, got " + optval); - return null; - } - try { - _hx_zmq_setbytessockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), optval.getData() ); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - } else { - throw new ZMQException(EINVAL); - } - return; - } - - /** - * Return a previously set socket option - * - * @param option - * @return - */ - public function getsockopt(option:SocketOptionsType):Dynamic - { - var _optval:Dynamic; - - if (closed) { - throw new ZMQException(ENOTSUP); - return null; - } - - if (Lambda.exists(ZMQ.intSocketOptionTypes, - function(so) { return so == option; } )) - { - - try { - _optval = Lib.nekoToHaxe(_hx_zmq_getintsockopt(_socketHandle, ZMQ.socketOptionTypeNo(option))); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - return null; - } - if (!Std.is(_optval,Int)) { - throw new String("Expected Int, got " + _optval); - return null; - } else { - return _optval; - } - } else if (Lambda.exists(ZMQ.int64SocketOptionTypes, - function(so) { return so == option; } )) - { - - try { - _optval = Lib.nekoToHaxe(_hx_zmq_getint64sockopt(_socketHandle, ZMQ.socketOptionTypeNo(option))); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - return null; - } - var _hi = Reflect.field(_optval, "hi"); - var _lo = Reflect.field(_optval, "lo"); - if (_hi == null || _lo == null) { - throw new String("Expected ZMQInt64Type, got " + _optval); - return null; - } else { - return {hi:_optval.hi, lo:_optval.lo}; - } - }else if (Lambda.exists(ZMQ.bytesSocketOptionTypes, - function(so) { return so == option; } )) - { - - try { - _optval = _hx_zmq_getbytessockopt(_socketHandle, ZMQ.socketOptionTypeNo(option)); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - return null; - } - return Bytes.ofData(_optval); - - } else { - throw new ZMQException(EINVAL); - return null; - } - return null; - - } - - /** - * Send a message on this socket - * - * This queues the message to be sent by the IO thread at a later time. - * - * @param data The content of the message - * @param ?flags Any supported SocketFlag DONTWAIT, SNDMORE - */ - public function sendMsg(data:Bytes, ?flags:SendReceiveFlagType):Void { - - if (closed) { - throw new ZMQException(ENOTSUP); - } - - try { - _hx_zmq_send(_socketHandle, data.getData(), ZMQ.sendReceiveFlagNo(flags)); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - } - - /** - * Receive a message on this socket - * - * Will return either a message, null (if DONTWAIT was used and there was no data received) or a ZMQException - * - * @param ?flags - * @return - */ - public function recvMsg(?flags:SendReceiveFlagType):Bytes { - - if (closed) - throw new ZMQException(ENOTSUP); - - var bytes:BytesData = null; - - try { - bytes = _hx_zmq_rcv(_socketHandle, ZMQ.sendReceiveFlagNo(flags)); - } catch (e:Int) { - throw new ZMQException(ZMQ.errNoToErrorType(e)); - } - - return { - if (bytes == null) { - null; - } else { - Bytes.ofData(bytes); - }; - } - - } - - /** - * Convenience method to test if socket has more parts of a multipart message to read - * @return - */ - public function hasReceiveMore():Bool { - if (closed) return false; - var r = getsockopt(ZMQ_RCVMORE); - return (r != null && r.lo == 1); - } - - private static var _hx_zmq_construct_socket = neko.Lib.load("hxzmq", "hx_zmq_construct_socket", 2); - private static var _hx_zmq_close = neko.Lib.load("hxzmq", "hx_zmq_close", 1); - private static var _hx_zmq_bind = neko.Lib.load("hxzmq", "hx_zmq_bind", 2); - private static var _hx_zmq_connect = neko.Lib.load("hxzmq", "hx_zmq_connect", 2); - private static var _hx_zmq_send = neko.Lib.load("hxzmq", "hx_zmq_send", 3); - private static var _hx_zmq_rcv = neko.Lib.load("hxzmq", "hx_zmq_rcv", 2); - private static var _hx_zmq_setintsockopt = neko.Lib.load("hxzmq", "hx_zmq_setintsockopt", 3); - private static var _hx_zmq_setint64sockopt = neko.Lib.load("hxzmq", "hx_zmq_setint64sockopt", 4); - private static var _hx_zmq_setbytessockopt = neko.Lib.load("hxzmq", "hx_zmq_setbytessockopt", 3); - private static var _hx_zmq_getintsockopt = neko.Lib.load("hxzmq", "hx_zmq_getintsockopt", 2); - private static var _hx_zmq_getint64sockopt = neko.Lib.load("hxzmq", "hx_zmq_getint64sockopt", 2); - private static var _hx_zmq_getbytessockopt = neko.Lib.load("hxzmq", "hx_zmq_getbytessockopt", 2); - -} +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq; + +import haxe.io.Bytes; +import haxe.io.BytesData; +import neko.Lib; +import neko.Sys; + +import org.zeromq.ZMQ; + +/** + * A 0MQ socket + * + * These objects will generally be created via the socket() method of a ZMQContext object. + * + * Class based on code from pyzmq project + * See: https://github.com/zeromq/pyzmq/blob/master/zmq/core/socket.pyx + */ +class ZMQSocket +{ + + /** Records if socket has been closed */ + public var closed(default,null):Bool; + + /** + * Hold reference to context associated with socket, to stop it being garbage collected + */ + public var context(default,null):ZMQContext; + + /** Opaque data used by hxzmq driver */ + public var _socketHandle(default,null):Dynamic; + + /** + * Constructor. + * + * Creates a new ZMQ socket + * @param context A ZMQ Context previously created + * @param type A ZMQ socket type + */ + public function new(context:ZMQContext, type:SocketType) + { + closed = true; + this.context = context; + try { + _socketHandle = _hx_zmq_construct_socket(context.contextHandle, ZMQ.socketTypeNo(type)); + + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + closed = false; + } + + /** + * Close the socket + * + * This can be called to close the socket by hand. If this is not + * called, the socket will automatically be closed when it is + * garbage collected. + */ + public function close() + { + if (_socketHandle != null && !closed) { + try { + _hx_zmq_close(_socketHandle); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + closed = true; + _socketHandle = null; + } + } + + /** + * Bind a socket to an address + * + * This causes the socket to listen on a network port. Sockets on the + * other side of this connection will use ``Socket.connect(addr)`` to + * connect to this socket. + * + * @param addr The address string. + * This has the form 'protocol://interface:port', + * for example 'tcp://127.0.0.1:5555'. Protocols supported are + * tcp, upd, pgm, inproc and ipc. If the address is unicode, it is + * encoded to utf-8 first. + */ + public function bind(addr:String) + { + if (closed) + throw new ZMQException(ENOTSUP); + + try { + _hx_zmq_bind(_socketHandle, Lib.haxeToNeko(addr)); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + } + + /** + * Connect to a remote ZMQ socket + * + * @param addr The address string + * This has the form 'protocol://interface:port', + * for example 'tcp://127.0.0.1:5555'. Protocols supported are + * tcp, upd, pgm, inproc and ipc. If the address is unicode, it is + * encoded to utf-8 first. + */ + public function connect(addr:String) + { + if (closed) + throw new ZMQException(ENOTSUP); + + try { + _hx_zmq_connect(_socketHandle, Lib.haxeToNeko(addr)); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + } + + /** + * Set socket options. + * + * See the ZMQ documentation for details on specific options: + * http://api.zeromq.org/master:zmq-setsockopt + * + * + * @param option SocketOptionsType (defined in ZMQ.hx) + * @param optval Either Int or String or Bytes + */ + public function setsockopt(option:SocketOptionsType, optval:Dynamic):Void { + + if (closed) + throw new ZMQException(ENOTSUP); + + // Handle 32 bit int options + if (Lambda.exists(ZMQ.intSocketOptionTypes, + function(so) { return so == option; } )) + { + if (!Std.is(optval,Int)) + throw new String("Expected Int, got " + optval); + + try { + _hx_zmq_setintsockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), optval); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + // Handle 64 bit int options + } else if (Lambda.exists(ZMQ.int64SocketOptionTypes, + function(so) { return so == option; } )) + { + var _hi = Reflect.field(optval, "hi"); + var _lo = Reflect.field(optval, "lo"); + if (_hi == null || _lo == null) { + throw new String("Expected ZMQInt64Type, got " + optval); + return null; + } + + try { + _hx_zmq_setint64sockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), _hi, _lo); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + // Handle bytes options + } else if (Lambda.exists(ZMQ.bytesSocketOptionTypes, + function(so) { return so == option; } )) + { + if (!Std.is(optval, Bytes)) { + throw new String("Expected Bytes, got " + optval); + return null; + } + try { + _hx_zmq_setbytessockopt(_socketHandle, ZMQ.socketOptionTypeNo(option), optval.getData() ); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + } else { + throw new ZMQException(EINVAL); + } + return; + } + + /** + * Return a previously set socket option + * + * @param option + * @return + */ + public function getsockopt(option:SocketOptionsType):Dynamic + { + var _optval:Dynamic; + + if (closed) { + throw new ZMQException(ENOTSUP); + return null; + } + + if (Lambda.exists(ZMQ.intSocketOptionTypes, + function(so) { return so == option; } )) + { + + try { + _optval = Lib.nekoToHaxe(_hx_zmq_getintsockopt(_socketHandle, ZMQ.socketOptionTypeNo(option))); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + return null; + } + if (!Std.is(_optval,Int)) { + throw new String("Expected Int, got " + _optval); + return null; + } else { + return _optval; + } + } else if (Lambda.exists(ZMQ.int64SocketOptionTypes, + function(so) { return so == option; } )) + { + + try { + _optval = Lib.nekoToHaxe(_hx_zmq_getint64sockopt(_socketHandle, ZMQ.socketOptionTypeNo(option))); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + return null; + } + var _hi = Reflect.field(_optval, "hi"); + var _lo = Reflect.field(_optval, "lo"); + if (_hi == null || _lo == null) { + throw new String("Expected ZMQInt64Type, got " + _optval); + return null; + } else { + return {hi:_optval.hi, lo:_optval.lo}; + } + }else if (Lambda.exists(ZMQ.bytesSocketOptionTypes, + function(so) { return so == option; } )) + { + + try { + _optval = _hx_zmq_getbytessockopt(_socketHandle, ZMQ.socketOptionTypeNo(option)); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + return null; + } + return Bytes.ofData(_optval); + + } else { + throw new ZMQException(EINVAL); + return null; + } + return null; + + } + + /** + * Send a message on this socket + * + * This queues the message to be sent by the IO thread at a later time. + * + * @param data The content of the message + * @param ?flags Any supported SocketFlag DONTWAIT, SNDMORE + */ + public function sendMsg(data:Bytes, ?flags:SendReceiveFlagType):Void { + + if (closed) { + throw new ZMQException(ENOTSUP); + } + + try { + _hx_zmq_send(_socketHandle, data.getData(), ZMQ.sendReceiveFlagNo(flags)); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + } + + /** + * Receive a message on this socket + * + * Will return either a message, null (if DONTWAIT was used and there was no data received) or a ZMQException + * + * @param ?flags + * @return + */ + public function recvMsg(?flags:SendReceiveFlagType):Bytes { + + if (closed) + throw new ZMQException(ENOTSUP); + + var bytes:BytesData = null; + + try { + bytes = _hx_zmq_rcv(_socketHandle, ZMQ.sendReceiveFlagNo(flags)); + } catch (e:Int) { + throw new ZMQException(ZMQ.errNoToErrorType(e)); + } + + return { + if (bytes == null) { + null; + } else { + Bytes.ofData(bytes); + }; + } + + } + + /** + * Convenience method to test if socket has more parts of a multipart message to read + * @return + */ + public function hasReceiveMore():Bool { + if (closed) return false; + var r = getsockopt(ZMQ_RCVMORE); + return (r != null && r.lo == 1); + } + + private static var _hx_zmq_construct_socket = neko.Lib.load("hxzmq", "hx_zmq_construct_socket", 2); + private static var _hx_zmq_close = neko.Lib.load("hxzmq", "hx_zmq_close", 1); + private static var _hx_zmq_bind = neko.Lib.load("hxzmq", "hx_zmq_bind", 2); + private static var _hx_zmq_connect = neko.Lib.load("hxzmq", "hx_zmq_connect", 2); + private static var _hx_zmq_send = neko.Lib.load("hxzmq", "hx_zmq_send", 3); + private static var _hx_zmq_rcv = neko.Lib.load("hxzmq", "hx_zmq_rcv", 2); + private static var _hx_zmq_setintsockopt = neko.Lib.load("hxzmq", "hx_zmq_setintsockopt", 3); + private static var _hx_zmq_setint64sockopt = neko.Lib.load("hxzmq", "hx_zmq_setint64sockopt", 4); + private static var _hx_zmq_setbytessockopt = neko.Lib.load("hxzmq", "hx_zmq_setbytessockopt", 3); + private static var _hx_zmq_getintsockopt = neko.Lib.load("hxzmq", "hx_zmq_getintsockopt", 2); + private static var _hx_zmq_getint64sockopt = neko.Lib.load("hxzmq", "hx_zmq_getint64sockopt", 2); + private static var _hx_zmq_getbytessockopt = neko.Lib.load("hxzmq", "hx_zmq_getbytessockopt", 2); + +} diff --git a/org/zeromq/guide/HelloWorldClient.hx b/org/zeromq/guide/HelloWorldClient.hx index d5ab2b6..94f5ecf 100644 --- a/org/zeromq/guide/HelloWorldClient.hx +++ b/org/zeromq/guide/HelloWorldClient.hx @@ -1,63 +1,63 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; -import haxe.io.Bytes; -import neko.Lib; -import neko.Sys; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -/** - * Hello World client in Haxe. - */ -class HelloWorldClient -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - var socket:ZMQSocket = context.socket(ZMQ_REQ); - - Lib.println("** HelloWorldClient (see: http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive)"); - - trace ("Connecting to hello world server..."); - socket.connect ("tcp://localhost:5556"); - - // Do 10 requests, waiting each time for a response - for (i in 0...10) { - var requestString = "Hello "; - - // Send the message - trace ("Sending request " + i + " ..."); - socket.sendMsg(Bytes.ofString(requestString)); - - // Wait for the reply - var msg:Bytes = socket.recvMsg(); - - trace ("Received reply " + i + ": [" + msg.toString() + "]"); - - } - - // Shut down socket and context - socket.close(); - context.term(); - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; +import haxe.io.Bytes; +import neko.Lib; +import neko.Sys; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +/** + * Hello World client in Haxe. + */ +class HelloWorldClient +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + var socket:ZMQSocket = context.socket(ZMQ_REQ); + + Lib.println("** HelloWorldClient (see: http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive)"); + + trace ("Connecting to hello world server..."); + socket.connect ("tcp://localhost:5556"); + + // Do 10 requests, waiting each time for a response + for (i in 0...10) { + var requestString = "Hello "; + + // Send the message + trace ("Sending request " + i + " ..."); + socket.sendMsg(Bytes.ofString(requestString)); + + // Wait for the reply + var msg:Bytes = socket.recvMsg(); + + trace ("Received reply " + i + ": [" + msg.toString() + "]"); + + } + + // Shut down socket and context + socket.close(); + context.term(); + } } \ No newline at end of file diff --git a/org/zeromq/guide/HelloWorldServer.hx b/org/zeromq/guide/HelloWorldServer.hx index 1149436..7c076db 100644 --- a/org/zeromq/guide/HelloWorldServer.hx +++ b/org/zeromq/guide/HelloWorldServer.hx @@ -1,69 +1,69 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; -import haxe.io.Bytes; -import neko.Lib; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQException; -import org.zeromq.ZMQSocket; - -/** - * Hello World server in Haxe - * Binds REP to tcp://*:5556 - * Expects "Hello" from client, replies with "World" - * - */ -class HelloWorldServer -{ - - public static function main() { - - var context:ZMQContext = ZMQContext.instance(); - var responder:ZMQSocket = context.socket(ZMQ_REP); - - Lib.println("** HelloWorldServer (see: http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive)"); - - responder.setsockopt(ZMQ_LINGER, 0); - responder.bind("tcp://*:5556"); - - try { - while (true) { - // Wait for next request from client - var request:Bytes = responder.recvMsg(); - - trace ("Received request:" + request.toString()); - - // Do some work - Sys.sleep(1); - - // Send reply back to client - responder.sendMsg(Bytes.ofString("World")); - } - } catch (e:ZMQException) { - trace (e.toString()); - } - responder.close(); - context.term(); - - } - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; +import haxe.io.Bytes; +import neko.Lib; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQException; +import org.zeromq.ZMQSocket; + +/** + * Hello World server in Haxe + * Binds REP to tcp://*:5556 + * Expects "Hello" from client, replies with "World" + * + */ +class HelloWorldServer +{ + + public static function main() { + + var context:ZMQContext = ZMQContext.instance(); + var responder:ZMQSocket = context.socket(ZMQ_REP); + + Lib.println("** HelloWorldServer (see: http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive)"); + + responder.setsockopt(ZMQ_LINGER, 0); + responder.bind("tcp://*:5556"); + + try { + while (true) { + // Wait for next request from client + var request:Bytes = responder.recvMsg(); + + trace ("Received request:" + request.toString()); + + // Do some work + Sys.sleep(1); + + // Send reply back to client + responder.sendMsg(Bytes.ofString("World")); + } + } catch (e:ZMQException) { + trace (e.toString()); + } + responder.close(); + context.term(); + + } + } \ No newline at end of file diff --git a/org/zeromq/guide/Interrupt.hx b/org/zeromq/guide/Interrupt.hx index 5048bf8..25f33ef 100644 --- a/org/zeromq/guide/Interrupt.hx +++ b/org/zeromq/guide/Interrupt.hx @@ -1,70 +1,70 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import haxe.io.Bytes; -import haxe.Stack; -import neko.Lib; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -/** - * Signal Handling - * - * Call - */ -class Interrupt -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - var receiver:ZMQSocket = context.socket(ZMQ_REP); - receiver.bind("tcp://127.0.0.1:5559"); - - Lib.println("** Interrupt (see: http://zguide.zeromq.org/page:all#Handling-Interrupt-Signals)"); - - ZMQ.catchSignals(); - - Lib.println ("\nPress Ctrl+C"); - - while (true) { - // Blocking read, will exit only on an interrupt (Ctrl+C) - - try { - var msg:Bytes = receiver.recvMsg(); - } catch (e:ZMQException) { - if (ZMQ.isInterrupted()) { - trace ("W: interrupt received, killing server ...\n"); - break; - } - - // Handle other errors - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - } - } - // Close up gracefully - receiver.close(); - context.term(); - - - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import haxe.io.Bytes; +import haxe.Stack; +import neko.Lib; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +/** + * Signal Handling + * + * Call + */ +class Interrupt +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + var receiver:ZMQSocket = context.socket(ZMQ_REP); + receiver.bind("tcp://127.0.0.1:5559"); + + Lib.println("** Interrupt (see: http://zguide.zeromq.org/page:all#Handling-Interrupt-Signals)"); + + ZMQ.catchSignals(); + + Lib.println ("\nPress Ctrl+C"); + + while (true) { + // Blocking read, will exit only on an interrupt (Ctrl+C) + + try { + var msg:Bytes = receiver.recvMsg(); + } catch (e:ZMQException) { + if (ZMQ.isInterrupted()) { + trace ("W: interrupt received, killing server ...\n"); + break; + } + + // Handle other errors + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + } + } + // Close up gracefully + receiver.close(); + context.term(); + + + } } \ No newline at end of file diff --git a/org/zeromq/guide/MTServer.hx b/org/zeromq/guide/MTServer.hx index 993d7fc..1ddba5b 100644 --- a/org/zeromq/guide/MTServer.hx +++ b/org/zeromq/guide/MTServer.hx @@ -1,156 +1,156 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import haxe.io.Bytes; -import haxe.Stack; -import neko.Lib; -import neko.Sys; -import neko.vm.Thread; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQPoller; -import org.zeromq.ZMQSocket; - -/** - * Multithreaded Hello World Server - * - * See: http://zguide.zeromq.org/page:all#Multithreading-with-MQ - */ -class MTServer -{ - - static function worker() { - var context:ZMQContext = ZMQContext.instance(); - - // Socket to talk to dispatcher - var responder:ZMQSocket = context.socket(ZMQ_REP); - responder.connect("inproc://workers"); - - ZMQ.catchSignals(); - - while (true) { - - try { - // Wait for next request from client - var request:Bytes = responder.recvMsg(); - - trace ("Received request:" + request.toString()); - - // Do some work - Sys.sleep(1); - - // Send reply back to client - responder.sendMsg(Bytes.ofString("World")); - } catch (e:ZMQException) { - if (ZMQ.isInterrupted()) { - break; - } - trace (e.toString()); - } - } - responder.close(); - return null; - } - - /** - * Implements a reqeust/reply QUEUE broker device - * Returns if poll is interrupted - * @param ctx - * @param frontend - * @param backend - */ - static function queueDevice(ctx:ZMQContext, frontend:ZMQSocket, backend:ZMQSocket) { - - // Initialise pollset - var poller:ZMQPoller = ctx.poller(); - poller.registerSocket(frontend, ZMQ.ZMQ_POLLIN()); - poller.registerSocket(backend, ZMQ.ZMQ_POLLIN()); - - ZMQ.catchSignals(); - - while (true) { - try { - poller.poll(); - if (poller.pollin(1)) { - var more:Bool = true; - while (more) { - // Receive message - var msg = frontend.recvMsg(); - more = frontend.hasReceiveMore(); - - // Broker it - backend.sendMsg(msg, { if (more) SNDMORE else null; } ); - } - } - - if (poller.pollin(2)) { - var more:Bool = true; - while (more) { - // Receive message - var msg = backend.recvMsg(); - more = backend.hasReceiveMore(); - - // Broker it - frontend.sendMsg(msg, { if (more) SNDMORE else null; } ); - } - } - } catch (e:ZMQException) { - if (ZMQ.isInterrupted()) { - break; - } - // Handle other errors - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - - } - - } - - } - public static function main() { - var workerThreads:List = new List(); - - var context:ZMQContext = ZMQContext.instance(); - - Lib.println ("** MTServer (see: http://zguide.zeromq.org/page:all#Multithreading-with-MQ)"); - - // Socket to talk to clients - var clients:ZMQSocket = context.socket(ZMQ_ROUTER); - clients.bind ("tcp://*:5556"); - - // Socket to talk to workers - var workers:ZMQSocket = context.socket(ZMQ_DEALER); - workers.bind ("inproc://workers"); - - // Launch worker thread pool - for (thread_nbr in 0 ... 5) { - workerThreads.add(Thread.create(worker)); - } - - // Invoke request / reply broker (aka QUEUE device) to connect clients to workers - queueDevice(context, clients, workers); - - // Close up shop - clients.close(); - workers.close(); - context.term(); - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import haxe.io.Bytes; +import haxe.Stack; +import neko.Lib; +import neko.Sys; +import neko.vm.Thread; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQPoller; +import org.zeromq.ZMQSocket; + +/** + * Multithreaded Hello World Server + * + * See: http://zguide.zeromq.org/page:all#Multithreading-with-MQ + */ +class MTServer +{ + + static function worker() { + var context:ZMQContext = ZMQContext.instance(); + + // Socket to talk to dispatcher + var responder:ZMQSocket = context.socket(ZMQ_REP); + responder.connect("inproc://workers"); + + ZMQ.catchSignals(); + + while (true) { + + try { + // Wait for next request from client + var request:Bytes = responder.recvMsg(); + + trace ("Received request:" + request.toString()); + + // Do some work + Sys.sleep(1); + + // Send reply back to client + responder.sendMsg(Bytes.ofString("World")); + } catch (e:ZMQException) { + if (ZMQ.isInterrupted()) { + break; + } + trace (e.toString()); + } + } + responder.close(); + return null; + } + + /** + * Implements a reqeust/reply QUEUE broker device + * Returns if poll is interrupted + * @param ctx + * @param frontend + * @param backend + */ + static function queueDevice(ctx:ZMQContext, frontend:ZMQSocket, backend:ZMQSocket) { + + // Initialise pollset + var poller:ZMQPoller = ctx.poller(); + poller.registerSocket(frontend, ZMQ.ZMQ_POLLIN()); + poller.registerSocket(backend, ZMQ.ZMQ_POLLIN()); + + ZMQ.catchSignals(); + + while (true) { + try { + poller.poll(); + if (poller.pollin(1)) { + var more:Bool = true; + while (more) { + // Receive message + var msg = frontend.recvMsg(); + more = frontend.hasReceiveMore(); + + // Broker it + backend.sendMsg(msg, { if (more) SNDMORE else null; } ); + } + } + + if (poller.pollin(2)) { + var more:Bool = true; + while (more) { + // Receive message + var msg = backend.recvMsg(); + more = backend.hasReceiveMore(); + + // Broker it + frontend.sendMsg(msg, { if (more) SNDMORE else null; } ); + } + } + } catch (e:ZMQException) { + if (ZMQ.isInterrupted()) { + break; + } + // Handle other errors + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + + } + + } + + } + public static function main() { + var workerThreads:List = new List(); + + var context:ZMQContext = ZMQContext.instance(); + + Lib.println ("** MTServer (see: http://zguide.zeromq.org/page:all#Multithreading-with-MQ)"); + + // Socket to talk to clients + var clients:ZMQSocket = context.socket(ZMQ_ROUTER); + clients.bind ("tcp://*:5556"); + + // Socket to talk to workers + var workers:ZMQSocket = context.socket(ZMQ_DEALER); + workers.bind ("inproc://workers"); + + // Launch worker thread pool + for (thread_nbr in 0 ... 5) { + workerThreads.add(Thread.create(worker)); + } + + // Invoke request / reply broker (aka QUEUE device) to connect clients to workers + queueDevice(context, clients, workers); + + // Close up shop + clients.close(); + workers.close(); + context.term(); + } } \ No newline at end of file diff --git a/org/zeromq/guide/Run.hx b/org/zeromq/guide/Run.hx index c56d945..ba625f5 100644 --- a/org/zeromq/guide/Run.hx +++ b/org/zeromq/guide/Run.hx @@ -1,105 +1,105 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import neko.io.File; -import neko.io.FileInput; -import neko.Lib; -import neko.Sys; - -import org.zeromq.guide.HelloWorldClient; -import org.zeromq.guide.HelloWorldServer; -import org.zeromq.guide.WUClient; -import org.zeromq.guide.WUServer; -import org.zeromq.guide.TaskVent; -import org.zeromq.guide.TaskWork; -import org.zeromq.guide.TaskSink; -import org.zeromq.guide.Interrupt; - -/** - * Main class that allows any of the implemented Haxe guide programs to run - */ -class Run -{ - - public static function main() - { - var selection:Int; - - Lib.println("** HaXe ZeroMQ Guide program launcher **"); - - if (Sys.args().length > 0) { - selection = Std.parseInt(Sys.args()[0]); - } else { - Lib.println(""); - Lib.println("Programs:"); - - Lib.println("1. HelloWorldClient"); - Lib.println("2. HelloWorldServer"); - Lib.println(""); - Lib.println("3. WUClient"); - Lib.println("4. WUServer"); - Lib.println(""); - Lib.println("5. TaskVent"); - Lib.println("6. TaskWork"); - Lib.println("7. TaskSink"); - Lib.println(""); - Lib.println("11. Interrupt (** Doesn't work on Windows!)"); - Lib.println(""); - Lib.println("12. MTServer"); - - do { - Lib.print("Type number followed by Enter key, or q to quit: "); - var f:FileInput = File.stdin(); - var str:String = f.readLine(); - - if (str.toLowerCase() == "q") { - return; - } - - selection = Std.parseInt(str); - } while (selection == null); - } - - switch (selection) { - case 1: - HelloWorldClient.main(); - case 2: - HelloWorldServer.main(); - case 3: - WUClient.main(); - case 4: - WUServer.main(); - case 5: - TaskVent.main(); - case 6: - TaskWork.main(); - case 7: - TaskSink.main(); - case 11: - Interrupt.main(); - case 12: - MTServer.main(); - default: - Lib.println ("Unknown program number ... exiting"); - } - } - -} +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import neko.io.File; +import neko.io.FileInput; +import neko.Lib; +import neko.Sys; + +import org.zeromq.guide.HelloWorldClient; +import org.zeromq.guide.HelloWorldServer; +import org.zeromq.guide.WUClient; +import org.zeromq.guide.WUServer; +import org.zeromq.guide.TaskVent; +import org.zeromq.guide.TaskWork; +import org.zeromq.guide.TaskSink; +import org.zeromq.guide.Interrupt; + +/** + * Main class that allows any of the implemented Haxe guide programs to run + */ +class Run +{ + + public static function main() + { + var selection:Int; + + Lib.println("** HaXe ZeroMQ Guide program launcher **"); + + if (Sys.args().length > 0) { + selection = Std.parseInt(Sys.args()[0]); + } else { + Lib.println(""); + Lib.println("Programs:"); + + Lib.println("1. HelloWorldClient"); + Lib.println("2. HelloWorldServer"); + Lib.println(""); + Lib.println("3. WUClient"); + Lib.println("4. WUServer"); + Lib.println(""); + Lib.println("5. TaskVent"); + Lib.println("6. TaskWork"); + Lib.println("7. TaskSink"); + Lib.println(""); + Lib.println("11. Interrupt (** Doesn't work on Windows!)"); + Lib.println(""); + Lib.println("12. MTServer"); + + do { + Lib.print("Type number followed by Enter key, or q to quit: "); + var f:FileInput = File.stdin(); + var str:String = f.readLine(); + + if (str.toLowerCase() == "q") { + return; + } + + selection = Std.parseInt(str); + } while (selection == null); + } + + switch (selection) { + case 1: + HelloWorldClient.main(); + case 2: + HelloWorldServer.main(); + case 3: + WUClient.main(); + case 4: + WUServer.main(); + case 5: + TaskVent.main(); + case 6: + TaskWork.main(); + case 7: + TaskSink.main(); + case 11: + Interrupt.main(); + case 12: + MTServer.main(); + default: + Lib.println ("Unknown program number ... exiting"); + } + } + +} diff --git a/org/zeromq/guide/TaskSink.hx b/org/zeromq/guide/TaskSink.hx index 3514bf9..273ab43 100644 --- a/org/zeromq/guide/TaskSink.hx +++ b/org/zeromq/guide/TaskSink.hx @@ -1,73 +1,73 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import haxe.io.Bytes; -import neko.Lib; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -/** - * Task sink in Haxe - * Binds PULL request socket to tcp://localhost:5558 - * Collects results from workers via this socket - * - * See: http://zguide.zeromq.org/page:all#Divide-and-Conquer - * - * Based on http://zguide.zeromq.org/java:tasksink - */ -class TaskSink -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - - Lib.println("** TaskSink (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); - - // Socket to receive messages on - var receiver:ZMQSocket = context.socket(ZMQ_PULL); - receiver.bind("tcp://127.0.0.1:5558"); - - // Wait for start of batch - var msgString = StringTools.trim(receiver.recvMsg().toString()); - - // Start our clock now - var tStart = Sys.time(); - - // Process 100 messages - var task_nbr:Int; - for (task_nbr in 0 ... 100) { - msgString = StringTools.trim(receiver.recvMsg().toString()); - if (task_nbr % 10 == 0) { - Lib.println(":"); // Print a ":" every 10 messages - } else { - Lib.print("."); - } - } - // Calculate and report duation of batch - var tEnd = Sys.time(); - Lib.println("Total elapsed time: " + Math.ceil((tEnd - tStart) * 1000) + " msec"); - - receiver.close(); - context.term(); - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import haxe.io.Bytes; +import neko.Lib; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +/** + * Task sink in Haxe + * Binds PULL request socket to tcp://localhost:5558 + * Collects results from workers via this socket + * + * See: http://zguide.zeromq.org/page:all#Divide-and-Conquer + * + * Based on http://zguide.zeromq.org/java:tasksink + */ +class TaskSink +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + + Lib.println("** TaskSink (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); + + // Socket to receive messages on + var receiver:ZMQSocket = context.socket(ZMQ_PULL); + receiver.bind("tcp://127.0.0.1:5558"); + + // Wait for start of batch + var msgString = StringTools.trim(receiver.recvMsg().toString()); + + // Start our clock now + var tStart = Sys.time(); + + // Process 100 messages + var task_nbr:Int; + for (task_nbr in 0 ... 100) { + msgString = StringTools.trim(receiver.recvMsg().toString()); + if (task_nbr % 10 == 0) { + Lib.println(":"); // Print a ":" every 10 messages + } else { + Lib.print("."); + } + } + // Calculate and report duation of batch + var tEnd = Sys.time(); + Lib.println("Total elapsed time: " + Math.ceil((tEnd - tStart) * 1000) + " msec"); + + receiver.close(); + context.term(); + } } \ No newline at end of file diff --git a/org/zeromq/guide/TaskVent.hx b/org/zeromq/guide/TaskVent.hx index 8936d72..9eb074a 100644 --- a/org/zeromq/guide/TaskVent.hx +++ b/org/zeromq/guide/TaskVent.hx @@ -1,82 +1,82 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import haxe.io.Bytes; -import haxe.Stack; -import neko.Lib; -import neko.io.File; -import neko.io.FileInput; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQException; -import org.zeromq.ZMQSocket; - -/** - * Task ventilator in Haxe - * Binds PUSH socket to tcp://localhost:5557 - * Sends batch of tasks to workers via that socket. - * - * Based on code from: http://zguide.zeromq.org/java:taskvent - */ -class TaskVent -{ - - public static function main() { - - try { - var context:ZMQContext = ZMQContext.instance(); - var sender:ZMQSocket = context.socket(ZMQ_PUSH); - - Lib.println("** TaskVent (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); - - sender.bind("tcp://127.0.0.1:5557"); - - Lib.println("Press Enter when the workers are ready: "); - var f:FileInput = File.stdin(); - var str:String = f.readLine(); - Lib.println("Sending tasks to workers ...\n"); - - // The first message is "0" and signals starts of batch - sender.sendMsg(Bytes.ofString("0")); - - // Send 100 tasks - var totalMsec:Int = 0; // Total expected cost in msec - for (task_nbr in 0 ... 100) { - var workload = Std.random(100) + 1; // Generates 1 to 100 msecs - totalMsec += workload; - Lib.print(workload + "."); - sender.sendMsg(Bytes.ofString(Std.string(workload))); - } - Lib.println("Total expected cost: " + totalMsec + " msec"); - - // Give 0MQ time to deliver - Sys.sleep(1); - - sender.close(); - context.term(); - } catch (e:ZMQException) { - trace("ZMQException #:" + ZMQ.errNoToErrorType(e.errNo) + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - - } - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import haxe.io.Bytes; +import haxe.Stack; +import neko.Lib; +import neko.io.File; +import neko.io.FileInput; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQException; +import org.zeromq.ZMQSocket; + +/** + * Task ventilator in Haxe + * Binds PUSH socket to tcp://localhost:5557 + * Sends batch of tasks to workers via that socket. + * + * Based on code from: http://zguide.zeromq.org/java:taskvent + */ +class TaskVent +{ + + public static function main() { + + try { + var context:ZMQContext = ZMQContext.instance(); + var sender:ZMQSocket = context.socket(ZMQ_PUSH); + + Lib.println("** TaskVent (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); + + sender.bind("tcp://127.0.0.1:5557"); + + Lib.println("Press Enter when the workers are ready: "); + var f:FileInput = File.stdin(); + var str:String = f.readLine(); + Lib.println("Sending tasks to workers ...\n"); + + // The first message is "0" and signals starts of batch + sender.sendMsg(Bytes.ofString("0")); + + // Send 100 tasks + var totalMsec:Int = 0; // Total expected cost in msec + for (task_nbr in 0 ... 100) { + var workload = Std.random(100) + 1; // Generates 1 to 100 msecs + totalMsec += workload; + Lib.print(workload + "."); + sender.sendMsg(Bytes.ofString(Std.string(workload))); + } + Lib.println("Total expected cost: " + totalMsec + " msec"); + + // Give 0MQ time to deliver + Sys.sleep(1); + + sender.close(); + context.term(); + } catch (e:ZMQException) { + trace("ZMQException #:" + ZMQ.errNoToErrorType(e.errNo) + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + + } + } } \ No newline at end of file diff --git a/org/zeromq/guide/TaskWork.hx b/org/zeromq/guide/TaskWork.hx index 289a4f2..880f06d 100644 --- a/org/zeromq/guide/TaskWork.hx +++ b/org/zeromq/guide/TaskWork.hx @@ -1,71 +1,71 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; - -import haxe.io.Bytes; -import neko.Lib; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -/** - * Task worker in Haxe - * Connects PULL socket to tcp://localhost:5557 - * Collects workloads from ventilator via that socket - * Connects PUSH socket to tcp://localhost:5558 - * Sends results to sink via that socket - * - * See: http://zguide.zeromq.org/page:all#Divide-and-Conquer - * - * Based on code from: http://zguide.zeromq.org/java:taskwork - */ -class TaskWork -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - - Lib.println("** TaskWork (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); - - // Socket to receive messages on - var receiver:ZMQSocket = context.socket(ZMQ_PULL); - receiver.connect("tcp://127.0.0.1:5557"); - - // Socket to send messages to - var sender:ZMQSocket = context.socket(ZMQ_PUSH); - sender.connect("tcp://127.0.0.1:5558"); - - // Process tasks forever - while (true) { - var msgString = StringTools.trim(receiver.recvMsg().toString()); - var sec:Float = Std.parseFloat(msgString) / 1000.0; - Lib.print(msgString + "."); - - // Do the work - Sys.sleep(sec); - - // Send results to sink - sender.sendMsg(Bytes.ofString("")); - } - - - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; + +import haxe.io.Bytes; +import neko.Lib; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +/** + * Task worker in Haxe + * Connects PULL socket to tcp://localhost:5557 + * Collects workloads from ventilator via that socket + * Connects PUSH socket to tcp://localhost:5558 + * Sends results to sink via that socket + * + * See: http://zguide.zeromq.org/page:all#Divide-and-Conquer + * + * Based on code from: http://zguide.zeromq.org/java:taskwork + */ +class TaskWork +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + + Lib.println("** TaskWork (see: http://zguide.zeromq.org/page:all#Divide-and-Conquer)"); + + // Socket to receive messages on + var receiver:ZMQSocket = context.socket(ZMQ_PULL); + receiver.connect("tcp://127.0.0.1:5557"); + + // Socket to send messages to + var sender:ZMQSocket = context.socket(ZMQ_PUSH); + sender.connect("tcp://127.0.0.1:5558"); + + // Process tasks forever + while (true) { + var msgString = StringTools.trim(receiver.recvMsg().toString()); + var sec:Float = Std.parseFloat(msgString) / 1000.0; + Lib.print(msgString + "."); + + // Do the work + Sys.sleep(sec); + + // Send results to sink + sender.sendMsg(Bytes.ofString("")); + } + + + } } \ No newline at end of file diff --git a/org/zeromq/guide/WUClient.hx b/org/zeromq/guide/WUClient.hx index c609648..5130ea7 100644 --- a/org/zeromq/guide/WUClient.hx +++ b/org/zeromq/guide/WUClient.hx @@ -1,89 +1,89 @@ -/** - * (c) $(CopyrightDate) Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; -import haxe.io.Bytes; -import neko.Lib; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQException; -import org.zeromq.ZMQSocket; - -/** - * Weather update client in Haxe - * Connects SUB socket to tcp://localhost:5556 - * Collects weather updates and finds average temp in zipcode - * - * Use optional argument to specify zip code (in range 1 to 100000) - * - * See: http://zguide.zeromq.org/page:all#Getting-the-Message-Out - */ -class WUClient -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - - Lib.println("** WUClient (see: http://zguide.zeromq.org/page:all#Getting-the-Message-Out)"); - - // Socket to talk to server - trace ("Collecting updates from weather server..."); - var subscriber:ZMQSocket = context.socket(ZMQ_SUB); - subscriber.setsockopt(ZMQ_LINGER, 0); // Don't block when closing socket at end - - subscriber.connect("tcp://localhost:5556"); - - // Subscribe to zipcode, default in NYC, 10001 - var filter:String = - if (Sys.args().length > 0) { - Sys.args()[0]; - } else { - "10001"; - }; - - try { - subscriber.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString(filter)); - } catch (e:ZMQException) { - trace (e.str()); - } - - // Process 100 updates - var update_nbr = 0; - var total_temp:Int = 0; - for (update_nbr in 0...100) { - var msg:Bytes = subscriber.recvMsg(); - trace (update_nbr+ ". Received: " + msg.toString()); - - var zipcode, temperature, relhumidity; - - var sscanf:Array = msg.toString().split(" "); - zipcode = sscanf[0]; - temperature = sscanf[1]; - relhumidity = sscanf[2]; - total_temp += Std.parseInt(temperature); - - } - trace ("Average temperature for zipcode " + filter + " was " + total_temp / 100); - - // Close gracefully - subscriber.close(); - context.term(); - } +/** + * (c) $(CopyrightDate) Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; +import haxe.io.Bytes; +import neko.Lib; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQException; +import org.zeromq.ZMQSocket; + +/** + * Weather update client in Haxe + * Connects SUB socket to tcp://localhost:5556 + * Collects weather updates and finds average temp in zipcode + * + * Use optional argument to specify zip code (in range 1 to 100000) + * + * See: http://zguide.zeromq.org/page:all#Getting-the-Message-Out + */ +class WUClient +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + + Lib.println("** WUClient (see: http://zguide.zeromq.org/page:all#Getting-the-Message-Out)"); + + // Socket to talk to server + trace ("Collecting updates from weather server..."); + var subscriber:ZMQSocket = context.socket(ZMQ_SUB); + subscriber.setsockopt(ZMQ_LINGER, 0); // Don't block when closing socket at end + + subscriber.connect("tcp://localhost:5556"); + + // Subscribe to zipcode, default in NYC, 10001 + var filter:String = + if (Sys.args().length > 0) { + Sys.args()[0]; + } else { + "10001"; + }; + + try { + subscriber.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString(filter)); + } catch (e:ZMQException) { + trace (e.str()); + } + + // Process 100 updates + var update_nbr = 0; + var total_temp:Int = 0; + for (update_nbr in 0...100) { + var msg:Bytes = subscriber.recvMsg(); + trace (update_nbr+ ". Received: " + msg.toString()); + + var zipcode, temperature, relhumidity; + + var sscanf:Array = msg.toString().split(" "); + zipcode = sscanf[0]; + temperature = sscanf[1]; + relhumidity = sscanf[2]; + total_temp += Std.parseInt(temperature); + + } + trace ("Average temperature for zipcode " + filter + " was " + total_temp / 100); + + // Close gracefully + subscriber.close(); + context.term(); + } } \ No newline at end of file diff --git a/org/zeromq/guide/WUServer.hx b/org/zeromq/guide/WUServer.hx index f76d98f..a668124 100644 --- a/org/zeromq/guide/WUServer.hx +++ b/org/zeromq/guide/WUServer.hx @@ -1,58 +1,58 @@ -/** - * (c) $(CopyrightDate) Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.guide; -import haxe.io.Bytes; -import neko.Lib; -import neko.Random; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -/** - * Weather update server in Haxe - * Binds PUB socket to tcp://*:5556 - * Publishes random weather updates - * - * See: http://zguide.zeromq.org/page:all#Getting-the-Message-Out - */ -class WUServer -{ - - public static function main() { - var context:ZMQContext = ZMQContext.instance(); - - Lib.println("** WUServer (see: http://zguide.zeromq.org/page:all#Getting-the-Message-Out)"); - - var publisher:ZMQSocket = context.socket(ZMQ_PUB); - publisher.bind("tcp://127.0.0.1:5556"); - - while (true) { - // Get values that will fool the boss - var zipcode, temperature, relhumidity; - zipcode = Std.random(100000) + 1; - temperature = Std.random(215) - 80 + 1; - relhumidity = Std.random(50) + 10 + 1; - - // Send message to all subscribers - var update:String = zipcode + " " + temperature + " " + relhumidity; - publisher.sendMsg(Bytes.ofString(update)); - } - } +/** + * (c) $(CopyrightDate) Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.guide; +import haxe.io.Bytes; +import neko.Lib; +import neko.Random; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +/** + * Weather update server in Haxe + * Binds PUB socket to tcp://*:5556 + * Publishes random weather updates + * + * See: http://zguide.zeromq.org/page:all#Getting-the-Message-Out + */ +class WUServer +{ + + public static function main() { + var context:ZMQContext = ZMQContext.instance(); + + Lib.println("** WUServer (see: http://zguide.zeromq.org/page:all#Getting-the-Message-Out)"); + + var publisher:ZMQSocket = context.socket(ZMQ_PUB); + publisher.bind("tcp://127.0.0.1:5556"); + + while (true) { + // Get values that will fool the boss + var zipcode, temperature, relhumidity; + zipcode = Std.random(100000) + 1; + temperature = Std.random(215) - 80 + 1; + relhumidity = Std.random(50) + 10 + 1; + + // Send message to all subscribers + var update:String = zipcode + " " + temperature + " " + relhumidity; + publisher.sendMsg(Bytes.ofString(update)); + } + } } \ No newline at end of file diff --git a/org/zeromq/test/BaseTest.hx b/org/zeromq/test/BaseTest.hx index dc7777b..7f5df55 100644 --- a/org/zeromq/test/BaseTest.hx +++ b/org/zeromq/test/BaseTest.hx @@ -1,160 +1,160 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.io.Bytes; -import haxe.unit.TestCase; -import haxe.PosInfos; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; -import org.zeromq.ZMQException; - -/** - * Utility methods. - * - * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/__init__.py - */ -class BaseTest extends TestCase -{ - - private var _context:ZMQContext; - private var _sockets:List; - - public override function setup():Void { - _context = ZMQContext.instance(); - _sockets = new List(); - } - - public override function tearDown():Void { - var _contexts:List = new List(); - var s:ZMQSocket; - var _ctx:ZMQContext; - - _contexts.add(_context); - for (s in _sockets) { - _contexts.add(s.context); - s.close(); - } - _sockets = null; - for (_ctx in _contexts) { - _ctx.term(); - } - - } - - /** - * Create a bound ZMQ socket pair using a random port - * @param type1 - * @param type2 - * @param ?interface - */ - public function createBoundPair(type1:SocketType, type2:SocketType, ?iface:String = 'tcp://127.0.0.1'): SocketPair { - if (_context == null || _context.closed) { - assertTrue(false); - return null; - } - - var randomPort:Int = Math.round(Math.random() * 18000) + 2000; - var _s1:ZMQSocket = _context.socket(type1); - _s1.setsockopt(ZMQ_LINGER, 0); - var _p1 = bindToRandomPort(_s1, iface); - var _s2:ZMQSocket = _context.socket(type2); - _s2.setsockopt(ZMQ_LINGER, 0); - _s2.connect(iface + ":" + _p1); - _sockets.add(_s1); - _sockets.add(_s2); - return { s1:_s1, s2:_s2 }; - } - - /** - * Send a message from p.s1 to p.s2, then send it back again from s2 to s1 - * @param p SocketPair - * @param msg original message to ping pong - * @return ping-ponged message - */ - public function ping_pong(p:SocketPair, msg:Bytes):Bytes { - //trace ("s1.sendMsg:"+msg.toString()); - p.s1.sendMsg(msg); - //trace ("s2.recvMsg"); - var msg2:Bytes = p.s2.recvMsg(); - //trace ("s2.sendMsg:"+msg2.toString()); - p.s2.sendMsg(msg2); - //trace ("s1.recvMsg"); - var msg3:Bytes = p.s1.recvMsg(); - //trace ("return"); - return msg3; - } - - /** - * Binds a socket to a random port in range - * @param s - * @param addr - * @param ?minPort = 2000 - * @param ?maxPort = 20000 - * @param ?maxTries = 100 - * @return - */ - private function bindToRandomPort(s:ZMQSocket, addr:String, ?minPort = 2000, ?maxPort = 20000, ?maxTries = 100):Int { - var _iter = new IntIter(1, maxTries); - var _port = minPort; - - for (i in _iter) { - try { - _port = Math.round(Math.random() * (maxPort - minPort)) + minPort; - s.bind(addr + ":" + _port); - return _port; - } catch (e:ZMQException) { - // Ignore error. Go round and try another port - } - } - throw new String("Could not bind socket to random port"); - return null; - } - - function assertRaisesZMQException(fn:Void->Void, err:ErrorType, ?c : PosInfos):Void { - - var res = ""; - - currentTest.done = true; - try { - fn(); - res = "no exception"; - } - catch (e:ZMQException) { - if (e.err == err) { - assertTrue(true); - return; - } - res = "ZMQException errNo:" + e.errNo + ", str:" + e.str(); - } catch (e:Dynamic) { - res = e; - } - - currentTest.success = false; - currentTest.error = "expected "+err+" but got "+res+" instead"; - currentTest.posInfos = c; - throw currentTest; - - } -} - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.io.Bytes; +import haxe.unit.TestCase; +import haxe.PosInfos; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; +import org.zeromq.ZMQException; + +/** + * Utility methods. + * + * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/__init__.py + */ +class BaseTest extends TestCase +{ + + private var _context:ZMQContext; + private var _sockets:List; + + public override function setup():Void { + _context = ZMQContext.instance(); + _sockets = new List(); + } + + public override function tearDown():Void { + var _contexts:List = new List(); + var s:ZMQSocket; + var _ctx:ZMQContext; + + _contexts.add(_context); + for (s in _sockets) { + _contexts.add(s.context); + s.close(); + } + _sockets = null; + for (_ctx in _contexts) { + _ctx.term(); + } + + } + + /** + * Create a bound ZMQ socket pair using a random port + * @param type1 + * @param type2 + * @param ?interface + */ + public function createBoundPair(type1:SocketType, type2:SocketType, ?iface:String = 'tcp://127.0.0.1'): SocketPair { + if (_context == null || _context.closed) { + assertTrue(false); + return null; + } + + var randomPort:Int = Math.round(Math.random() * 18000) + 2000; + var _s1:ZMQSocket = _context.socket(type1); + _s1.setsockopt(ZMQ_LINGER, 0); + var _p1 = bindToRandomPort(_s1, iface); + var _s2:ZMQSocket = _context.socket(type2); + _s2.setsockopt(ZMQ_LINGER, 0); + _s2.connect(iface + ":" + _p1); + _sockets.add(_s1); + _sockets.add(_s2); + return { s1:_s1, s2:_s2 }; + } + + /** + * Send a message from p.s1 to p.s2, then send it back again from s2 to s1 + * @param p SocketPair + * @param msg original message to ping pong + * @return ping-ponged message + */ + public function ping_pong(p:SocketPair, msg:Bytes):Bytes { + //trace ("s1.sendMsg:"+msg.toString()); + p.s1.sendMsg(msg); + //trace ("s2.recvMsg"); + var msg2:Bytes = p.s2.recvMsg(); + //trace ("s2.sendMsg:"+msg2.toString()); + p.s2.sendMsg(msg2); + //trace ("s1.recvMsg"); + var msg3:Bytes = p.s1.recvMsg(); + //trace ("return"); + return msg3; + } + + /** + * Binds a socket to a random port in range + * @param s + * @param addr + * @param ?minPort = 2000 + * @param ?maxPort = 20000 + * @param ?maxTries = 100 + * @return + */ + private function bindToRandomPort(s:ZMQSocket, addr:String, ?minPort = 2000, ?maxPort = 20000, ?maxTries = 100):Int { + var _iter = new IntIter(1, maxTries); + var _port = minPort; + + for (i in _iter) { + try { + _port = Math.round(Math.random() * (maxPort - minPort)) + minPort; + s.bind(addr + ":" + _port); + return _port; + } catch (e:ZMQException) { + // Ignore error. Go round and try another port + } + } + throw new String("Could not bind socket to random port"); + return null; + } + + function assertRaisesZMQException(fn:Void->Void, err:ErrorType, ?c : PosInfos):Void { + + var res = ""; + + currentTest.done = true; + try { + fn(); + res = "no exception"; + } + catch (e:ZMQException) { + if (e.err == err) { + assertTrue(true); + return; + } + res = "ZMQException errNo:" + e.errNo + ", str:" + e.str(); + } catch (e:Dynamic) { + res = e; + } + + currentTest.success = false; + currentTest.error = "expected "+err+" but got "+res+" instead"; + currentTest.posInfos = c; + throw currentTest; + + } +} + typedef SocketPair = { s1:ZMQSocket, s2:ZMQSocket }; \ No newline at end of file diff --git a/org/zeromq/test/TestAll.hx b/org/zeromq/test/TestAll.hx index 412ff28..801dd4d 100644 --- a/org/zeromq/test/TestAll.hx +++ b/org/zeromq/test/TestAll.hx @@ -1,46 +1,46 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.unit.TestRunner; -import org.zeromq.test.TestContext; -import org.zeromq.test.TestVersion; - -class TestAll -{ - - public static function main() { - - var runner:TestRunner = new TestRunner(); - - // Define tests - runner.add(new TestVersion()); - runner.add(new TestContext()); - runner.add(new TestError()); - runner.add(new TestSocket()); - runner.add(new TestPubSub()); - runner.add(new TestMultiPartMessage()); - runner.add(new TestReqRep()); - runner.add(new TestPoller()); - - // Run - runner.run(); - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.unit.TestRunner; +import org.zeromq.test.TestContext; +import org.zeromq.test.TestVersion; + +class TestAll +{ + + public static function main() { + + var runner:TestRunner = new TestRunner(); + + // Define tests + runner.add(new TestVersion()); + runner.add(new TestContext()); + runner.add(new TestError()); + runner.add(new TestSocket()); + runner.add(new TestPubSub()); + runner.add(new TestMultiPartMessage()); + runner.add(new TestReqRep()); + runner.add(new TestPoller()); + + // Run + runner.run(); + } } \ No newline at end of file diff --git a/org/zeromq/test/TestContext.hx b/org/zeromq/test/TestContext.hx index d6cef3d..440ef54 100644 --- a/org/zeromq/test/TestContext.hx +++ b/org/zeromq/test/TestContext.hx @@ -1,114 +1,114 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQException; -import org.zeromq.ZMQPoller; -import org.zeromq.ZMQSocket; - -/** - * ZMQ.ZMQContext class tests - * - * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_context.py - */ -class TestContext extends BaseTest -{ - - public function testInit() - { - var c1 = ZMQContext.instance(); - assertTrue(Std.is(c1, ZMQContext)); - c1 = null; - - var c2 = ZMQContext.instance(); - assertTrue(Std.is(c2, ZMQContext)); - c2 = null; - - var c3 = ZMQContext.instance(); - assertTrue(Std.is(c3, ZMQContext)); - c3 = null; - } - - public function testTerm() - { - var c1:ZMQContext = ZMQContext.instance(); - c1.term(); - assertTrue(c1.closed); - } - - public function testFailInit() { - - var c1:ZMQContext; - assertRaisesZMQException(function() c1 = ZMQContext.instance(0),EINVAL); - } - - public function testInstance() { - var ctx = ZMQContext.instance(); - var c2 = ZMQContext.instance(2); - assertTrue(Std.is(c2, ZMQContext)); - assertTrue(c2 == ctx); - c2.term(); - - var c3:ZMQContext = ZMQContext.instance(); - var c4:ZMQContext = ZMQContext.instance(); - assertFalse(c3 == c2); - assertFalse(c3.closed); - assertTrue(c3 == c4); - } - - public function testSocket() { - var ctx:ZMQContext = ZMQContext.instance(); - var s:ZMQSocket = ctx.socket(ZMQ_PUB); - - assertTrue(s != null); - assertFalse(s.closed); - s.close(); - - ctx.term(); - assertRaisesZMQException(function() s = ctx.socket(ZMQ_PUB), ENOTSUP); - try { - s = ctx.socket(ZMQ_PUB); - assertTrue(false); - } catch (e:ZMQException) { - assertTrue(e.err == ENOTSUP); - } - - } - - public function testPoller() { - var ctx:ZMQContext = ZMQContext.instance(); - var p:ZMQPoller = ctx.poller(); - - assertTrue(p != null); - - ctx.term(); - assertRaisesZMQException(function() p = ctx.poller(), ENOTSUP); - } - - public override function setup():Void { - // No setup needed for these tests - } - - public override function tearDown():Void { - // No tearDown needed for these tests - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQException; +import org.zeromq.ZMQPoller; +import org.zeromq.ZMQSocket; + +/** + * ZMQ.ZMQContext class tests + * + * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_context.py + */ +class TestContext extends BaseTest +{ + + public function testInit() + { + var c1 = ZMQContext.instance(); + assertTrue(Std.is(c1, ZMQContext)); + c1 = null; + + var c2 = ZMQContext.instance(); + assertTrue(Std.is(c2, ZMQContext)); + c2 = null; + + var c3 = ZMQContext.instance(); + assertTrue(Std.is(c3, ZMQContext)); + c3 = null; + } + + public function testTerm() + { + var c1:ZMQContext = ZMQContext.instance(); + c1.term(); + assertTrue(c1.closed); + } + + public function testFailInit() { + + var c1:ZMQContext; + assertRaisesZMQException(function() c1 = ZMQContext.instance(0),EINVAL); + } + + public function testInstance() { + var ctx = ZMQContext.instance(); + var c2 = ZMQContext.instance(2); + assertTrue(Std.is(c2, ZMQContext)); + assertTrue(c2 == ctx); + c2.term(); + + var c3:ZMQContext = ZMQContext.instance(); + var c4:ZMQContext = ZMQContext.instance(); + assertFalse(c3 == c2); + assertFalse(c3.closed); + assertTrue(c3 == c4); + } + + public function testSocket() { + var ctx:ZMQContext = ZMQContext.instance(); + var s:ZMQSocket = ctx.socket(ZMQ_PUB); + + assertTrue(s != null); + assertFalse(s.closed); + s.close(); + + ctx.term(); + assertRaisesZMQException(function() s = ctx.socket(ZMQ_PUB), ENOTSUP); + try { + s = ctx.socket(ZMQ_PUB); + assertTrue(false); + } catch (e:ZMQException) { + assertTrue(e.err == ENOTSUP); + } + + } + + public function testPoller() { + var ctx:ZMQContext = ZMQContext.instance(); + var p:ZMQPoller = ctx.poller(); + + assertTrue(p != null); + + ctx.term(); + assertRaisesZMQException(function() p = ctx.poller(), ENOTSUP); + } + + public override function setup():Void { + // No setup needed for these tests + } + + public override function tearDown():Void { + // No tearDown needed for these tests + } } \ No newline at end of file diff --git a/org/zeromq/test/TestError.hx b/org/zeromq/test/TestError.hx index 3a680fc..b89d4a8 100644 --- a/org/zeromq/test/TestError.hx +++ b/org/zeromq/test/TestError.hx @@ -1,54 +1,54 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQException; - -class TestError extends BaseTest -{ - - public function testStrError() { - - var iter = new IntIter(1, 10); - for (i in iter) { - var s = ZMQ.strError(i); - assertTrue(Std.is(s, String)); - } - } - - public function testZMQException() { - - var e:ZMQException = new ZMQException(EINVAL); - assertTrue(e.errNo == ZMQ.errorTypeToErrNo(EINVAL)); - assertTrue(e.str() == ZMQ.strError(ZMQ.errorTypeToErrNo(EINVAL))); - - assertTrue(ZMQ.errNoToErrorType(ZMQ.errorTypeToErrNo(EINVAL)) == EINVAL); - - } - public override function setup():Void { - // No setup needed for these tests - } - - public override function tearDown():Void { - // No tearDown needed for these tests - } - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQException; + +class TestError extends BaseTest +{ + + public function testStrError() { + + var iter = new IntIter(1, 10); + for (i in iter) { + var s = ZMQ.strError(i); + assertTrue(Std.is(s, String)); + } + } + + public function testZMQException() { + + var e:ZMQException = new ZMQException(EINVAL); + assertTrue(e.errNo == ZMQ.errorTypeToErrNo(EINVAL)); + assertTrue(e.str() == ZMQ.strError(ZMQ.errorTypeToErrNo(EINVAL))); + + assertTrue(ZMQ.errNoToErrorType(ZMQ.errorTypeToErrNo(EINVAL)) == EINVAL); + + } + public override function setup():Void { + // No setup needed for these tests + } + + public override function tearDown():Void { + // No tearDown needed for these tests + } + } \ No newline at end of file diff --git a/org/zeromq/test/TestInterrupt.hx b/org/zeromq/test/TestInterrupt.hx index f5c7507..86feb9a 100644 --- a/org/zeromq/test/TestInterrupt.hx +++ b/org/zeromq/test/TestInterrupt.hx @@ -1,55 +1,55 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; -import haxe.io.Bytes; -import neko.Sys; -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; - -class TestInterrupt extends BaseTest -{ - - public function testInterrupt() { - - var context:ZMQContext = ZMQContext.instance(); - var receiver:ZMQSocket = context.socket(ZMQ_REP); - receiver.bind("tcp://127.0.0.1:5559"); - trace ("About to call catchSignals"); - ZMQ.catchSignals(); - - print("\nPress Ctrl+C"); - - while (true) { - // Blocking read, will exit only on an interrupt (Ctrl+C) - var msg:Bytes = receiver.recvMsg(); - - if (ZMQ.isInterrupted()) { - assertTrue(true); - break; - } - // Should not get here - assertTrue(false); - } - // Close up gracefully - receiver.close(); - context.term(); - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; +import haxe.io.Bytes; +import neko.Sys; +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; + +class TestInterrupt extends BaseTest +{ + + public function testInterrupt() { + + var context:ZMQContext = ZMQContext.instance(); + var receiver:ZMQSocket = context.socket(ZMQ_REP); + receiver.bind("tcp://127.0.0.1:5559"); + trace ("About to call catchSignals"); + ZMQ.catchSignals(); + + print("\nPress Ctrl+C"); + + while (true) { + // Blocking read, will exit only on an interrupt (Ctrl+C) + var msg:Bytes = receiver.recvMsg(); + + if (ZMQ.isInterrupted()) { + assertTrue(true); + break; + } + // Should not get here + assertTrue(false); + } + // Close up gracefully + receiver.close(); + context.term(); + } } \ No newline at end of file diff --git a/org/zeromq/test/TestMultiPartMessage.hx b/org/zeromq/test/TestMultiPartMessage.hx index d1b2285..fa63b4f 100644 --- a/org/zeromq/test/TestMultiPartMessage.hx +++ b/org/zeromq/test/TestMultiPartMessage.hx @@ -1,64 +1,64 @@ -/** - * (c) $(CopyrightDate) Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.io.Bytes; -import haxe.Stack; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQException; -import org.zeromq.test.BaseTest; - -class TestMultiPartMessage extends BaseTest -{ - - public function testMultiPartMessage() - { - var pair:SocketPair = null; - - try { - pair = createBoundPair(ZMQ_REQ, ZMQ_REP); - pair.s1.setsockopt(ZMQ_LINGER, 0); - pair.s2.setsockopt(ZMQ_LINGER, 0); - - var msg1:Bytes = Bytes.ofString("message1"); - var msg2:Bytes = Bytes.ofString("message2"); - var msg3:Bytes = Bytes.ofString("message3"); - pair.s1.sendMsg(msg1, SNDMORE); // 1st part of multipart message - pair.s1.sendMsg(msg2, SNDMORE); // 2nd part of multipart message - pair.s1.sendMsg(msg3); // last part of multipart message - - // Receive multipart message - var i:Int = 1; - do { - var b:Bytes = pair.s2.recvMsg(); // Blocking call - assertEquals(Std.string(i), b.toString().charAt(7)); - i++; - } while (pair.s2.hasReceiveMore()); - assertTrue(i == 4); // 3 parts received - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.toString()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - +/** + * (c) $(CopyrightDate) Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.io.Bytes; +import haxe.Stack; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQException; +import org.zeromq.test.BaseTest; + +class TestMultiPartMessage extends BaseTest +{ + + public function testMultiPartMessage() + { + var pair:SocketPair = null; + + try { + pair = createBoundPair(ZMQ_REQ, ZMQ_REP); + pair.s1.setsockopt(ZMQ_LINGER, 0); + pair.s2.setsockopt(ZMQ_LINGER, 0); + + var msg1:Bytes = Bytes.ofString("message1"); + var msg2:Bytes = Bytes.ofString("message2"); + var msg3:Bytes = Bytes.ofString("message3"); + pair.s1.sendMsg(msg1, SNDMORE); // 1st part of multipart message + pair.s1.sendMsg(msg2, SNDMORE); // 2nd part of multipart message + pair.s1.sendMsg(msg3); // last part of multipart message + + // Receive multipart message + var i:Int = 1; + do { + var b:Bytes = pair.s2.recvMsg(); // Blocking call + assertEquals(Std.string(i), b.toString().charAt(7)); + i++; + } while (pair.s2.hasReceiveMore()); + assertTrue(i == 4); // 3 parts received + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.toString()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + } \ No newline at end of file diff --git a/org/zeromq/test/TestPoller.hx b/org/zeromq/test/TestPoller.hx index a2a1049..1b8e8a1 100644 --- a/org/zeromq/test/TestPoller.hx +++ b/org/zeromq/test/TestPoller.hx @@ -1,193 +1,193 @@ -/** - * (c) $(CopyrightDate) Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.io.Bytes; -import neko.Sys; -import haxe.Stack; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQPoller; -import org.zeromq.ZMQException; - -import org.zeromq.test.BaseTest; - -class TestPoller extends BaseTest -{ - - public function testRegistering() { - - var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - - // Create Poller object - var poller:ZMQPoller = new ZMQPoller(); - assertTrue(poller != null); - - poller.registerSocket(pair.s2, ZMQ.ZMQ_POLLIN()); - poller.registerSocket(pair.s1, ZMQ.ZMQ_POLLOUT()); - assertEquals(2, poller.getSize()); - - assertEquals(true, poller.unregisterSocket(pair.s2)); - assertEquals(1, poller.getSize()); - - poller.unregisterAllSockets(); - assertEquals(0, poller.getSize()); - } - - public function testPollingPairPair() { - - var pair:SocketPair = createBoundPair(ZMQ_PAIR, ZMQ_PAIR); - var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); - try { - Sys.sleep(0.1); // Allow sockets time to connect - - var poller:ZMQPoller = new ZMQPoller(); - poller.registerSocket(pair.s1, pollinout); - poller.registerSocket(pair.s2, pollinout); - - var numSocks = poller.poll(); - assertEquals(2, numSocks); - assertEquals(2, poller.revents.length); - assertTrue(poller.pollout(1)); // PAIR socket s1 should be ready for writing - assertTrue(poller.pollout(2)); // PAIR socket s2 should be ready for writing - - // Now do a send on both, wait and test for POLLOUT|POLLIN - pair.s1.sendMsg(Bytes.ofString("msg1")); - pair.s2.sendMsg(Bytes.ofString("msg2")); - Sys.sleep(0.1); - - numSocks = poller.poll(); - assertTrue(poller.pollin(1) && poller.pollout(1)); // PAIR socket s1 should be ready for reading & writing - assertTrue(poller.pollin(2) && poller.pollout(2)); // PAIR socket s2 should be reasy for reading & writing - - // Make sure both are in POLLOUT after recv - var msg1 = pair.s1.recvMsg(); - var msg2 = pair.s2.recvMsg(); - numSocks = poller.poll(); - assertTrue(poller.pollout(1)); // PAIR socket s1 should be ready for writing - assertTrue(poller.pollout(2)); // PAIR socket s2 should be ready for writing - - poller.unregisterAllSockets(); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - - public function testPollingReqRep() { - - var pair:SocketPair = createBoundPair(ZMQ_REP, ZMQ_REQ); - var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); - try { - Sys.sleep(0.1); // Allow sockets time to connect - - var poller:ZMQPoller = new ZMQPoller(); - poller.registerSocket(pair.s1, pollinout); - poller.registerSocket(pair.s2, pollinout); - - var numSocks = poller.poll(); - assertEquals(1, numSocks); // Only one revent bitmask with an event - assertEquals(2, poller.getSize()); - assertTrue(poller.noevents(1)); // REP socket s1 no events - assertTrue(poller.pollout(2)); // REQ socket s2 should be ready for writing - - // Make sure s2 REQ socket immediately goes into state 0 after send - pair.s2.sendMsg(Bytes.ofString("msg1")); - numSocks = poller.poll(); - assertTrue(poller.noevents(2)); - - // Make sure that s1 goes into POLLIN state after a sleep() - Sys.sleep(0.1); - numSocks = poller.poll(); - assertTrue(poller.pollin(1)); - - // Make sure s1 REP socket goes into POLLOUT after recv - var msg1 = pair.s1.recvMsg(); - numSocks = poller.poll(); - assertTrue(poller.pollout(1)); - - // Make sure s1 REP socket immediately goes into state 0 after send - pair.s1.sendMsg(Bytes.ofString("msg2")); - numSocks = poller.poll(); - assertTrue(poller.noevents(1)); - - // Make sure that s2 goes into POLLIN state after a sleep() - Sys.sleep(0.1); - numSocks = poller.poll(); - assertTrue(poller.pollin(2)); - - // Make sure s2 REQ socket goes into POLLOUT after recv - var msg1 = pair.s2.recvMsg(); - numSocks = poller.poll(); - assertTrue(poller.pollout(2)); - - poller.unregisterAllSockets(); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - } - - public function testPollingPubSub() { - var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); - try { - pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("")); - Sys.sleep(0.1); // Allow sockets time to connect - - var poller:ZMQPoller = new ZMQPoller(); - poller.registerSocket(pair.s1, pollinout); - poller.registerSocket(pair.s2, ZMQ.ZMQ_POLLIN()); - - // Makes sure only first is send ready - var numSocks = poller.poll(); - assertTrue(poller.pollout(1)); - assertTrue(poller.noevents(2)); - - // Make sure s1 stays in POLLOUT after send - pair.s1.sendMsg(Bytes.ofString("msg1")); - numSocks = poller.poll(); - assertTrue(poller.pollout(1)); - - // Make sure SUB s2 is ready for reading - Sys.sleep(0.1); - numSocks = poller.poll(); - assertTrue(poller.pollin(2)); - - var msg2:Bytes = pair.s2.recvMsg(); - numSocks = poller.poll(); - assertTrue(poller.noevents(2)); - - poller.unregisterAllSockets(); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - - } +/** + * (c) $(CopyrightDate) Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.io.Bytes; +import neko.Sys; +import haxe.Stack; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQPoller; +import org.zeromq.ZMQException; + +import org.zeromq.test.BaseTest; + +class TestPoller extends BaseTest +{ + + public function testRegistering() { + + var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + + // Create Poller object + var poller:ZMQPoller = new ZMQPoller(); + assertTrue(poller != null); + + poller.registerSocket(pair.s2, ZMQ.ZMQ_POLLIN()); + poller.registerSocket(pair.s1, ZMQ.ZMQ_POLLOUT()); + assertEquals(2, poller.getSize()); + + assertEquals(true, poller.unregisterSocket(pair.s2)); + assertEquals(1, poller.getSize()); + + poller.unregisterAllSockets(); + assertEquals(0, poller.getSize()); + } + + public function testPollingPairPair() { + + var pair:SocketPair = createBoundPair(ZMQ_PAIR, ZMQ_PAIR); + var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); + try { + Sys.sleep(0.1); // Allow sockets time to connect + + var poller:ZMQPoller = new ZMQPoller(); + poller.registerSocket(pair.s1, pollinout); + poller.registerSocket(pair.s2, pollinout); + + var numSocks = poller.poll(); + assertEquals(2, numSocks); + assertEquals(2, poller.revents.length); + assertTrue(poller.pollout(1)); // PAIR socket s1 should be ready for writing + assertTrue(poller.pollout(2)); // PAIR socket s2 should be ready for writing + + // Now do a send on both, wait and test for POLLOUT|POLLIN + pair.s1.sendMsg(Bytes.ofString("msg1")); + pair.s2.sendMsg(Bytes.ofString("msg2")); + Sys.sleep(0.1); + + numSocks = poller.poll(); + assertTrue(poller.pollin(1) && poller.pollout(1)); // PAIR socket s1 should be ready for reading & writing + assertTrue(poller.pollin(2) && poller.pollout(2)); // PAIR socket s2 should be reasy for reading & writing + + // Make sure both are in POLLOUT after recv + var msg1 = pair.s1.recvMsg(); + var msg2 = pair.s2.recvMsg(); + numSocks = poller.poll(); + assertTrue(poller.pollout(1)); // PAIR socket s1 should be ready for writing + assertTrue(poller.pollout(2)); // PAIR socket s2 should be ready for writing + + poller.unregisterAllSockets(); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + + public function testPollingReqRep() { + + var pair:SocketPair = createBoundPair(ZMQ_REP, ZMQ_REQ); + var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); + try { + Sys.sleep(0.1); // Allow sockets time to connect + + var poller:ZMQPoller = new ZMQPoller(); + poller.registerSocket(pair.s1, pollinout); + poller.registerSocket(pair.s2, pollinout); + + var numSocks = poller.poll(); + assertEquals(1, numSocks); // Only one revent bitmask with an event + assertEquals(2, poller.getSize()); + assertTrue(poller.noevents(1)); // REP socket s1 no events + assertTrue(poller.pollout(2)); // REQ socket s2 should be ready for writing + + // Make sure s2 REQ socket immediately goes into state 0 after send + pair.s2.sendMsg(Bytes.ofString("msg1")); + numSocks = poller.poll(); + assertTrue(poller.noevents(2)); + + // Make sure that s1 goes into POLLIN state after a sleep() + Sys.sleep(0.1); + numSocks = poller.poll(); + assertTrue(poller.pollin(1)); + + // Make sure s1 REP socket goes into POLLOUT after recv + var msg1 = pair.s1.recvMsg(); + numSocks = poller.poll(); + assertTrue(poller.pollout(1)); + + // Make sure s1 REP socket immediately goes into state 0 after send + pair.s1.sendMsg(Bytes.ofString("msg2")); + numSocks = poller.poll(); + assertTrue(poller.noevents(1)); + + // Make sure that s2 goes into POLLIN state after a sleep() + Sys.sleep(0.1); + numSocks = poller.poll(); + assertTrue(poller.pollin(2)); + + // Make sure s2 REQ socket goes into POLLOUT after recv + var msg1 = pair.s2.recvMsg(); + numSocks = poller.poll(); + assertTrue(poller.pollout(2)); + + poller.unregisterAllSockets(); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + } + + public function testPollingPubSub() { + var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + var pollinout:Int = ZMQ.ZMQ_POLLIN() | ZMQ.ZMQ_POLLOUT(); + try { + pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("")); + Sys.sleep(0.1); // Allow sockets time to connect + + var poller:ZMQPoller = new ZMQPoller(); + poller.registerSocket(pair.s1, pollinout); + poller.registerSocket(pair.s2, ZMQ.ZMQ_POLLIN()); + + // Makes sure only first is send ready + var numSocks = poller.poll(); + assertTrue(poller.pollout(1)); + assertTrue(poller.noevents(2)); + + // Make sure s1 stays in POLLOUT after send + pair.s1.sendMsg(Bytes.ofString("msg1")); + numSocks = poller.poll(); + assertTrue(poller.pollout(1)); + + // Make sure SUB s2 is ready for reading + Sys.sleep(0.1); + numSocks = poller.poll(); + assertTrue(poller.pollin(2)); + + var msg2:Bytes = pair.s2.recvMsg(); + numSocks = poller.poll(); + assertTrue(poller.noevents(2)); + + poller.unregisterAllSockets(); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + + } } \ No newline at end of file diff --git a/org/zeromq/test/TestPubSub.hx b/org/zeromq/test/TestPubSub.hx index 636117a..7299961 100644 --- a/org/zeromq/test/TestPubSub.hx +++ b/org/zeromq/test/TestPubSub.hx @@ -1,91 +1,91 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.io.Bytes; -import haxe.Stack; -import neko.Sys; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQSocket; -import org.zeromq.ZMQException; -import org.zeromq.test.BaseTest; - -class TestPubSub extends BaseTest -{ - - public function testBasicPubSub() { - - try { - var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - _sockets.add(pair.s1); - _sockets.add(pair.s2); - - // Subscribe to everything - pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("")); - - Sys.sleep(0.1); - pair.s1.sendMsg(Bytes.ofString("foo")); - var msg:Bytes = pair.s2.recvMsg(); // this is a blocking call - assertTrue(msg.toString() == "foo"); - - var a:Bytes = Bytes.alloc(1); - for (i in 0...10) { - a.set(0, i); - pair.s1.sendMsg(a); - msg = pair.s2.recvMsg(); - assertTrue(msg.length == 1); - assertTrue(msg.get(0) == i); - } - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - - public function testTopic() { - try { - var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - _sockets.add(pair.s1); - _sockets.add(pair.s2); - - var b:Bytes = Bytes.ofString("x"); - pair.s2.setsockopt(ZMQ_SUBSCRIBE, b); - Sys.sleep(0.1); // make sure subscriber gets first message published by publisher - - pair.s1.sendMsg(Bytes.ofString("message")); // send message that shouldnt meet the subscribe filter - pair.s1.sendMsg(Bytes.ofString("xmessage")); // send message that should meet the subscribe filter - var msg:Bytes = pair.s2.recvMsg(DONTWAIT); // This is a non-blocking call - assertTrue(msg == null); - - msg = pair.s2.recvMsg(); // this is a blocking call - assertTrue(msg != null); - assertTrue(msg.toString() == "xmessage"); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.io.Bytes; +import haxe.Stack; +import neko.Sys; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQSocket; +import org.zeromq.ZMQException; +import org.zeromq.test.BaseTest; + +class TestPubSub extends BaseTest +{ + + public function testBasicPubSub() { + + try { + var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + _sockets.add(pair.s1); + _sockets.add(pair.s2); + + // Subscribe to everything + pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("")); + + Sys.sleep(0.1); + pair.s1.sendMsg(Bytes.ofString("foo")); + var msg:Bytes = pair.s2.recvMsg(); // this is a blocking call + assertTrue(msg.toString() == "foo"); + + var a:Bytes = Bytes.alloc(1); + for (i in 0...10) { + a.set(0, i); + pair.s1.sendMsg(a); + msg = pair.s2.recvMsg(); + assertTrue(msg.length == 1); + assertTrue(msg.get(0) == i); + } + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + + public function testTopic() { + try { + var pair:SocketPair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + _sockets.add(pair.s1); + _sockets.add(pair.s2); + + var b:Bytes = Bytes.ofString("x"); + pair.s2.setsockopt(ZMQ_SUBSCRIBE, b); + Sys.sleep(0.1); // make sure subscriber gets first message published by publisher + + pair.s1.sendMsg(Bytes.ofString("message")); // send message that shouldnt meet the subscribe filter + pair.s1.sendMsg(Bytes.ofString("xmessage")); // send message that should meet the subscribe filter + var msg:Bytes = pair.s2.recvMsg(DONTWAIT); // This is a non-blocking call + assertTrue(msg == null); + + msg = pair.s2.recvMsg(); // this is a blocking call + assertTrue(msg != null); + assertTrue(msg.toString() == "xmessage"); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + } } \ No newline at end of file diff --git a/org/zeromq/test/TestReqRep.hx b/org/zeromq/test/TestReqRep.hx index 8277402..6386d30 100644 --- a/org/zeromq/test/TestReqRep.hx +++ b/org/zeromq/test/TestReqRep.hx @@ -1,125 +1,125 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.io.Bytes; -import StringTools; -import haxe.Stack; -import neko.Sys; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQSocket; -import org.zeromq.ZMQException; -import org.zeromq.test.BaseTest; - -/** - * Test class for REQ and REP socket types. - * - * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_reqrep.py - */ -class TestReqRep extends BaseTest -{ - - public function TODOtestBasicReqRep() { - var pair:SocketPair; - try { - pair = createBoundPair(ZMQ_REQ, ZMQ_REP); - var msg1:Bytes = Bytes.ofString("message1"); - - var msg2:Bytes = ping_pong(pair, msg1); - assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - - public function testMultiple() { - var pair:SocketPair; - var msg1,msg2:Bytes; - - try { - pair = createBoundPair(ZMQ_REQ, ZMQ_REP); - for (i in 9...12) { - msg1 = Bytes.ofString(i + " "); - msg2 = ping_pong(pair, msg1); - assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); - } - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - } - - public function testBadSendRcv() { - var pair:SocketPair; - try { - pair = createBoundPair(ZMQ_REQ, ZMQ_REP); - - //TODO: Why is this raising a Invalid argument (mutex.hpp:98) ereror at runtime? - assertRaisesZMQException(function() { pair.s1.recvMsg(); }, EFSM); // Try receiving on a REQ socket - assertRaisesZMQException(function() { pair.s2.sendMsg(Bytes.ofString("foo")); }, EFSM); // Try sending on a REP socket before receiving request - - // Added to prevent an abort trap being raised when we run the test - // See: https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_reqrep.py - var msg1:Bytes = Bytes.ofString("message1"); - var msg2:Bytes = ping_pong(pair, msg1); - assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - - public function testLargeMessage() { - var pair:SocketPair; - try { - pair = createBoundPair(ZMQ_REQ, ZMQ_REP); - - var b:StringBuf = new StringBuf(); - - //TODO: Fails when set >= 6922 - for (c in 0...1000) { - b.add("x"); - } - - var msg1:Bytes = Bytes.ofString(b.toString()); - for (i in 0...10) { - var msg2:Bytes = ping_pong(pair, msg1); - assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); - } - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - } - - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.io.Bytes; +import StringTools; +import haxe.Stack; +import neko.Sys; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQSocket; +import org.zeromq.ZMQException; +import org.zeromq.test.BaseTest; + +/** + * Test class for REQ and REP socket types. + * + * Based on https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_reqrep.py + */ +class TestReqRep extends BaseTest +{ + + public function TODOtestBasicReqRep() { + var pair:SocketPair; + try { + pair = createBoundPair(ZMQ_REQ, ZMQ_REP); + var msg1:Bytes = Bytes.ofString("message1"); + + var msg2:Bytes = ping_pong(pair, msg1); + assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + + public function testMultiple() { + var pair:SocketPair; + var msg1,msg2:Bytes; + + try { + pair = createBoundPair(ZMQ_REQ, ZMQ_REP); + for (i in 9...12) { + msg1 = Bytes.ofString(i + " "); + msg2 = ping_pong(pair, msg1); + assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); + } + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + } + + public function testBadSendRcv() { + var pair:SocketPair; + try { + pair = createBoundPair(ZMQ_REQ, ZMQ_REP); + + //TODO: Why is this raising a Invalid argument (mutex.hpp:98) ereror at runtime? + assertRaisesZMQException(function() { pair.s1.recvMsg(); }, EFSM); // Try receiving on a REQ socket + assertRaisesZMQException(function() { pair.s2.sendMsg(Bytes.ofString("foo")); }, EFSM); // Try sending on a REP socket before receiving request + + // Added to prevent an abort trap being raised when we run the test + // See: https://github.com/zeromq/pyzmq/blob/master/zmq/tests/test_reqrep.py + var msg1:Bytes = Bytes.ofString("message1"); + var msg2:Bytes = ping_pong(pair, msg1); + assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + + public function testLargeMessage() { + var pair:SocketPair; + try { + pair = createBoundPair(ZMQ_REQ, ZMQ_REP); + + var b:StringBuf = new StringBuf(); + + //TODO: Fails when set >= 6922 + for (c in 0...1000) { + b.add("x"); + } + + var msg1:Bytes = Bytes.ofString(b.toString()); + for (i in 0...10) { + var msg2:Bytes = ping_pong(pair, msg1); + assertTrue(StringTools.startsWith(msg2.toString(),msg1.toString())); + } + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + } + + } \ No newline at end of file diff --git a/org/zeromq/test/TestSocket.hx b/org/zeromq/test/TestSocket.hx index 3f4610b..2b9e806 100644 --- a/org/zeromq/test/TestSocket.hx +++ b/org/zeromq/test/TestSocket.hx @@ -1,190 +1,190 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import haxe.Stack; -import haxe.io.Bytes; - -import org.zeromq.ZMQ; -import org.zeromq.ZMQContext; -import org.zeromq.ZMQSocket; -import org.zeromq.ZMQException; -import org.zeromq.test.BaseTest; - -/** - * Haxe test class focused on the ZMQSocket haxe binding class - * - * Assumes a baseline ZMQ version of 2.1.4 (ie. some tests may not work if run against any earlier versions of the libzmq library) - */ -class TestSocket extends BaseTest -{ - - public function testCreate() { - var ctx:ZMQContext = ZMQContext.instance(); - var s:ZMQSocket = ctx.socket(ZMQ_PUB); - - assertTrue(s != null); - assertFalse(s.closed); - - // Test bind to invalid protocol - assertRaisesZMQException(function() s.bind("ftl://a"), EPROTONOSUPPORT); - assertRaisesZMQException(function() s.bind("tcp://"),EINVAL); - assertRaisesZMQException(function() s.connect("ftl://a"),EPROTONOSUPPORT); - - s.close(); - - assertTrue(s.closed); - - } - - public function testClose() { - var ctx:ZMQContext = ZMQContext.instance(); - var s:ZMQSocket = ctx.socket(ZMQ_PUB); - s.close(); - assertTrue(s.closed); - - assertRaisesZMQException(function() s.bind("") , ENOTSUP); - assertRaisesZMQException(function() s.connect(""), ENOTSUP); - } - - public function testBoundPair() { - var pair:SocketPair = null; - - try { - pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - assertTrue(pair != null); - assertTrue(!pair.s1.closed); - assertTrue(!pair.s2.closed); - - } - - public function testIntSocketOptions() { - var pair:SocketPair = null; - - try { - pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - pair.s1.setsockopt(ZMQ_LINGER, 0); - assertTrue(pair.s1.getsockopt(ZMQ_LINGER) == 0); - pair.s1.setsockopt(ZMQ_LINGER, -1); - assertTrue(pair.s1.getsockopt(ZMQ_LINGER) == -1); - var r:Int = pair.s1.getsockopt(ZMQ_EVENTS); - assertEquals(ZMQ.ZMQ_POLLOUT(), r); - assertRaisesZMQException(function() { pair.s1.setsockopt(ZMQ_EVENTS, 2 ^ 7 - 1); }, EINVAL); - assertEquals(ZMQ.socketTypeNo(ZMQ_PUB), pair.s1.getsockopt(ZMQ_TYPE)); - assertEquals(ZMQ.socketTypeNo(ZMQ_SUB), pair.s2.getsockopt(ZMQ_TYPE)); - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - - public function testInt64SocketOptions() { - var pair:SocketPair = null; - var r:ZMQInt64Type = null; - - try { - pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - pair.s1.setsockopt(ZMQ_LINGER, 0); - r = pair.s1.getsockopt(ZMQ_HWM); - assertTrue(r.lo == 0); // Test default HWM is 0 for a new socket - assertTrue(r.hi == 0); - pair.s2.setsockopt(ZMQ_HWM, { hi:128, lo:128 } ); // (128 * 2^32)+128 - var r:ZMQInt64Type = pair.s2.getsockopt(ZMQ_HWM); - assertTrue(r.lo == 128); - assertTrue(r.hi == 128); - - r = pair.s1.getsockopt(ZMQ_AFFINITY); - assertEquals(0, r.lo); - - r = pair.s1.getsockopt(ZMQ_SWAP); - assertTrue(r.lo == 0); - assertTrue(r.hi == 0); - pair.s1.setsockopt(ZMQ_SWAP, { hi:1, lo:255 } ); // (1 * 2^32) + 255 bytes - r = pair.s1.getsockopt(ZMQ_SWAP); - assertTrue(r.lo == 255); - assertTrue(r.hi == 1); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - } - - public function testBytesSocketOptions() { - var pair:SocketPair = null; - - try { - pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); - pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("foo")); - - // Test that you cannot retrieve a previously sefined SUBSCRIBE option value using getsockopt - // See: http://api.zeromq.org/2-1-3:zmq-getsockopt - assertRaisesZMQException(function() pair.s2.getsockopt(ZMQ_SUBSCRIBE),EINVAL); - - pair.s1.setsockopt(ZMQ_IDENTITY, Bytes.ofString("Socket1")); - var b:Bytes = pair.s1.getsockopt(ZMQ_IDENTITY); - assertEquals("Socket1", b.toString()); - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - } - public function testSendBasic() { - - try { - var pair:SocketPair = createBoundPair(ZMQ_PAIR, ZMQ_PAIR); - pair.s1.setsockopt(ZMQ_LINGER, 0); - pair.s2.setsockopt(ZMQ_LINGER, 0); - pair.s1.sendMsg(Bytes.ofString("foo")); - var msg:Bytes = pair.s2.recvMsg(); - assertTrue(msg.toString() == "foo"); - - - var a:Bytes = Bytes.alloc(1); - for (i in 0...10) { - a.set(0, i); - pair.s1.sendMsg(a); - msg = pair.s2.recvMsg(); - assertTrue(msg.length == 1); - assertTrue(msg.get(0) == i); - } - - } catch (e:ZMQException) { - trace("ZMQException #:" + e.errNo + ", str:" + e.str()); - trace (Stack.toString(Stack.exceptionStack())); - assertTrue(false); - } - - - } - - - +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import haxe.Stack; +import haxe.io.Bytes; + +import org.zeromq.ZMQ; +import org.zeromq.ZMQContext; +import org.zeromq.ZMQSocket; +import org.zeromq.ZMQException; +import org.zeromq.test.BaseTest; + +/** + * Haxe test class focused on the ZMQSocket haxe binding class + * + * Assumes a baseline ZMQ version of 2.1.4 (ie. some tests may not work if run against any earlier versions of the libzmq library) + */ +class TestSocket extends BaseTest +{ + + public function testCreate() { + var ctx:ZMQContext = ZMQContext.instance(); + var s:ZMQSocket = ctx.socket(ZMQ_PUB); + + assertTrue(s != null); + assertFalse(s.closed); + + // Test bind to invalid protocol + assertRaisesZMQException(function() s.bind("ftl://a"), EPROTONOSUPPORT); + assertRaisesZMQException(function() s.bind("tcp://"),EINVAL); + assertRaisesZMQException(function() s.connect("ftl://a"),EPROTONOSUPPORT); + + s.close(); + + assertTrue(s.closed); + + } + + public function testClose() { + var ctx:ZMQContext = ZMQContext.instance(); + var s:ZMQSocket = ctx.socket(ZMQ_PUB); + s.close(); + assertTrue(s.closed); + + assertRaisesZMQException(function() s.bind("") , ENOTSUP); + assertRaisesZMQException(function() s.connect(""), ENOTSUP); + } + + public function testBoundPair() { + var pair:SocketPair = null; + + try { + pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + assertTrue(pair != null); + assertTrue(!pair.s1.closed); + assertTrue(!pair.s2.closed); + + } + + public function testIntSocketOptions() { + var pair:SocketPair = null; + + try { + pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + pair.s1.setsockopt(ZMQ_LINGER, 0); + assertTrue(pair.s1.getsockopt(ZMQ_LINGER) == 0); + pair.s1.setsockopt(ZMQ_LINGER, -1); + assertTrue(pair.s1.getsockopt(ZMQ_LINGER) == -1); + var r:Int = pair.s1.getsockopt(ZMQ_EVENTS); + assertEquals(ZMQ.ZMQ_POLLOUT(), r); + assertRaisesZMQException(function() { pair.s1.setsockopt(ZMQ_EVENTS, 2 ^ 7 - 1); }, EINVAL); + assertEquals(ZMQ.socketTypeNo(ZMQ_PUB), pair.s1.getsockopt(ZMQ_TYPE)); + assertEquals(ZMQ.socketTypeNo(ZMQ_SUB), pair.s2.getsockopt(ZMQ_TYPE)); + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + + public function testInt64SocketOptions() { + var pair:SocketPair = null; + var r:ZMQInt64Type = null; + + try { + pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + pair.s1.setsockopt(ZMQ_LINGER, 0); + r = pair.s1.getsockopt(ZMQ_HWM); + assertTrue(r.lo == 0); // Test default HWM is 0 for a new socket + assertTrue(r.hi == 0); + pair.s2.setsockopt(ZMQ_HWM, { hi:128, lo:128 } ); // (128 * 2^32)+128 + var r:ZMQInt64Type = pair.s2.getsockopt(ZMQ_HWM); + assertTrue(r.lo == 128); + assertTrue(r.hi == 128); + + r = pair.s1.getsockopt(ZMQ_AFFINITY); + assertEquals(0, r.lo); + + r = pair.s1.getsockopt(ZMQ_SWAP); + assertTrue(r.lo == 0); + assertTrue(r.hi == 0); + pair.s1.setsockopt(ZMQ_SWAP, { hi:1, lo:255 } ); // (1 * 2^32) + 255 bytes + r = pair.s1.getsockopt(ZMQ_SWAP); + assertTrue(r.lo == 255); + assertTrue(r.hi == 1); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + } + + public function testBytesSocketOptions() { + var pair:SocketPair = null; + + try { + pair = createBoundPair(ZMQ_PUB, ZMQ_SUB); + pair.s2.setsockopt(ZMQ_SUBSCRIBE, Bytes.ofString("foo")); + + // Test that you cannot retrieve a previously sefined SUBSCRIBE option value using getsockopt + // See: http://api.zeromq.org/2-1-3:zmq-getsockopt + assertRaisesZMQException(function() pair.s2.getsockopt(ZMQ_SUBSCRIBE),EINVAL); + + pair.s1.setsockopt(ZMQ_IDENTITY, Bytes.ofString("Socket1")); + var b:Bytes = pair.s1.getsockopt(ZMQ_IDENTITY); + assertEquals("Socket1", b.toString()); + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + } + public function testSendBasic() { + + try { + var pair:SocketPair = createBoundPair(ZMQ_PAIR, ZMQ_PAIR); + pair.s1.setsockopt(ZMQ_LINGER, 0); + pair.s2.setsockopt(ZMQ_LINGER, 0); + pair.s1.sendMsg(Bytes.ofString("foo")); + var msg:Bytes = pair.s2.recvMsg(); + assertTrue(msg.toString() == "foo"); + + + var a:Bytes = Bytes.alloc(1); + for (i in 0...10) { + a.set(0, i); + pair.s1.sendMsg(a); + msg = pair.s2.recvMsg(); + assertTrue(msg.length == 1); + assertTrue(msg.get(0) == i); + } + + } catch (e:ZMQException) { + trace("ZMQException #:" + e.errNo + ", str:" + e.str()); + trace (Stack.toString(Stack.exceptionStack())); + assertTrue(false); + } + + + } + + + } \ No newline at end of file diff --git a/org/zeromq/test/TestVersion.hx b/org/zeromq/test/TestVersion.hx index fea3dba..3ea21bd 100644 --- a/org/zeromq/test/TestVersion.hx +++ b/org/zeromq/test/TestVersion.hx @@ -1,56 +1,56 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - -package org.zeromq.test; - -import org.zeromq.ZMQ; - -class TestVersion extends BaseTest -{ - - public function testVersionMethods() - { - var _major = ZMQ.versionMajor(); - assertTrue(_major >= 2); - - var _minor = ZMQ.versionMinor(); - assertTrue(_minor >= 0); - - var _patch = ZMQ.versionPatch(); - assertTrue(_patch >= 0); - - var _version = ZMQ.version_full(); - trace ("version_full:" + _version); - assertTrue(_version >= 0); - - } - - public function testMakeVersion() - { - assertTrue(ZMQ.makeVersion(1, 2, 3) == 10203); - } - - public override function setup():Void { - // No setup needed for these tests - } - - public override function tearDown():Void { - // No tearDown needed for these tests - } +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + +package org.zeromq.test; + +import org.zeromq.ZMQ; + +class TestVersion extends BaseTest +{ + + public function testVersionMethods() + { + var _major = ZMQ.versionMajor(); + assertTrue(_major >= 2); + + var _minor = ZMQ.versionMinor(); + assertTrue(_minor >= 0); + + var _patch = ZMQ.versionPatch(); + assertTrue(_patch >= 0); + + var _version = ZMQ.version_full(); + trace ("version_full:" + _version); + assertTrue(_version >= 0); + + } + + public function testMakeVersion() + { + assertTrue(ZMQ.makeVersion(1, 2, 3) == 10203); + } + + public override function setup():Void { + // No setup needed for these tests + } + + public override function tearDown():Void { + // No tearDown needed for these tests + } } \ No newline at end of file diff --git a/src/Poller.cpp b/src/Poller.cpp index 6b7ff71..26e7250 100644 --- a/src/Poller.cpp +++ b/src/Poller.cpp @@ -1,101 +1,101 @@ -/* - Copyright (c) Richard Smith 2011 - - This file is part of hxzmq. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see . -*/ - -#include -#include -#include -#include - - -#include "socket.h" - -value hx_zmq_poll (value sockets_, value events_, value timeout_) { - - if (!val_is_int(timeout_) || !val_is_array(sockets_) || !val_is_array(events_)) { - val_throw(alloc_int(EINVAL)); - return alloc_null(); - } - - //value *sockets_array; - //value *events_array; - //sockets_array = val_array_ptr( sockets_ ); - //events_array = val_array_ptr ( events_ ); - - int ls = val_array_size(sockets_); - int le = val_array_size(events_); - - if (ls != le) { - val_throw(alloc_int(EINVAL)); - return alloc_null(); - } - - zmq_pollitem_t *pitem = new zmq_pollitem_t [ls]; - for (int i = 0; i < ls; i++) { - value socket = val_array_i(sockets_, i); - value event = val_array_i(events_, i); - - // Test that array index values are of the expected value type - val_check_kind(socket, k_zmq_socket_handle); - if (!val_is_int(event)) { - val_throw(alloc_int(EINVAL)); - delete [] pitem; - return alloc_null(); - } - - pitem [i].socket = val_data (socket); - pitem [i].fd = 0; - pitem [i].events = val_int (event); - pitem [i].revents = 0; - } - - gc_enter_blocking(); - - int rc = 0; - int err = 0; - long tout = val_int(timeout_); - - rc = zmq_poll (pitem, ls, tout); - err = zmq_errno(); - - gc_exit_blocking(); - - if (rc == -1) { - val_throw(alloc_int(err)); - delete [] pitem; - return alloc_null(); - } - - // return results - value retObj = alloc_empty_object (); - alloc_field( retObj, val_id("_ret"), alloc_int(rc)); - // build returned _revents int array - value haxe_revents = alloc_array(ls); - for (int i = 0; i < ls; i++) { - val_array_set_i (haxe_revents, i, alloc_int(pitem[i].revents)); - } - alloc_field( retObj, val_id("_revents"), haxe_revents); - - delete [] pitem; - pitem = NULL; - - return retObj; - -} - +/* + Copyright (c) Richard Smith 2011 + + This file is part of hxzmq. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the Lesser GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + Lesser GNU General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + + +#include "socket.h" + +value hx_zmq_poll (value sockets_, value events_, value timeout_) { + + if (!val_is_int(timeout_) || !val_is_array(sockets_) || !val_is_array(events_)) { + val_throw(alloc_int(EINVAL)); + return alloc_null(); + } + + //value *sockets_array; + //value *events_array; + //sockets_array = val_array_ptr( sockets_ ); + //events_array = val_array_ptr ( events_ ); + + int ls = val_array_size(sockets_); + int le = val_array_size(events_); + + if (ls != le) { + val_throw(alloc_int(EINVAL)); + return alloc_null(); + } + + zmq_pollitem_t *pitem = new zmq_pollitem_t [ls]; + for (int i = 0; i < ls; i++) { + value socket = val_array_i(sockets_, i); + value event = val_array_i(events_, i); + + // Test that array index values are of the expected value type + val_check_kind(socket, k_zmq_socket_handle); + if (!val_is_int(event)) { + val_throw(alloc_int(EINVAL)); + delete [] pitem; + return alloc_null(); + } + + pitem [i].socket = val_data (socket); + pitem [i].fd = 0; + pitem [i].events = val_int (event); + pitem [i].revents = 0; + } + + gc_enter_blocking(); + + int rc = 0; + int err = 0; + long tout = val_int(timeout_); + + rc = zmq_poll (pitem, ls, tout); + err = zmq_errno(); + + gc_exit_blocking(); + + if (rc == -1) { + val_throw(alloc_int(err)); + delete [] pitem; + return alloc_null(); + } + + // return results + value retObj = alloc_empty_object (); + alloc_field( retObj, val_id("_ret"), alloc_int(rc)); + // build returned _revents int array + value haxe_revents = alloc_array(ls); + for (int i = 0; i < ls; i++) { + val_array_set_i (haxe_revents, i, alloc_int(pitem[i].revents)); + } + alloc_field( retObj, val_id("_revents"), haxe_revents); + + delete [] pitem; + pitem = NULL; + + return retObj; + +} + DEFINE_PRIM (hx_zmq_poll, 3); \ No newline at end of file diff --git a/src/socket.h b/src/socket.h index 04139d4..30d6203 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,24 +1,24 @@ -/** - * (c) 2011 Richard J Smith - * - * This file is part of hxzmq - * - * hxzmq is free software; you can redistribute it and/or modify it under - * the terms of the Lesser GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * hxzmq is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Lesser GNU General Public License for more details. - * - * You should have received a copy of the Lesser GNU General Public License - * along with this program. If not, see . - */ - - -#include - -// Define a Kind type name for ZMQ socket handles, which are opaque to the Haxe layer -DECLARE_KIND(k_zmq_socket_handle); +/** + * (c) 2011 Richard J Smith + * + * This file is part of hxzmq + * + * hxzmq is free software; you can redistribute it and/or modify it under + * the terms of the Lesser GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * hxzmq is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Lesser GNU General Public License for more details. + * + * You should have received a copy of the Lesser GNU General Public License + * along with this program. If not, see . + */ + + +#include + +// Define a Kind type name for ZMQ socket handles, which are opaque to the Haxe layer +DECLARE_KIND(k_zmq_socket_handle);