ezmq 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -167,7 +167,7 @@
167
167
  <li class="public ">
168
168
  <span class="summary_signature">
169
169
 
170
- <a href="#initialize-instance_method" title="#initialize (instance method)">- (Publisher) <strong>initialize</strong>(action: -&gt; m { puts m }, **options) </a>
170
+ <a href="#initialize-instance_method" title="#initialize (instance method)">- (Publisher) <strong>initialize</strong>(**options) </a>
171
171
 
172
172
 
173
173
 
@@ -193,7 +193,7 @@
193
193
  <li class="public ">
194
194
  <span class="summary_signature">
195
195
 
196
- <a href="#listen-instance_method" title="#listen (instance method)">- (void) <strong>listen</strong>(handler: -&gt; { @action.call(receive) }) </a>
196
+ <a href="#listen-instance_method" title="#listen (instance method)">- (void) <strong>listen</strong> {|message| ... }</a>
197
197
 
198
198
 
199
199
 
@@ -208,7 +208,7 @@
208
208
 
209
209
 
210
210
  <span class="summary_desc"><div class='inline'>
211
- <p>By default, waits for a message and calls @action with the message.</p>
211
+ <p>By default, waits for a message and prints it to STDOUT.</p>
212
212
  </div></span>
213
213
 
214
214
  </li>
@@ -282,7 +282,7 @@
282
282
  <div class="method_details first">
283
283
  <h3 class="signature first" id="initialize-instance_method">
284
284
 
285
- - (<tt><span class='object_link'><a href="Publisher.html" title="EZMQ::Publisher (class)">Publisher</a></span></tt>) <strong>initialize</strong>(action: -&gt; m { puts m }, **options)
285
+ - (<tt><span class='object_link'><a href="Publisher.html" title="EZMQ::Publisher (class)">Publisher</a></span></tt>) <strong>initialize</strong>(**options)
286
286
 
287
287
 
288
288
 
@@ -308,22 +308,6 @@
308
308
  <p class="tag_title">Parameters:</p>
309
309
  <ul class="param">
310
310
 
311
- <li>
312
-
313
- <span class='name'>action</span>
314
-
315
-
316
- <span class='type'>(<tt>lambda</tt>)</span>
317
-
318
-
319
-
320
- &mdash;
321
- <div class='inline'>
322
- <p>the action to perform when a message is received.</p>
323
- </div>
324
-
325
- </li>
326
-
327
311
  <li>
328
312
 
329
313
  <span class='name'>options</span>
@@ -335,7 +319,7 @@
335
319
 
336
320
  &mdash;
337
321
  <div class='inline'>
338
- <p>optional parameters</p>
322
+ <p>optional parameters.</p>
339
323
  </div>
340
324
 
341
325
  </li>
@@ -345,8 +329,6 @@
345
329
 
346
330
 
347
331
 
348
-
349
-
350
332
  <p class="tag_title">Options Hash (<tt>**options</tt>):</p>
351
333
  <ul class="option">
352
334
 
@@ -370,7 +352,7 @@
370
352
  <p class="tag_title">See Also:</p>
371
353
  <ul class="see">
372
354
 
373
- <li><span class='object_link'><a href="Socket.html" title="EZMQ::Socket (class)">EZMQ::Socket for a list of optional parameters.</a></span></li>
355
+ <li><span class='object_link'><a href="Socket.html" title="EZMQ::Socket (class)">EZMQ::Socket for optional parameters.</a></span></li>
374
356
 
375
357
  </ul>
376
358
 
@@ -380,17 +362,15 @@
380
362
  <pre class="lines">
381
363
 
382
364
 
383
- 187
384
- 188
385
- 189
386
- 190
387
- 191</pre>
365
+ 218
366
+ 219
367
+ 220
368
+ 221</pre>
388
369
  </td>
389
370
  <td>
390
- <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 187</span>
371
+ <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 218</span>
391
372
 
392
- <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>action:</span> <span class='tlambda'>-&gt;</span> <span class='id identifier rubyid_m'>m</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_m'>m</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>**</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
393
- <span class='ivar'>@action</span> <span class='op'>=</span> <span class='id identifier rubyid_action'>action</span>
373
+ <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='op'>**</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
394
374
  <span class='kw'>super</span> <span class='symbol'>:connect</span><span class='comma'>,</span> <span class='const'>ZMQ</span><span class='op'>::</span><span class='const'>SUB</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span>
395
375
  <span class='id identifier rubyid_subscribe'>subscribe</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:topic</span><span class='rbracket'>]</span> <span class='kw'>if</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:topic</span><span class='rbracket'>]</span>
396
376
  <span class='kw'>end</span></pre>
@@ -432,12 +412,12 @@
432
412
  <pre class="lines">
433
413
 
434
414
 
435
- 172
436
- 173
437
- 174</pre>
415
+ 206
416
+ 207
417
+ 208</pre>
438
418
  </td>
439
419
  <td>
440
- <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 172</span>
420
+ <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 206</span>
441
421
 
442
422
  <span class='kw'>def</span> <span class='id identifier rubyid_action'>action</span>
443
423
  <span class='ivar'>@action</span>
@@ -457,7 +437,7 @@
457
437
  <div class="method_details first">
458
438
  <h3 class="signature first" id="listen-instance_method">
459
439
 
460
- - (<tt>void</tt>) <strong>listen</strong>(handler: -&gt; { @action.call(receive) })
440
+ - (<tt>void</tt>) <strong>listen</strong> {|message| ... }
461
441
 
462
442
 
463
443
 
@@ -466,49 +446,79 @@
466
446
  </h3><div class="docstring">
467
447
  <div class="discussion">
468
448
  <p class="note returns_void">This method returns an undefined value.</p>
469
- <p>By default, waits for a message and calls @action with the message.</p>
449
+ <p>By default, waits for a message and prints it to STDOUT.</p>
470
450
 
471
451
 
472
452
  </div>
473
453
  </div>
474
454
  <div class="tags">
475
- <p class="tag_title">Parameters:</p>
476
- <ul class="param">
455
+
456
+ <p class="tag_title">Yields:</p>
457
+ <ul class="yield">
477
458
 
478
459
  <li>
479
460
 
480
- <span class='name'>handler</span>
481
461
 
462
+ <span class='type'></span>
482
463
 
483
- <span class='type'>(<tt>lambda</tt>)</span>
464
+
465
+
466
+
467
+ <div class='inline'>
468
+ <p>message passes the message received to the block.</p>
469
+ </div>
470
+
471
+ </li>
472
+
473
+ </ul>
474
+ <p class="tag_title">Yield Parameters:</p>
475
+ <ul class="yieldparam">
476
+
477
+ <li>
478
+
479
+ <span class='name'>message</span>
480
+
481
+
482
+ <span class='type'>(<tt>String</tt>)</span>
484
483
 
485
484
 
486
485
 
487
486
  &mdash;
488
487
  <div class='inline'>
489
- <p>how requests are handled.</p>
488
+ <p>the message received.</p>
490
489
  </div>
491
490
 
492
491
  </li>
493
492
 
494
493
  </ul>
495
494
 
496
-
497
495
  </div><table class="source_code">
498
496
  <tr>
499
497
  <td>
500
498
  <pre class="lines">
501
499
 
502
500
 
503
- 225
504
- 226
505
- 227</pre>
501
+ 256
502
+ 257
503
+ 258
504
+ 259
505
+ 260
506
+ 261
507
+ 262
508
+ 263
509
+ 264</pre>
506
510
  </td>
507
511
  <td>
508
- <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 225</span>
509
-
510
- <span class='kw'>def</span> <span class='id identifier rubyid_listen'>listen</span><span class='lparen'>(</span><span class='label'>handler:</span> <span class='tlambda'>-&gt;</span> <span class='tlambeg'>{</span> <span class='ivar'>@action</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_receive'>receive</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='rparen'>)</span>
511
- <span class='id identifier rubyid_loop'>loop</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_handler'>handler</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='rbrace'>}</span>
512
+ <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 256</span>
513
+
514
+ <span class='kw'>def</span> <span class='id identifier rubyid_listen'>listen</span>
515
+ <span class='id identifier rubyid_loop'>loop</span> <span class='kw'>do</span>
516
+ <span class='kw'>if</span> <span class='id identifier rubyid_block_given?'>block_given?</span>
517
+ <span class='kw'>yield</span> <span class='id identifier rubyid_receive'>receive</span>
518
+ <span class='kw'>else</span>
519
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_receive'>receive</span>
520
+ <span class='kw'>end</span>
521
+ <span class='kw'>end</span>
512
522
  <span class='kw'>end</span></pre>
513
523
  </td>
514
524
  </tr>
@@ -592,15 +602,15 @@ provided, #initialize calls #subscribe automatically.</p>
592
602
  <pre class="lines">
593
603
 
594
604
 
595
- 204
596
- 205
597
- 206</pre>
605
+ 234
606
+ 235
607
+ 236</pre>
598
608
  </td>
599
609
  <td>
600
- <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 204</span>
610
+ <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 234</span>
601
611
 
602
612
  <span class='kw'>def</span> <span class='id identifier rubyid_subscribe'>subscribe</span><span class='lparen'>(</span><span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span>
603
- <span class='ivar'>@socket</span><span class='period'>.</span><span class='id identifier rubyid_setsockopt'>setsockopt</span><span class='lparen'>(</span><span class='const'>ZMQ</span><span class='op'>::</span><span class='const'>SUBSCRIBE</span><span class='comma'>,</span> <span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='int'>0</span> <span class='op'>?</span> <span class='kw'>true</span> <span class='op'>:</span> <span class='kw'>false</span>
613
+ <span class='ivar'>@socket</span><span class='period'>.</span><span class='id identifier rubyid_setsockopt'>setsockopt</span><span class='lparen'>(</span><span class='const'>ZMQ</span><span class='op'>::</span><span class='const'>SUBSCRIBE</span><span class='comma'>,</span> <span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='int'>0</span>
604
614
  <span class='kw'>end</span></pre>
605
615
  </td>
606
616
  </tr>
@@ -672,15 +682,15 @@ set, this will only remove one.</p>
672
682
  <pre class="lines">
673
683
 
674
684
 
675
- 215
676
- 216
677
- 217</pre>
685
+ 245
686
+ 246
687
+ 247</pre>
678
688
  </td>
679
689
  <td>
680
- <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 215</span>
690
+ <pre class="code"><span class="info file"># File 'lib/ezmq.rb', line 245</span>
681
691
 
682
692
  <span class='kw'>def</span> <span class='id identifier rubyid_unsubscribe'>unsubscribe</span><span class='lparen'>(</span><span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span>
683
- <span class='ivar'>@socket</span><span class='period'>.</span><span class='id identifier rubyid_setsockopt'>setsockopt</span><span class='lparen'>(</span><span class='const'>ZMQ</span><span class='op'>::</span><span class='const'>UNSUBSCRIBE</span><span class='comma'>,</span> <span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='int'>0</span> <span class='op'>?</span> <span class='kw'>true</span> <span class='op'>:</span> <span class='kw'>false</span>
693
+ <span class='ivar'>@socket</span><span class='period'>.</span><span class='id identifier rubyid_setsockopt'>setsockopt</span><span class='lparen'>(</span><span class='const'>ZMQ</span><span class='op'>::</span><span class='const'>UNSUBSCRIBE</span><span class='comma'>,</span> <span class='id identifier rubyid_topic'>topic</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='int'>0</span>
684
694
  <span class='kw'>end</span></pre>
685
695
  </td>
686
696
  </tr>
@@ -692,7 +702,7 @@ set, this will only remove one.</p>
692
702
  </div>
693
703
 
694
704
  <div id="footer">
695
- Generated on Tue Jan 6 12:58:18 2015 by
705
+ Generated on Fri Jan 9 13:06:27 2015 by
696
706
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
697
707
  0.8.7.6 (ruby-2.0.0).
698
708
  </div>
@@ -160,7 +160,7 @@
160
160
  </div>
161
161
 
162
162
  <div id="footer">
163
- Generated on Tue Jan 6 12:58:18 2015 by
163
+ Generated on Fri Jan 9 13:06:26 2015 by
164
164
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
165
165
  0.8.7.6 (ruby-2.0.0).
166
166
  </div>
@@ -61,13 +61,99 @@
61
61
 
62
62
  <iframe id="search_frame"></iframe>
63
63
 
64
- <div id="content"><div id='filecontents'><h1>EZMQ (Effortless ZeroMQ)</h1>
64
+ <div id="content"><div id='filecontents'><h1><a href="https://colstrom.github.io/ezmq/">EZMQ (Effortless ZeroMQ)</a></h1>
65
+
66
+ <h2>Overview</h2>
65
67
 
66
68
  <p>EZMQ is a wrapper around the wonderful <code>ffi-rzmq</code> gem, which (as the name suggests) uses FFI, and exposes a fairly raw C-like interface. As elegant as 0MQ is, C doesn't feel like Ruby, and FFI bindings feel like C. EZMQ makes some reasonable assumptions to help you focus on what makes your code special, and not worry about setting up 0MQ.</p>
67
69
 
68
70
  <p>Any of the magical hand-wavey bits (contexts, sockets, etc) are still exposed for tinkering, EZMQ just starts you off with some sane defaults.</p>
69
71
 
70
- <h2>Operating System Notes</h2>
72
+ <h1>Examples</h1>
73
+
74
+ <p>Most of these examples are trivial, because ZMQ is just the fabric of your networked application(s).</p>
75
+
76
+ <h2>Echo Server</h2>
77
+
78
+ <p>Waits for a request, replies with the same request.</p>
79
+
80
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
81
+
82
+ <span class='id identifier rubyid_server'>server</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Server</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
83
+ <span class='id identifier rubyid_server'>server</span><span class='period'>.</span><span class='id identifier rubyid_listen'>listen</span>
84
+ </code></pre>
85
+
86
+ <h2>Synchronous Client Request</h2>
87
+
88
+ <p>Sends a message, prints the reply when it arrives.</p>
89
+
90
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
91
+
92
+ <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Client</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
93
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
94
+ </code></pre>
95
+
96
+ <h2>Confirming Logging Server</h2>
97
+
98
+ <p>Waits for a request, prints it to STDOUT, and thanks the client for it.</p>
99
+
100
+ <pre class="code ruby"><code class="ruby">require &#39;ezmq&#39;
101
+
102
+ server = EZMQ::Server.new
103
+ server.listen do |message|
104
+ puts message
105
+ &#39;Thanks for the message!&#39; # The return of the block is sent to the client.
106
+ end
107
+
108
+ JSON Echo Server
109
+ ----------------
110
+ Waits for JSON message, decodes it, re-encodes it, and sends it back.
111
+ </code></pre>
112
+
113
+ <p>require 'ezmq'
114
+ require 'json'</p>
115
+
116
+ <p>server = EZMQ::Server.new encode: -> m { JSON.dump m }, decode: -> m { JSON.load m }
117
+ server.listen
118
+ ```</p>
119
+
120
+ <h2>JSON Synchronous Client Request</h2>
121
+
122
+ <p>Encodes a message in JSON, sends it twice, prints the first one raw, and decodes the second.</p>
123
+
124
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
125
+ <span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>json</span><span class='tstring_end'>&#39;</span></span>
126
+
127
+ <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Client</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>encode:</span> <span class='tlambda'>-&gt;</span> <span class='id identifier rubyid_m'>m</span> <span class='tlambeg'>{</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_dump'>dump</span> <span class='id identifier rubyid_m'>m</span> <span class='rbrace'>}</span>
128
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
129
+ <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_decode'>decode</span> <span class='op'>=</span> <span class='tlambda'>-&gt;</span> <span class='id identifier rubyid_m'>m</span> <span class='tlambeg'>{</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_load'>load</span> <span class='id identifier rubyid_m'>m</span> <span class='rbrace'>}</span>
130
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
131
+ </code></pre>
132
+
133
+ <h2>'foorever' Publisher</h2>
134
+
135
+ <p>Publishes an endless stream of 'foo's with a topic of 'foorever'.</p>
136
+
137
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
138
+
139
+ <span class='id identifier rubyid_publisher'>publisher</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='period'>.</span><span class='const'>Publisher</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>topic:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foorever</span><span class='tstring_end'>&#39;</span></span>
140
+
141
+ <span class='id identifier rubyid_loop'>loop</span> <span class='kw'>do</span>
142
+ <span class='id identifier rubyid_publisher'>publisher</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foo</span><span class='tstring_end'>&#39;</span></span>
143
+ <span class='kw'>end</span>
144
+ </code></pre>
145
+
146
+ <h2>'foorever' Subscriber</h2>
147
+
148
+ <p>Subscribes to topic 'foorever', prints any messages it receives.</p>
149
+
150
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
151
+
152
+ <span class='id identifier rubyid_subscriber'>subscriber</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='period'>.</span><span class='const'>Subscriber</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>topic:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foorever</span><span class='tstring_end'>&#39;</span></span>
153
+ <span class='id identifier rubyid_subscriber'>subscriber</span><span class='period'>.</span><span class='id identifier rubyid_listen'>listen</span>
154
+ </code></pre>
155
+
156
+ <h1>Operating System Notes</h1>
71
157
 
72
158
  <p>As this relies on <a href="https://github.com/chuckremes/ffi-rzmq">ffi-rzmq</a>, you will need to have the zeromq libraries available.</p>
73
159
 
@@ -86,7 +172,7 @@ sudo aptitude install libzmq3-dev
86
172
  </div></div>
87
173
 
88
174
  <div id="footer">
89
- Generated on Tue Jan 6 12:58:18 2015 by
175
+ Generated on Fri Jan 9 13:06:26 2015 by
90
176
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
91
177
  0.8.7.6 (ruby-2.0.0).
92
178
  </div>
@@ -61,13 +61,99 @@
61
61
 
62
62
  <iframe id="search_frame"></iframe>
63
63
 
64
- <div id="content"><div id='filecontents'><h1>EZMQ (Effortless ZeroMQ)</h1>
64
+ <div id="content"><div id='filecontents'><h1><a href="https://colstrom.github.io/ezmq/">EZMQ (Effortless ZeroMQ)</a></h1>
65
+
66
+ <h2>Overview</h2>
65
67
 
66
68
  <p>EZMQ is a wrapper around the wonderful <code>ffi-rzmq</code> gem, which (as the name suggests) uses FFI, and exposes a fairly raw C-like interface. As elegant as 0MQ is, C doesn't feel like Ruby, and FFI bindings feel like C. EZMQ makes some reasonable assumptions to help you focus on what makes your code special, and not worry about setting up 0MQ.</p>
67
69
 
68
70
  <p>Any of the magical hand-wavey bits (contexts, sockets, etc) are still exposed for tinkering, EZMQ just starts you off with some sane defaults.</p>
69
71
 
70
- <h2>Operating System Notes</h2>
72
+ <h1>Examples</h1>
73
+
74
+ <p>Most of these examples are trivial, because ZMQ is just the fabric of your networked application(s).</p>
75
+
76
+ <h2>Echo Server</h2>
77
+
78
+ <p>Waits for a request, replies with the same request.</p>
79
+
80
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
81
+
82
+ <span class='id identifier rubyid_server'>server</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Server</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
83
+ <span class='id identifier rubyid_server'>server</span><span class='period'>.</span><span class='id identifier rubyid_listen'>listen</span>
84
+ </code></pre>
85
+
86
+ <h2>Synchronous Client Request</h2>
87
+
88
+ <p>Sends a message, prints the reply when it arrives.</p>
89
+
90
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
91
+
92
+ <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Client</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
93
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
94
+ </code></pre>
95
+
96
+ <h2>Confirming Logging Server</h2>
97
+
98
+ <p>Waits for a request, prints it to STDOUT, and thanks the client for it.</p>
99
+
100
+ <pre class="code ruby"><code class="ruby">require &#39;ezmq&#39;
101
+
102
+ server = EZMQ::Server.new
103
+ server.listen do |message|
104
+ puts message
105
+ &#39;Thanks for the message!&#39; # The return of the block is sent to the client.
106
+ end
107
+
108
+ JSON Echo Server
109
+ ----------------
110
+ Waits for JSON message, decodes it, re-encodes it, and sends it back.
111
+ </code></pre>
112
+
113
+ <p>require 'ezmq'
114
+ require 'json'</p>
115
+
116
+ <p>server = EZMQ::Server.new encode: -> m { JSON.dump m }, decode: -> m { JSON.load m }
117
+ server.listen
118
+ ```</p>
119
+
120
+ <h2>JSON Synchronous Client Request</h2>
121
+
122
+ <p>Encodes a message in JSON, sends it twice, prints the first one raw, and decodes the second.</p>
123
+
124
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
125
+ <span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>json</span><span class='tstring_end'>&#39;</span></span>
126
+
127
+ <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='op'>::</span><span class='const'>Client</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>encode:</span> <span class='tlambda'>-&gt;</span> <span class='id identifier rubyid_m'>m</span> <span class='tlambeg'>{</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_dump'>dump</span> <span class='id identifier rubyid_m'>m</span> <span class='rbrace'>}</span>
128
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
129
+ <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_decode'>decode</span> <span class='op'>=</span> <span class='tlambda'>-&gt;</span> <span class='id identifier rubyid_m'>m</span> <span class='tlambeg'>{</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_load'>load</span> <span class='id identifier rubyid_m'>m</span> <span class='rbrace'>}</span>
130
+ <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_request'>request</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>test</span><span class='tstring_end'>&#39;</span></span>
131
+ </code></pre>
132
+
133
+ <h2>'foorever' Publisher</h2>
134
+
135
+ <p>Publishes an endless stream of 'foo's with a topic of 'foorever'.</p>
136
+
137
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
138
+
139
+ <span class='id identifier rubyid_publisher'>publisher</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='period'>.</span><span class='const'>Publisher</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>topic:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foorever</span><span class='tstring_end'>&#39;</span></span>
140
+
141
+ <span class='id identifier rubyid_loop'>loop</span> <span class='kw'>do</span>
142
+ <span class='id identifier rubyid_publisher'>publisher</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foo</span><span class='tstring_end'>&#39;</span></span>
143
+ <span class='kw'>end</span>
144
+ </code></pre>
145
+
146
+ <h2>'foorever' Subscriber</h2>
147
+
148
+ <p>Subscribes to topic 'foorever', prints any messages it receives.</p>
149
+
150
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ezmq</span><span class='tstring_end'>&#39;</span></span>
151
+
152
+ <span class='id identifier rubyid_subscriber'>subscriber</span> <span class='op'>=</span> <span class='const'>EZMQ</span><span class='period'>.</span><span class='const'>Subscriber</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='label'>topic:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>foorever</span><span class='tstring_end'>&#39;</span></span>
153
+ <span class='id identifier rubyid_subscriber'>subscriber</span><span class='period'>.</span><span class='id identifier rubyid_listen'>listen</span>
154
+ </code></pre>
155
+
156
+ <h1>Operating System Notes</h1>
71
157
 
72
158
  <p>As this relies on <a href="https://github.com/chuckremes/ffi-rzmq">ffi-rzmq</a>, you will need to have the zeromq libraries available.</p>
73
159
 
@@ -86,7 +172,7 @@ sudo aptitude install libzmq3-dev
86
172
  </div></div>
87
173
 
88
174
  <div id="footer">
89
- Generated on Tue Jan 6 12:58:18 2015 by
175
+ Generated on Fri Jan 9 13:06:26 2015 by
90
176
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
91
177
  0.8.7.6 (ruby-2.0.0).
92
178
  </div>