PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/JAwaPI/src/org/jeromq/ZMQ.java

https://github.com/s-faychatelard/API
Java | 1721 lines | 716 code | 231 blank | 774 comment | 49 complexity | d5ee8a52f3d8ab9d6a83471a55ab69fd MD5 | raw file
  1. /*
  2. Copyright (c) 1991-2011 iMatix Corporation <www.imatix.com>
  3. Copyright other contributors as noted in the AUTHORS file.
  4. This file is part of 0MQ.
  5. 0MQ is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. 0MQ is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package org.jeromq;
  17. import java.io.IOException;
  18. import java.nio.ByteBuffer;
  19. import java.nio.channels.SelectableChannel;
  20. import java.nio.channels.Selector;
  21. import zmq.Ctx;
  22. import zmq.DecoderBase;
  23. import zmq.EncoderBase;
  24. import zmq.SocketBase;
  25. import zmq.ZError;
  26. /**
  27. * @deprecated use org.zeromq namespace
  28. */
  29. public class ZMQ {
  30. /**
  31. * Socket flag to indicate that more message parts are coming.
  32. */
  33. public static final int SNDMORE = zmq.ZMQ.ZMQ_SNDMORE;
  34. // Values for flags in Socket's send and recv functions.
  35. /**
  36. * Socket flag to indicate a nonblocking send or recv mode.
  37. */
  38. public static final int DONTWAIT = zmq.ZMQ.ZMQ_DONTWAIT;
  39. public static final int NOBLOCK = zmq.ZMQ.ZMQ_DONTWAIT;
  40. // Socket types, used when creating a Socket.
  41. /**
  42. * Flag to specify a exclusive pair of sockets.
  43. */
  44. public static final int PAIR = zmq.ZMQ.ZMQ_PAIR;
  45. /**
  46. * Flag to specify a PUB socket, receiving side must be a SUB or XSUB.
  47. */
  48. public static final int PUB = zmq.ZMQ.ZMQ_PUB;
  49. /**
  50. * Flag to specify the receiving part of the PUB or XPUB socket.
  51. */
  52. public static final int SUB = zmq.ZMQ.ZMQ_SUB;
  53. /**
  54. * Flag to specify a REQ socket, receiving side must be a REP.
  55. */
  56. public static final int REQ = zmq.ZMQ.ZMQ_REQ;
  57. /**
  58. * Flag to specify the receiving part of a REQ socket.
  59. */
  60. public static final int REP = zmq.ZMQ.ZMQ_REP;
  61. /**
  62. * Flag to specify a DEALER socket (aka XREQ).
  63. * DEALER is really a combined ventilator / sink
  64. * that does load-balancing on output and fair-queuing on input
  65. * with no other semantics. It is the only socket type that lets
  66. * you shuffle messages out to N nodes and shuffle the replies
  67. * back, in a raw bidirectional asynch pattern.
  68. */
  69. public static final int DEALER = zmq.ZMQ.ZMQ_DEALER;
  70. /**
  71. * Old alias for DEALER flag.
  72. * Flag to specify a XREQ socket, receiving side must be a XREP.
  73. *
  74. * @deprecated As of release 3.0 of zeromq, replaced by {@link #DEALER}
  75. */
  76. public static final int XREQ = DEALER;
  77. /**
  78. * Flag to specify ROUTER socket (aka XREP).
  79. * ROUTER is the socket that creates and consumes request-reply
  80. * routing envelopes. It is the only socket type that lets you route
  81. * messages to specific connections if you know their identities.
  82. */
  83. public static final int ROUTER = zmq.ZMQ.ZMQ_ROUTER;
  84. /**
  85. * Old alias for ROUTER flag.
  86. * Flag to specify the receiving part of a XREQ socket.
  87. *
  88. * @deprecated As of release 3.0 of zeromq, replaced by {@link #ROUTER}
  89. */
  90. public static final int XREP = ROUTER;
  91. /**
  92. * Flag to specify the receiving part of a PUSH socket.
  93. */
  94. public static final int PULL = zmq.ZMQ.ZMQ_PULL;
  95. /**
  96. * Flag to specify a PUSH socket, receiving side must be a PULL.
  97. */
  98. public static final int PUSH = zmq.ZMQ.ZMQ_PUSH;
  99. /**
  100. * Flag to specify a XPUB socket, receiving side must be a SUB or XSUB.
  101. * Subscriptions can be received as a message. Subscriptions start with
  102. * a '1' byte. Unsubscriptions start with a '0' byte.
  103. */
  104. public static final int XPUB = zmq.ZMQ.ZMQ_XPUB;
  105. /**
  106. * Flag to specify the receiving part of the PUB or XPUB socket. Allows
  107. */
  108. public static final int XSUB = zmq.ZMQ.ZMQ_XSUB;
  109. /**
  110. * Flag to specify a STREAMER device.
  111. */
  112. public static final int STREAMER = zmq.ZMQ.ZMQ_STREAMER ;
  113. /**
  114. * Flag to specify a FORWARDER device.
  115. */
  116. public static final int FORWARDER = zmq.ZMQ.ZMQ_FORWARDER ;
  117. /**
  118. * Flag to specify a QUEUE device.
  119. */
  120. public static final int QUEUE = zmq.ZMQ.ZMQ_QUEUE ;
  121. /**
  122. * @see ZMQ#PULL
  123. */
  124. @Deprecated
  125. public static final int UPSTREAM = PULL;
  126. /**
  127. * @see ZMQ#PUSH
  128. */
  129. @Deprecated
  130. public static final int DOWNSTREAM = PUSH;
  131. public static final int POLLIN = zmq.ZMQ.ZMQ_POLLIN;
  132. public static final int POLLOUT = zmq.ZMQ.ZMQ_POLLOUT;
  133. public static final int POLLERR = zmq.ZMQ.ZMQ_POLLERR;
  134. /**
  135. * ZMQ Events
  136. */
  137. public static final int EVENT_CONNECTED = zmq.ZMQ.ZMQ_EVENT_CONNECTED;
  138. public static final int EVENT_DELAYED = zmq.ZMQ.ZMQ_EVENT_CONNECT_DELAYED;
  139. public static final int EVENT_RETRIED = zmq.ZMQ.ZMQ_EVENT_CONNECT_RETRIED;
  140. public static final int EVENT_CONNECT_FAILED = zmq.ZMQ.ZMQ_EVENT_CONNECT_FAILED;
  141. public static final int EVENT_LISTENING = zmq.ZMQ.ZMQ_EVENT_LISTENING;
  142. public static final int EVENT_BIND_FAILED = zmq.ZMQ.ZMQ_EVENT_BIND_FAILED;
  143. public static final int EVENT_ACCEPTED = zmq.ZMQ.ZMQ_EVENT_ACCEPTED;
  144. public static final int EVENT_ACCEPT_FAILED = zmq.ZMQ.ZMQ_EVENT_ACCEPT_FAILED;
  145. public static final int EVENT_CLOSED = zmq.ZMQ.ZMQ_EVENT_CLOSED;
  146. public static final int EVENT_CLOSE_FAILED = zmq.ZMQ.ZMQ_EVENT_CLOSE_FAILED;
  147. public static final int EVENT_DISCONNECTED = zmq.ZMQ.ZMQ_EVENT_DISCONNECTED;
  148. public static final int EVENT_ALL = zmq.ZMQ.ZMQ_EVENT_ALL;
  149. /**
  150. * Create a new Context.
  151. *
  152. * @param ioThreads
  153. * Number of threads to use, usually 1 is sufficient for most use cases.
  154. * @return the Context
  155. */
  156. public static Context context(int ioThreads) {
  157. return new Context(ioThreads);
  158. }
  159. public static Context context() {
  160. return new Context(1);
  161. }
  162. /**
  163. * @deprecated use org.zeromq namespace
  164. */
  165. public static class Context {
  166. private final Ctx ctx;
  167. /**
  168. * Class constructor.
  169. *
  170. * @param ioThreads
  171. * size of the threads pool to handle I/O operations.
  172. */
  173. protected Context(int ioThreads) {
  174. ctx = zmq.ZMQ.zmq_init(ioThreads);
  175. }
  176. /**
  177. * This is an explicit "destructor". It can be called to ensure the corresponding 0MQ
  178. * Context has been disposed of.
  179. */
  180. public void term() {
  181. ctx.terminate();
  182. }
  183. /**
  184. * Create a new Socket within this context.
  185. *
  186. * @param type
  187. * the socket type.
  188. * @return the newly created Socket.
  189. */
  190. public Socket socket(int type) {
  191. return new Socket(this, type);
  192. }
  193. /**
  194. * Create a new Poller within this context, with a default size.
  195. *
  196. * @return the newly created Poller.
  197. */
  198. public Poller poller () {
  199. return new Poller (this);
  200. }
  201. /**
  202. * Create a new Poller within this context, with a specified initial size.
  203. *
  204. * @param size
  205. * the poller initial size.
  206. * @return the newly created Poller.
  207. */
  208. public Poller poller (int size) {
  209. return new Poller (this, size);
  210. }
  211. }
  212. /**
  213. * @deprecated use org.zeromq namespace
  214. */
  215. public static class Socket {
  216. // This port range is defined by IANA for dynamic or private ports
  217. // We use this when choosing a port for dynamic binding.
  218. private static final int DYNFROM = 0xc000;
  219. private static final int DYNTO = 0xffff;
  220. private final Ctx ctx;
  221. private final SocketBase base;
  222. /**
  223. * Class constructor.
  224. *
  225. * @param context
  226. * a 0MQ context previously created.
  227. * @param type
  228. * the socket type.
  229. */
  230. protected Socket(Context context, int type) {
  231. ctx = context.ctx;
  232. base = ctx.create_socket(type);
  233. mayRaise();
  234. }
  235. protected Socket(SocketBase base_) {
  236. ctx = null;
  237. base = base_;
  238. }
  239. public SocketBase base() {
  240. return base;
  241. }
  242. private final static void mayRaise ()
  243. {
  244. if (zmq.ZError.is (0) || zmq.ZError.is (zmq.ZError.EAGAIN) ) ;
  245. else if (zmq.ZError.is (zmq.ZError.ETERM) )
  246. throw new ZMQException.CtxTerminated ();
  247. else
  248. throw new ZMQException (zmq.ZError.errno ());
  249. }
  250. /**
  251. * This is an explicit "destructor". It can be called to ensure the corresponding 0MQ Socket
  252. * has been disposed of.
  253. */
  254. public final void close() {
  255. base.close();
  256. }
  257. /**
  258. * The 'ZMQ_TYPE option shall retrieve the socket type for the specified
  259. * 'socket'. The socket type is specified at socket creation time and
  260. * cannot be modified afterwards.
  261. *
  262. * @return the socket type.
  263. * @since 2.1.0
  264. */
  265. public final int getType () {
  266. return base.getsockopt(zmq.ZMQ.ZMQ_TYPE);
  267. }
  268. /**
  269. * @see #setLinger(long)
  270. *
  271. * @return the linger period.
  272. * @since 2.1.0
  273. */
  274. public final long getLinger() {
  275. return base.getsockopt(zmq.ZMQ.ZMQ_LINGER);
  276. }
  277. /**
  278. * The 'ZMQ_LINGER' option shall retrieve the period for pending outbound
  279. * messages to linger in memory after closing the socket. Value of -1 means
  280. * infinite. Pending messages will be kept until they are fully transferred to
  281. * the peer. Value of 0 means that all the pending messages are dropped immediately
  282. * when socket is closed. Positive value means number of milliseconds to keep
  283. * trying to send the pending messages before discarding them.
  284. *
  285. * @param linger
  286. * the linger period.
  287. * @since 2.1.0
  288. */
  289. public final void setLinger (long linger)
  290. {
  291. base.setsockopt (zmq.ZMQ.ZMQ_LINGER, (int) linger);
  292. }
  293. /**
  294. * @see #setReconnectIVL(long)
  295. *
  296. * @return the reconnectIVL.
  297. * @since 3.0.0
  298. */
  299. public final long getReconnectIVL() {
  300. return base.getsockopt(zmq.ZMQ.ZMQ_RECONNECT_IVL);
  301. }
  302. /**
  303. * @since 3.0.0
  304. */
  305. public final void setReconnectIVL(long value) {
  306. base.setsockopt(zmq.ZMQ.ZMQ_RECONNECT_IVL, (int)value);
  307. mayRaise();
  308. }
  309. /**
  310. * @see #setBacklog(long)
  311. *
  312. * @return the backlog.
  313. * @since 3.0.0
  314. */
  315. public final long getBacklog() {
  316. return base.getsockopt(zmq.ZMQ.ZMQ_BACKLOG);
  317. }
  318. /**
  319. * @since 3.0.0
  320. */
  321. public final void setBacklog(long value) {
  322. base.setsockopt(zmq.ZMQ.ZMQ_BACKLOG, (int)value);
  323. mayRaise();
  324. }
  325. /**
  326. * @see #setReconnectIVLMax(long)
  327. *
  328. * @return the reconnectIVLMax.
  329. * @since 3.0.0
  330. */
  331. public final long getReconnectIVLMax () {
  332. return base.getsockopt(zmq.ZMQ.ZMQ_RECONNECT_IVL_MAX);
  333. }
  334. /**
  335. * @since 3.0.0
  336. */
  337. public final void setReconnectIVLMax (long value) {
  338. base.setsockopt(zmq.ZMQ.ZMQ_RECONNECT_IVL_MAX, (int)value);
  339. mayRaise();
  340. }
  341. /**
  342. * @see #setMaxMsgSize(long)
  343. *
  344. * @return the maxMsgSize.
  345. * @since 3.0.0
  346. */
  347. public final long getMaxMsgSize() {
  348. return (Long)base.getsockoptx(zmq.ZMQ.ZMQ_MAXMSGSIZE);
  349. }
  350. /**
  351. * @since 3.0.0
  352. */
  353. public final void setMaxMsgSize(long value) {
  354. base.setsockopt(zmq.ZMQ.ZMQ_MAXMSGSIZE, value);
  355. mayRaise();
  356. }
  357. /**
  358. * @see #setSndHWM(long)
  359. *
  360. * @return the SndHWM.
  361. * @since 3.0.0
  362. */
  363. public final long getSndHWM() {
  364. return base.getsockopt(zmq.ZMQ.ZMQ_SNDHWM);
  365. }
  366. /**
  367. * @since 3.0.0
  368. */
  369. public final void setSndHWM(long value) {
  370. base.setsockopt(zmq.ZMQ.ZMQ_SNDHWM, (int)value);
  371. mayRaise();
  372. }
  373. /**
  374. * @see #setRcvHWM(long)
  375. *
  376. * @return the recvHWM period.
  377. * @since 3.0.0
  378. */
  379. public final long getRcvHWM() {
  380. return base.getsockopt(zmq.ZMQ.ZMQ_RCVHWM);
  381. }
  382. /**
  383. * @since 3.0.0
  384. */
  385. public final void setRcvHWM(long value) {
  386. base.setsockopt(zmq.ZMQ.ZMQ_RCVHWM, (int)value);
  387. mayRaise();
  388. }
  389. /**
  390. * @see #setHWM(long)
  391. *
  392. * @return the High Water Mark.
  393. */
  394. @Deprecated
  395. public final long getHWM() {
  396. return -1;
  397. }
  398. /**
  399. * The 'ZMQ_HWM' option shall set the high water mark for the specified 'socket'. The high
  400. * water mark is a hard limit on the maximum number of outstanding messages 0MQ shall queue
  401. * in memory for any single peer that the specified 'socket' is communicating with.
  402. *
  403. * If this limit has been reached the socket shall enter an exceptional state and depending
  404. * on the socket type, 0MQ shall take appropriate action such as blocking or dropping sent
  405. * messages. Refer to the individual socket descriptions in the man page of zmq_socket[3] for
  406. * details on the exact action taken for each socket type.
  407. *
  408. * @param hwm
  409. * the number of messages to queue.
  410. */
  411. public final void setHWM(long hwm) {
  412. setSndHWM (hwm);
  413. setRcvHWM (hwm);
  414. }
  415. /**
  416. * @see #setSwap(long)
  417. *
  418. * @return the number of messages to swap at most.
  419. */
  420. @Deprecated
  421. public final long getSwap() {
  422. // not support at zeromq 3
  423. return -1L;
  424. }
  425. /**
  426. * Get the Swap. The 'ZMQ_SWAP' option shall set the disk offload (swap) size for the
  427. * specified 'socket'. A socket which has 'ZMQ_SWAP' set to a non-zero value may exceed its
  428. * high water mark; in this case outstanding messages shall be offloaded to storage on disk
  429. * rather than held in memory.
  430. *
  431. * @param swap
  432. * The value of 'ZMQ_SWAP' defines the maximum size of the swap space in bytes.
  433. */
  434. @Deprecated
  435. public final void setSwap(long value) {
  436. // not support at zeromq 3
  437. }
  438. /**
  439. * @see #setAffinity(long)
  440. *
  441. * @return the affinity.
  442. */
  443. public final long getAffinity() {
  444. return (Long)base.getsockoptx(zmq.ZMQ.ZMQ_AFFINITY);
  445. }
  446. /**
  447. * Get the Affinity. The 'ZMQ_AFFINITY' option shall set the I/O thread affinity for newly
  448. * created connections on the specified 'socket'.
  449. *
  450. * Affinity determines which threads from the 0MQ I/O thread pool associated with the
  451. * socket's _context_ shall handle newly created connections. A value of zero specifies no
  452. * affinity, meaning that work shall be distributed fairly among all 0MQ I/O threads in the
  453. * thread pool. For non-zero values, the lowest bit corresponds to thread 1, second lowest
  454. * bit to thread 2 and so on. For example, a value of 3 specifies that subsequent
  455. * connections on 'socket' shall be handled exclusively by I/O threads 1 and 2.
  456. *
  457. * See also in the man page of zmq_init[3] for details on allocating the number of I/O threads for a
  458. * specific _context_.
  459. *
  460. * @param affinity
  461. * the affinity.
  462. */
  463. public final void setAffinity(long value) {
  464. base.setsockopt(zmq.ZMQ.ZMQ_AFFINITY, value);
  465. mayRaise();
  466. }
  467. /**
  468. * @see #setIdentity(byte[])
  469. *
  470. * @return the Identitiy.
  471. */
  472. public final byte[] getIdentity() {
  473. return (byte[]) base.getsockoptx(zmq.ZMQ.ZMQ_IDENTITY);
  474. }
  475. /**
  476. * The 'ZMQ_IDENTITY' option shall set the identity of the specified 'socket'. Socket
  477. * identity determines if existing 0MQ infastructure (_message queues_, _forwarding
  478. * devices_) shall be identified with a specific application and persist across multiple
  479. * runs of the application.
  480. *
  481. * If the socket has no identity, each run of an application is completely separate from
  482. * other runs. However, with identity set the socket shall re-use any existing 0MQ
  483. * infrastructure configured by the previous run(s). Thus the application may receive
  484. * messages that were sent in the meantime, _message queue_ limits shall be shared with
  485. * previous run(s) and so on.
  486. *
  487. * Identity should be at least one byte and at most 255 bytes long. Identities starting with
  488. * binary zero are reserved for use by 0MQ infrastructure.
  489. *
  490. * @param identity
  491. */
  492. public final void setIdentity(byte[] identity) {
  493. base.setsockopt(zmq.ZMQ.ZMQ_IDENTITY, identity);
  494. mayRaise();
  495. }
  496. public final void setIdentity(String identity) {
  497. setIdentity(identity.getBytes());
  498. }
  499. /**
  500. * @see #setRate(long)
  501. *
  502. * @return the Rate.
  503. */
  504. public final long getRate() {
  505. return base.getsockopt(zmq.ZMQ.ZMQ_RATE);
  506. }
  507. /**
  508. * The 'ZMQ_RATE' option shall set the maximum send or receive data rate for multicast
  509. * transports such as in the man page of zmq_pgm[7] using the specified 'socket'.
  510. *
  511. * @param rate
  512. */
  513. public final void setRate(long value) {
  514. base.setsockopt(zmq.ZMQ.ZMQ_RATE, (int)value);
  515. mayRaise();
  516. }
  517. /**
  518. * @see #setRecoveryInterval(long)
  519. *
  520. * @return the RecoveryIntervall.
  521. */
  522. public final long getRecoveryInterval () {
  523. return base.getsockopt(zmq.ZMQ.ZMQ_RECOVERY_IVL);
  524. }
  525. /**
  526. * The 'ZMQ_RECOVERY_IVL' option shall set the recovery interval for multicast transports
  527. * using the specified 'socket'. The recovery interval determines the maximum time in
  528. * seconds that a receiver can be absent from a multicast group before unrecoverable data
  529. * loss will occur.
  530. *
  531. * CAUTION: Excersize care when setting large recovery intervals as the data needed for
  532. * recovery will be held in memory. For example, a 1 minute recovery interval at a data rate
  533. * of 1Gbps requires a 7GB in-memory buffer. {Purpose of this Method}
  534. *
  535. * @param recovery_ivl
  536. */
  537. public final void setRecoveryInterval (long value) {
  538. base.setsockopt(zmq.ZMQ.ZMQ_RECOVERY_IVL, (int)value);
  539. mayRaise();
  540. }
  541. /**
  542. * @see #setMulticastLoop(boolean)
  543. *
  544. * @return the Multicast Loop.
  545. */
  546. @Deprecated
  547. public final boolean hasMulticastLoop () {
  548. return false;
  549. }
  550. /**
  551. * The 'ZMQ_MCAST_LOOP' option shall control whether data sent via multicast transports
  552. * using the specified 'socket' can also be received by the sending host via loopback. A
  553. * value of zero disables the loopback functionality, while the default value of 1 enables
  554. * the loopback functionality. Leaving multicast loopback enabled when it is not required
  555. * can have a negative impact on performance. Where possible, disable 'ZMQ_MCAST_LOOP' in
  556. * production environments.
  557. *
  558. * @param mcast_loop
  559. */
  560. @Deprecated
  561. public final void setMulticastLoop (boolean mcast_loop) {
  562. }
  563. /**
  564. * @see #setMulticastHops(long)
  565. *
  566. * @return the Multicast Hops.
  567. */
  568. public final long getMulticastHops () {
  569. return base.getsockopt(zmq.ZMQ.ZMQ_MULTICAST_HOPS);
  570. }
  571. /**
  572. * Sets the time-to-live field in every multicast packet sent from this socket.
  573. * The default is 1 which means that the multicast packets don't leave the local
  574. * network.
  575. *
  576. * @param mcast_hops
  577. */
  578. public final void setMulticastHops (long value) {
  579. base.setsockopt(zmq.ZMQ.ZMQ_MULTICAST_HOPS, (int)value);
  580. mayRaise();
  581. }
  582. /**
  583. * @see #setReceiveTimeOut(int)
  584. *
  585. * @return the Receive Timeout
  586. */
  587. public final int getReceiveTimeOut() {
  588. return base.getsockopt(zmq.ZMQ.ZMQ_RCVTIMEO);
  589. }
  590. /**
  591. * Sets the timeout for receive operation on the socket. If the value is 0, recv
  592. * will return immediately, with null if there is no message to receive.
  593. * If the value is -1, it will block until a message is available. For all other
  594. * values, it will wait for a message for that amount of time before returning with
  595. * an null.
  596. *
  597. * @param timeout
  598. */
  599. public final void setReceiveTimeOut(int value) {
  600. base.setsockopt(zmq.ZMQ.ZMQ_RCVTIMEO, value);
  601. mayRaise();
  602. }
  603. /**
  604. * @see #setSendTimeOut(int)
  605. *
  606. * @return the Send Timeout.
  607. */
  608. public final int getSendTimeOut() {
  609. return (int)base.getsockopt(zmq.ZMQ.ZMQ_SNDTIMEO);
  610. }
  611. /**
  612. * Sets the timeout for send operation on the socket. If the value is 0, send
  613. * will return immediately, with a false if the message cannot be sent.
  614. * If the value is -1, it will block until the message is sent. For all other
  615. * values, it will try to send the message for that amount of time before
  616. * returning with a false.
  617. *
  618. * @param timeout
  619. */
  620. public final void setSendTimeOut(int value) {
  621. base.setsockopt(zmq.ZMQ.ZMQ_SNDTIMEO, value);
  622. mayRaise();
  623. }
  624. /**
  625. * @see #setSendBufferSize(long)
  626. *
  627. * @return the kernel send buffer size.
  628. */
  629. public final long getSendBufferSize() {
  630. return base.getsockopt(zmq.ZMQ.ZMQ_SNDBUF);
  631. }
  632. /**
  633. * The 'ZMQ_SNDBUF' option shall set the underlying kernel transmit buffer size for the
  634. * 'socket' to the specified size in bytes. A value of zero means leave the OS default
  635. * unchanged. For details please refer to your operating system documentation for the
  636. * 'SO_SNDBUF' socket option.
  637. *
  638. * @param sndbuf
  639. */
  640. public final void setSendBufferSize(long value) {
  641. base.setsockopt(zmq.ZMQ.ZMQ_SNDBUF, (int)value);
  642. mayRaise();
  643. }
  644. /**
  645. * @see #setReceiveBufferSize(long)
  646. *
  647. * @return the kernel receive buffer size.
  648. */
  649. public final long getReceiveBufferSize() {
  650. return base.getsockopt(zmq.ZMQ.ZMQ_RCVBUF);
  651. }
  652. /**
  653. * The 'ZMQ_RCVBUF' option shall set the underlying kernel receive buffer size for the
  654. * 'socket' to the specified size in bytes. A value of zero means leave the OS default
  655. * unchanged. For details refer to your operating system documentation for the 'SO_RCVBUF'
  656. * socket option.
  657. *
  658. * @param rcvbuf
  659. */
  660. public final void setReceiveBufferSize(long value) {
  661. base.setsockopt(zmq.ZMQ.ZMQ_RCVBUF, (int)value);
  662. mayRaise();
  663. }
  664. /**
  665. * The 'ZMQ_RCVMORE' option shall return a boolean value indicating if the multi-part
  666. * message currently being read from the specified 'socket' has more message parts to
  667. * follow. If there are no message parts to follow or if the message currently being read is
  668. * not a multi-part message a value of zero shall be returned. Otherwise, a value of 1 shall
  669. * be returned.
  670. *
  671. * @return true if there are more messages to receive.
  672. */
  673. public final boolean hasReceiveMore ()
  674. {
  675. return base.getsockopt (zmq.ZMQ.ZMQ_RCVMORE) == 1;
  676. }
  677. /**
  678. * The 'ZMQ_FD' option shall retrieve file descriptor associated with the 0MQ
  679. * socket. The descriptor can be used to integrate 0MQ socket into an existing
  680. * event loop. It should never be used for anything else than polling -- such as
  681. * reading or writing. The descriptor signals edge-triggered IN event when
  682. * something has happened within the 0MQ socket. It does not necessarily mean that
  683. * the messages can be read or written. Check ZMQ_EVENTS option to find out whether
  684. * the 0MQ socket is readable or writeable.
  685. *
  686. * @return the underlying file descriptor.
  687. * @since 2.1.0
  688. */
  689. public final SelectableChannel getFD() {
  690. return (SelectableChannel)base.getsockoptx(zmq.ZMQ.ZMQ_FD);
  691. }
  692. /**
  693. * The 'ZMQ_EVENTS' option shall retrieve event flags for the specified socket.
  694. * If a message can be read from the socket ZMQ_POLLIN flag is set. If message can
  695. * be written to the socket ZMQ_POLLOUT flag is set.
  696. *
  697. * @return the mask of outstanding events.
  698. * @since 2.1.0
  699. */
  700. public final int getEvents() {
  701. return base.getsockopt(zmq.ZMQ.ZMQ_EVENTS);
  702. }
  703. /**
  704. * The 'ZMQ_SUBSCRIBE' option shall establish a new message filter on a 'ZMQ_SUB' socket.
  705. * Newly created 'ZMQ_SUB' sockets shall filter out all incoming messages, therefore you
  706. * should call this option to establish an initial message filter.
  707. *
  708. * An empty 'option_value' of length zero shall subscribe to all incoming messages. A
  709. * non-empty 'option_value' shall subscribe to all messages beginning with the specified
  710. * prefix. Mutiple filters may be attached to a single 'ZMQ_SUB' socket, in which case a
  711. * message shall be accepted if it matches at least one filter.
  712. *
  713. * @param topic
  714. */
  715. public final void subscribe(byte[] topic) {
  716. base.setsockopt(zmq.ZMQ.ZMQ_SUBSCRIBE, topic);
  717. mayRaise();
  718. }
  719. public final void subscribe(String topic) {
  720. subscribe(topic.getBytes());
  721. }
  722. /**
  723. * The 'ZMQ_UNSUBSCRIBE' option shall remove an existing message filter on a 'ZMQ_SUB'
  724. * socket. The filter specified must match an existing filter previously established with
  725. * the 'ZMQ_SUBSCRIBE' option. If the socket has several instances of the same filter
  726. * attached the 'ZMQ_UNSUBSCRIBE' option shall remove only one instance, leaving the rest in
  727. * place and functional.
  728. *
  729. * @param topic
  730. */
  731. public final void unsubscribe(byte[] topic) {
  732. base.setsockopt(zmq.ZMQ.ZMQ_UNSUBSCRIBE, topic);
  733. mayRaise();
  734. }
  735. public final void unsubscribe(String topic) {
  736. unsubscribe(topic.getBytes());
  737. }
  738. /**
  739. * Set custom Encoder
  740. * @param cls
  741. */
  742. public final void setEncoder(Class<? extends EncoderBase> cls) {
  743. base.setsockopt(zmq.ZMQ.ZMQ_ENCODER, cls);
  744. }
  745. /**
  746. * Set custom Decoder
  747. * @param cls
  748. */
  749. public final void setDecoder(Class<? extends DecoderBase> cls) {
  750. base.setsockopt(zmq.ZMQ.ZMQ_DECODER, cls);
  751. }
  752. /**
  753. * @see #setRouterMandatory (boolean)
  754. *
  755. * @return the Router Manadatory.
  756. */
  757. public final boolean getRouterMandatory () {
  758. return false;
  759. }
  760. /**
  761. * Set Router Mandatory
  762. * @param mandadatory
  763. */
  764. public final void setRouterMandatory (boolean mandatory) {
  765. base.setsockopt (zmq.ZMQ.ZMQ_ROUTER_MANDATORY, mandatory ? 1 : 0);
  766. }
  767. /**
  768. * Bind to network interface. Start listening for new connections.
  769. *
  770. * @param addr
  771. * the endpoint to bind to.
  772. */
  773. public final int bind (String addr)
  774. {
  775. return bind (addr, DYNFROM, DYNTO);
  776. }
  777. /**
  778. * Bind to network interface. Start listening for new connections.
  779. *
  780. * @param addr
  781. * the endpoint to bind to.
  782. * @param min
  783. * The minimum port in the range of ports to try.
  784. * @param max
  785. * The maximum port in the range of ports to try.
  786. */
  787. private final int bind (String addr, int min, int max)
  788. {
  789. if (addr.endsWith (":*")) {
  790. int port = min;
  791. String prefix = addr.substring (0, addr.lastIndexOf (':') + 1);
  792. while (port <= max) {
  793. addr = prefix + port;
  794. // Try to bind on the next plausible port
  795. if (base.bind (addr))
  796. return port;
  797. port++;
  798. }
  799. return -1;
  800. } else {
  801. if (base.bind(addr)) {
  802. int port = 0;
  803. try {
  804. port = Integer.parseInt (
  805. addr.substring (addr.lastIndexOf (':') + 1));
  806. } catch (NumberFormatException e) {
  807. }
  808. return port;
  809. } else {
  810. return -1;
  811. }
  812. }
  813. }
  814. /**
  815. * Bind to network interface to a random port. Start listening for new
  816. * connections.
  817. *
  818. * @param addr
  819. * the endpoint to bind to.
  820. */
  821. public int bindToRandomPort (String addr)
  822. {
  823. return bind (addr + ":*", DYNFROM, DYNTO);
  824. }
  825. /**
  826. * Bind to network interface to a random port. Start listening for new
  827. * connections.
  828. *
  829. * @param addr
  830. * the endpoint to bind to.
  831. * @param min
  832. * The minimum port in the range of ports to try.
  833. * @param max
  834. * The maximum port in the range of ports to try.
  835. */
  836. public int bindToRandomPort (String addr, int min, int max)
  837. {
  838. return bind (addr + ":*", min, max);
  839. }
  840. /**
  841. * Connect to remote application.
  842. *
  843. * @param addr
  844. * the endpoint to connect to.
  845. */
  846. public final boolean connect (String addr_) {
  847. boolean ret = base.connect (addr_);
  848. mayRaise ();
  849. return ret;
  850. }
  851. public final boolean send (String data) {
  852. zmq.Msg msg = new zmq.Msg(data);
  853. return base.send(msg, 0);
  854. }
  855. public final boolean sendMore (String data) {
  856. zmq.Msg msg = new zmq.Msg(data);
  857. return base.send(msg, zmq.ZMQ.ZMQ_SNDMORE);
  858. }
  859. public final boolean send (String data, int flags) {
  860. zmq.Msg msg = new zmq.Msg(data);
  861. return base.send(msg, flags);
  862. }
  863. public final boolean send (byte[] data) {
  864. zmq.Msg msg = new zmq.Msg(data);
  865. return base.send(msg, 0);
  866. }
  867. public final boolean sendMore (byte[] data) {
  868. zmq.Msg msg = new zmq.Msg(data);
  869. return base.send(msg, zmq.ZMQ.ZMQ_SNDMORE);
  870. }
  871. public final boolean send (byte[] data, int flags) {
  872. zmq.Msg msg = new zmq.Msg(data);
  873. return base.send(msg, flags);
  874. }
  875. /**
  876. * Send a message.
  877. *
  878. * @param msg
  879. * the message to send, as an array of bytes.
  880. * @return true if send was successful, false otherwise.
  881. */
  882. public final boolean send (Msg msg) {
  883. return base.send(msg.base, 0);
  884. }
  885. /**
  886. * Send a message.
  887. *
  888. * @param msg
  889. * the message to send, as an array of bytes.
  890. * @return true if send was successful, false otherwise.
  891. */
  892. public final boolean sendMore (Msg msg) {
  893. return base.send(msg.base, zmq.ZMQ.ZMQ_SNDMORE);
  894. }
  895. /**
  896. * Send a message.
  897. *
  898. * @param msg
  899. * the message to send, as an array of bytes.
  900. * @param flags
  901. * the flags to apply to the send operation.
  902. * @return true if send was successful, false otherwise.
  903. */
  904. public final boolean send (Msg msg, int flags) {
  905. return base.send(msg.base, flags);
  906. }
  907. /**
  908. * Receive a message.
  909. *
  910. * @return the message received, as an array of bytes; null on error.
  911. */
  912. public final byte [] recv ()
  913. {
  914. return recv (0);
  915. }
  916. /**
  917. * Receive a message.
  918. *
  919. * @param flags
  920. * the flags to apply to the receive operation.
  921. * @return the message received, as an array of bytes; null on error.
  922. */
  923. public final byte [] recv (int flags)
  924. {
  925. zmq.Msg msg = base.recv(flags);
  926. if (msg != null) {
  927. return msg.data();
  928. }
  929. return null;
  930. }
  931. /**
  932. * Receive a message in to a specified buffer.
  933. *
  934. * @param buffer
  935. * byte[] to copy zmq message payload in to.
  936. * @param offset
  937. * offset in buffer to write data
  938. * @param len
  939. * max bytes to write to buffer.
  940. * If len is smaller than the incoming message size,
  941. * the message will be truncated.
  942. * @param flags
  943. * the flags to apply to the receive operation.
  944. * @return the number of bytes read, -1 on error
  945. */
  946. public final int recv (byte [] buffer, int offset, int len, int flags)
  947. {
  948. zmq.Msg msg = base.recv(flags);
  949. if (msg != null) {
  950. System.arraycopy(msg.data(), 0, buffer, offset, len);
  951. return msg.size();
  952. }
  953. return -1;
  954. }
  955. /**
  956. * Receive a message.
  957. *
  958. * @return the message received, as a Msg object; null on no message.
  959. */
  960. public final Msg recvMsg ()
  961. {
  962. return recvMsg (0);
  963. }
  964. /**
  965. * Receive a message.
  966. *
  967. * @param flags
  968. * the flags to apply to the receive operation.
  969. * @return the message received, as a Msg object; null on no message.
  970. */
  971. public final Msg recvMsg (int flags)
  972. {
  973. zmq.Msg msg = base.recv(flags);
  974. if (msg != null) {
  975. return new Msg (msg);
  976. }
  977. return null;
  978. }
  979. /**
  980. *
  981. * @return the message received, as a String object; null on no message.
  982. */
  983. public final String recvStr () {
  984. return recvStr (0);
  985. }
  986. /**
  987. *
  988. * @param flags the flags to apply to the receive operation.
  989. * @return the message received, as a String object; null on no message.
  990. */
  991. public final String recvStr (int flags)
  992. {
  993. zmq.Msg msg = base.recv(flags);
  994. if (msg != null) {
  995. return new String (msg.data());
  996. }
  997. return null;
  998. }
  999. public boolean monitor (String addr, int events)
  1000. {
  1001. return base.monitor (addr, events);
  1002. }
  1003. public void dump() {
  1004. System.out.println("----------------------------------------");
  1005. while(true) {
  1006. Msg msg = recvMsg(0);
  1007. System.out.println(String.format("[%03d] %s", msg.size(),
  1008. msg.size() > 0 ? new String(msg.data()) : ""));
  1009. if (!hasReceiveMore()) {
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. * @deprecated this will be deleted
  1017. */
  1018. public static class Msg {
  1019. public final static int MORE = zmq.Msg.more;
  1020. private final zmq.Msg base;
  1021. public Msg () {
  1022. base = new zmq.Msg (); //empty message for bottom
  1023. }
  1024. public Msg (zmq.Msg msg) {
  1025. base = msg;
  1026. }
  1027. public Msg (int size) {
  1028. base = new zmq.Msg (size);
  1029. }
  1030. public Msg (String data) {
  1031. base = new zmq.Msg (data);
  1032. }
  1033. public Msg (ByteBuffer data) {
  1034. base = new zmq.Msg (data);
  1035. }
  1036. public int size () {
  1037. return base.size ();
  1038. }
  1039. public ByteBuffer buf () {
  1040. return base.buf ();
  1041. }
  1042. public byte [] data () {
  1043. return base.data ();
  1044. }
  1045. public void put (String data, int idx) {
  1046. base.put (data, idx);
  1047. }
  1048. public void put (byte [] data, int idx) {
  1049. base.put (data, idx);
  1050. }
  1051. public void put (Msg data, int idx) {
  1052. base.put (data.base, idx);
  1053. }
  1054. public void setFlags (int flags) {
  1055. base.set_flags (flags);
  1056. }
  1057. public int getFlags ()
  1058. {
  1059. return base.flags ();
  1060. }
  1061. public boolean hasMore ()
  1062. {
  1063. return base.has_more ();
  1064. }
  1065. public void close ()
  1066. {
  1067. base.close ();
  1068. }
  1069. }
  1070. // GC closes selector handle
  1071. protected static class ReuseableSelector {
  1072. private Selector selector;
  1073. protected ReuseableSelector () {
  1074. selector = null;
  1075. }
  1076. public Selector open () throws IOException
  1077. {
  1078. selector = Selector.open();
  1079. return selector;
  1080. }
  1081. public Selector get ()
  1082. {
  1083. assert (selector != null);
  1084. assert (selector.isOpen ());
  1085. return selector;
  1086. }
  1087. @Override
  1088. public void finalize ()
  1089. {
  1090. try {
  1091. selector.close ();
  1092. } catch (IOException e) {
  1093. }
  1094. try {
  1095. super.finalize ();
  1096. } catch (Throwable e) {
  1097. }
  1098. }
  1099. }
  1100. /**
  1101. * @deprecated use org.zeromq namespace
  1102. */
  1103. public static class Poller {
  1104. /**
  1105. * These values can be ORed to specify what we want to poll for.
  1106. */
  1107. public static final int POLLIN = zmq.ZMQ.ZMQ_POLLIN;
  1108. public static final int POLLOUT = zmq.ZMQ.ZMQ_POLLOUT;
  1109. public static final int POLLERR = zmq.ZMQ.ZMQ_POLLERR;
  1110. private static final int SIZE_DEFAULT = 32;
  1111. private static final int SIZE_INCREMENT = 16;
  1112. private static final ThreadLocal <ReuseableSelector> holder = new ThreadLocal <ReuseableSelector> ();
  1113. private Selector selector;
  1114. private zmq.PollItem [] items;
  1115. private long timeout;
  1116. private int next;
  1117. /**
  1118. * Class constructor.
  1119. *
  1120. * @param context
  1121. * a 0MQ context previously created.
  1122. * @param size
  1123. * the number of Sockets this poller will contain.
  1124. */
  1125. protected Poller (Context context, int size) {
  1126. items = new zmq.PollItem[size];
  1127. timeout = -1L;
  1128. next = 0;
  1129. open ();
  1130. }
  1131. /**
  1132. * Class constructor.
  1133. *
  1134. * @param context
  1135. * a 0MQ context previously created.
  1136. */
  1137. protected Poller (Context context) {
  1138. this(context, SIZE_DEFAULT);
  1139. }
  1140. private void open () {
  1141. if (holder.get () == null) {
  1142. synchronized (holder) {
  1143. try {
  1144. if (selector == null) {
  1145. ReuseableSelector s = new ReuseableSelector ();
  1146. selector = s.open ();
  1147. holder.set (s);
  1148. }
  1149. } catch (IOException e) {
  1150. throw new ZError.IOException(e);
  1151. }
  1152. }
  1153. }
  1154. selector = holder.get ().get ();
  1155. }
  1156. /**
  1157. * Register a Socket for polling on all events.
  1158. *
  1159. * @param socket
  1160. * the Socket we are registering.
  1161. * @return the index identifying this Socket in the poll set.
  1162. */
  1163. public int register (Socket socket) {
  1164. return register (socket, POLLIN | POLLOUT | POLLERR);
  1165. }
  1166. /**
  1167. * Register a Socket for polling on the specified events.
  1168. *
  1169. * Automatically grow the internal representation if needed.
  1170. *
  1171. * @param socket
  1172. * the Socket we are registering.
  1173. * @param events
  1174. * a mask composed by XORing POLLIN, POLLOUT and POLLERR.
  1175. * @return the index identifying this Socket in the poll set.
  1176. */
  1177. public int register (Socket socket, int events) {
  1178. return insert (new zmq.PollItem (socket.base, events));
  1179. }
  1180. /**
  1181. * Register a Socket for polling on the specified events.
  1182. *
  1183. * Automatically grow the internal representation if needed.
  1184. *
  1185. * @param channel
  1186. * the Channel we are registering.
  1187. * @param events
  1188. * a mask composed by XORing POLLIN, POLLOUT and POLLERR.
  1189. * @return the index identifying this Channel in the poll set.
  1190. */
  1191. public int register (SelectableChannel channel, int events) {
  1192. return insert (new zmq.PollItem (channel, events));
  1193. }
  1194. private int insert (zmq.PollItem item)
  1195. {
  1196. int pos = next++;
  1197. if (pos == items.length) {
  1198. zmq.PollItem[] nitems = new zmq.PollItem[items.length + SIZE_INCREMENT];
  1199. System.arraycopy (items, 0, nitems, 0, items.length);
  1200. items = nitems;
  1201. }
  1202. items [pos] = item;
  1203. return pos;
  1204. }
  1205. /**
  1206. * Unregister a Socket for polling on the specified events.
  1207. *
  1208. * @param socket
  1209. * the Socket to be unregistered
  1210. */
  1211. public void unregister (Socket socket) {
  1212. for (int pos = 0; pos < items.length ;pos++) {
  1213. zmq.PollItem item = items[pos];
  1214. if (item.getSocket () == socket.base) {
  1215. remove (pos);
  1216. break;
  1217. }
  1218. }
  1219. }
  1220. /**
  1221. * Unregister a Socket for polling on the specified events.
  1222. *
  1223. * @param channel
  1224. * the Socket to be unregistered
  1225. */
  1226. public void unregister (SelectableChannel channel) {
  1227. for (int pos = 0; pos < items.length ;pos++) {
  1228. zmq.PollItem item = items[pos];
  1229. if (item.getRawSocket () == channel) {
  1230. remove (pos);
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. private void remove (int pos)
  1236. {
  1237. next--;
  1238. if (pos != next)
  1239. items [pos] = items [next];
  1240. items [next] = null;
  1241. }
  1242. /**
  1243. * Get the socket associated with an index.
  1244. *
  1245. * @param index
  1246. * the desired index.
  1247. * @return the Socket associated with that index (or null).
  1248. */
  1249. public Socket getSocket (int index) {
  1250. if (index < 0 || index >= this.next)
  1251. return null;
  1252. return new Socket(items [index].getSocket ());
  1253. }
  1254. /**
  1255. * Get the current poll timeout.
  1256. *
  1257. * @return the current poll timeout in microseconds.
  1258. * @deprecated Timeout handling has been moved to the poll() methods.
  1259. */
  1260. public long getTimeout () {
  1261. return this.timeout;
  1262. }
  1263. /**
  1264. * Set the poll timeout.
  1265. *
  1266. * @param timeout
  1267. * the desired poll timeout in microseconds.
  1268. * @deprecated Timeout handling has been moved to the poll() methods.
  1269. */
  1270. public void setTimeout (long timeout) {
  1271. if (timeout < -1)
  1272. return;
  1273. this.timeout = timeout;
  1274. }
  1275. /**
  1276. * Get the current poll set size.
  1277. *
  1278. * @return the current poll set size.
  1279. */
  1280. public int getSize () {
  1281. return items.length;
  1282. }
  1283. /**
  1284. * Get the index for the next position in the poll set size.
  1285. *
  1286. * @return the index for the next position in the poll set size.
  1287. */
  1288. public int getNext () {
  1289. return this.next;
  1290. }
  1291. /**
  1292. * Issue a poll call. If the poller's internal timeout value
  1293. * has been set, use that value as timeout; otherwise, block
  1294. * indefinitely.
  1295. *
  1296. * @return how many objects where signaled by poll ().
  1297. */
  1298. public int poll () {
  1299. long tout = -1L;
  1300. if (this.timeout > -1L) {
  1301. tout = this.timeout;
  1302. }
  1303. return poll(tout);
  1304. }
  1305. /**
  1306. * Issue a poll call, using the specified timeout value.
  1307. * <p>
  1308. * Since ZeroMQ 3.0, the timeout parameter is in <i>milliseconds<i>,
  1309. * but prior to this the unit was <i>microseconds</i>.
  1310. *
  1311. * @param tout
  1312. * the timeout, as per zmq_poll ();
  1313. * if -1, it will block indefinitely until an event
  1314. * happens; if 0, it will return immediately;
  1315. * otherwise, it will wait for at most that many
  1316. * milliseconds/microseconds (see above).
  1317. *
  1318. * @see "http://api.zeromq.org/3-0:zmq-poll"
  1319. *
  1320. * @return how many objects where signaled by poll ()
  1321. */
  1322. public int poll(long tout) {
  1323. return zmq.ZMQ.zmq_poll (selector, items, tout);
  1324. }
  1325. /**
  1326. * Check whether the specified element in the poll set was signaled for input.
  1327. *
  1328. * @param index
  1329. *
  1330. * @return true if the element was signaled.
  1331. */
  1332. public boolean pollin (int index) {
  1333. if (index < 0 || index >= this.next)
  1334. return false;
  1335. return items [index].isReadable();
  1336. }
  1337. /**
  1338. * Check whether the specified element in the poll set was signaled for output.
  1339. *
  1340. * @param index
  1341. *
  1342. * @return true if the element was signaled.
  1343. */
  1344. public boolean pollout (int index) {
  1345. if (index < 0 || index >= this.next)
  1346. return false;
  1347. return items [index].isWritable ();
  1348. }
  1349. /**
  1350. * Check whether the specified element in the poll set was signaled for error.
  1351. *
  1352. * @param index
  1353. *
  1354. * @return true if the element was signaled.
  1355. */
  1356. public boolean pollerr (int index) {
  1357. if (index < 0 || index >= this.next)
  1358. return false;
  1359. return items [index].isError();
  1360. }
  1361. }
  1362. /**
  1363. * @deprecated use org.zeromq namespace
  1364. */
  1365. public static class PollItem {
  1366. private final zmq.PollItem base;
  1367. public PollItem (Socket s, int ops) {
  1368. base = new zmq.PollItem (s.base, ops);
  1369. }
  1370. public PollItem (SelectableChannel s, int ops) {
  1371. base = new zmq.PollItem (s, ops);
  1372. }
  1373. public final zmq.PollItem base() {
  1374. return base;
  1375. }
  1376. public final SelectableChannel getChannel() {
  1377. return base.getRawSocket ();
  1378. }
  1379. public final SocketBase getSocket() {
  1380. return base.getSocket();
  1381. }
  1382. public final boolean isReadable() {
  1383. return base.isReadable();
  1384. }
  1385. public final boolean isWritable() {
  1386. return base.isWritable ();
  1387. }
  1388. public final int interestOps(int ops) {
  1389. return base.interestOps(ops);
  1390. }
  1391. }
  1392. /**
  1393. * @deprecated use org.zeromq namespace
  1394. */
  1395. public enum Error {
  1396. ENOTSUP(ZError.ENOTSUP),
  1397. EPROTONOSUPPORT(ZError.EPROTONOSUPPORT),
  1398. ENOBUFS(ZError.ENOBUFS),
  1399. ENETDOWN(ZError.ENETDOWN),
  1400. EADDRINUSE(ZError.EADDRINUSE),
  1401. EADDRNOTAVAIL(ZError.EADDRNOTAVAIL),
  1402. ECONNREFUSED(ZError.ECONNREFUSED),
  1403. EINPROGRESS(ZError.EINPROGRESS),
  1404. EMTHREAD(ZError.EMTHREAD),
  1405. EFSM(ZError.EFSM),
  1406. ENOCOMPATPROTO(ZError.ENOCOMPATPROTO),
  1407. ETERM(ZError.ETERM);
  1408. private final long code;
  1409. Error(long code) {
  1410. this.code = code;
  1411. }
  1412. public long getCode() {
  1413. return code;
  1414. }
  1415. public static Error findByCode(int code) {
  1416. for (Error e : Error.class.getEnumConstants()) {
  1417. if (e.getCode() == code) {
  1418. return e;
  1419. }
  1420. }
  1421. throw new IllegalArgumentException("Unknown " + Error.class.getName() + " enum code:" + code);
  1422. }
  1423. }
  1424. public static boolean device (int type_, Socket sa, Socket sb)
  1425. {
  1426. return zmq.ZMQ.zmq_proxy (sa.base, sb.base, null);
  1427. }
  1428. public static int poll (PollItem[] items, long timeout) {
  1429. zmq.PollItem[] items_ = new zmq.PollItem[items.length];
  1430. for (int i=0; i < items.length; i++) {
  1431. items_[i] = items[i].base;
  1432. }
  1433. return zmq.ZMQ.zmq_poll(items_, timeout);
  1434. }
  1435. /**
  1436. * @return Major version number of the ZMQ library.
  1437. */
  1438. public static int getMajorVersion ()
  1439. {
  1440. return zmq.ZMQ.ZMQ_VERSION_MAJOR;
  1441. }
  1442. /**
  1443. * @return Major version number of the ZMQ library.
  1444. */
  1445. public static int getMinorVersion ()
  1446. {
  1447. return zmq.ZMQ.ZMQ_VERSION_MINOR;
  1448. }
  1449. /**
  1450. * @return Major version number of the ZMQ library.
  1451. */
  1452. public static int getPatchVersion ()
  1453. {
  1454. return zmq.ZMQ.ZMQ_VERSION_PATCH;
  1455. }
  1456. /**
  1457. * @return Full version number of the ZMQ library used for comparing versions.
  1458. */
  1459. public static int getFullVersion() {
  1460. return zmq.ZMQ.ZMQ_MAKE_VERSION(zmq.ZMQ.ZMQ_VERSION_MAJOR,
  1461. zmq.ZMQ.ZMQ_VERSION_MINOR,
  1462. zmq.ZMQ.ZMQ_VERSION_PATCH);
  1463. }
  1464. /**
  1465. * @param major Version major component.
  1466. * @param minor Version minor component.
  1467. * @param patch Version patch component.
  1468. *
  1469. * @return Comparible single int version number.
  1470. */
  1471. public static int makeVersion ( final int major,
  1472. final int minor,
  1473. final int patch )
  1474. {
  1475. return zmq.ZMQ.ZMQ_MAKE_VERSION ( major, minor, patch );
  1476. }
  1477. /**
  1478. * @return String version number in the form major.minor.patch.
  1479. */
  1480. public static String getVersionString() {
  1481. return "" + zmq.ZMQ.ZMQ_VERSION_MAJOR + "." +
  1482. zmq.ZMQ.ZMQ_VERSION_MINOR + "." +
  1483. zmq.ZMQ.ZMQ_VERSION_PATCH;
  1484. }
  1485. }