dripdrop 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,7 @@ Let's start by looking at the normalized communication interface in a simple app
32
32
  route :my_client, :http_client, 'http://127.0.0.1:2201'
33
33
 
34
34
  # Our http server is a simple time server
35
- my_server.on_recv do |message,response|
35
+ my_server.on_receive do |message,response|
36
36
  response.send_message(:name => 'time', :body => {'time' => Time.now.to_s})
37
37
  end
38
38
 
@@ -57,7 +57,7 @@ What we've done here is use HTTP as a simple messaging protocol. Yes, we've thro
57
57
 
58
58
  That replaces the HTTP server and client with ultra-high performance zeromq sockets. Now, protocols have varying strengths and weaknesses, and ZeroMQ is not HTTP necessarily, for instance, given a :zmq_pub socket, you can only send_messages, but there is no response message, because :zmq_pub is the publishing end of a request/reply pattern. The messaging API attempts to reduce all methods on sockets to the following set:
59
59
 
60
- * on_recv (sometimes takes a block with |message,response| if it can send a response)
60
+ * on_receive (sometimes takes a block with |message,response| if it can send a response)
61
61
  * send_message
62
62
  * on_open (Websockets only)
63
63
  * on_close (Websockets only)
@@ -88,7 +88,7 @@ The tools mentioned above are useful, but if you try and build a larger app you'
88
88
  # The method #run here is merely a convention
89
89
  class StatsCollector < DripDrop::Node::Nodelet
90
90
  def run
91
- stats_raw.on_recv do |raw_stat_msg|
91
+ stats_raw.on_receive do |raw_stat_msg|
92
92
  # Custom filtering code could go here...
93
93
  stats_filtered.send_message(raw_stat_msg)
94
94
  end
@@ -103,7 +103,7 @@ The tools mentioned above are useful, but if you try and build a larger app you'
103
103
  end
104
104
 
105
105
  def run
106
- stats_ingress.on_recv do |message|
106
+ stats_ingress.on_receive do |message|
107
107
  @name_counts[message.name] += 1
108
108
  puts @name_counts.inspect
109
109
  end
@@ -10,15 +10,15 @@ DripDrop::Node.new do
10
10
  route :http_collector, :http_server, 'http://127.0.0.1:8080'
11
11
  route :http_agent, :http_client, http_collector.address
12
12
 
13
- stats_sub1.on_recv do |message|
13
+ stats_sub1.on_receive do |message|
14
14
  puts "Receiver 1: #{message.body}"
15
15
  end
16
- stats_sub2.on_recv do |message|
16
+ stats_sub2.on_receive do |message|
17
17
  puts "Receiver 2: #{message.body}"
18
18
  end
19
19
 
20
20
  i = 0
21
- http_collector.on_recv do |message,response|
21
+ http_collector.on_receive do |message,response|
22
22
  i += 1
23
23
  stats_pub.send_message(message)
24
24
  response.send_message(:name => 'ack', :body => {:seq => i})
@@ -29,7 +29,7 @@ class Coordinator < DripDrop::Node::Nodelet
29
29
  end
30
30
 
31
31
  def proxy_reqs
32
- reqs_in.on_recv do |message, response|
32
+ reqs_in.on_receive do |message, response|
33
33
  puts "Proxying #{message.inspect} to htout"
34
34
  reqs_htout.send_message(message) do |http_response|
35
35
  puts "Received http response #{http_response.inspect} sending back"
@@ -58,7 +58,7 @@ class WSListener < DripDrop::Node::Nodelet
58
58
 
59
59
  def broadcast_to_websockets
60
60
  # Receives messages from Broadcast Out
61
- broadcast_in.on_recv do |message|
61
+ broadcast_in.on_receive do |message|
62
62
  puts "Broadcast In recv: #{message.inspect}"
63
63
  @client_channel.push(message)
64
64
  end
@@ -87,7 +87,7 @@ class WSListener < DripDrop::Node::Nodelet
87
87
  @client_channel.unsubscribe sigs_sids[conn.signature]
88
88
  end
89
89
 
90
- ws.on_recv do |message,conn|
90
+ ws.on_receive do |message,conn|
91
91
  puts "WS Recv #{message.name}"
92
92
  reqs_out.send_message(message) do |resp_message|
93
93
  puts "Recvd resp_message #{resp_message.inspect}, sending back to client"
@@ -5,7 +5,7 @@ DripDrop::Node.new do
5
5
  addr = 'http://127.0.0.1:2200'
6
6
 
7
7
  i = 0
8
- http_server(addr).on_recv do |msg,response|
8
+ http_server(addr).on_receive do |msg,response|
9
9
  i += 1
10
10
  response.send_message(msg)
11
11
  end
@@ -4,24 +4,24 @@ Thread.abort_on_exception = true
4
4
 
5
5
  #Define our handlers
6
6
  DripDrop::Node.new do
7
- route :pub, :zmq_publish, 'tcp://localhost:2200', :bind
7
+ route :pub, :zmq_publish, 'tcp://127.0.0.1:2200', :bind
8
8
  route :sub1, :zmq_subscribe, pub.address, :connect, :topic_filter => /[13579]$/
9
9
  route :sub2, :zmq_subscribe, pub.address, :connect, :topic_filter => /[02468]$/
10
10
  route :sub3, :zmq_subscribe, pub.address, :connect
11
11
 
12
- sub1.on_recv do |message|
12
+ sub1.on_receive do |message|
13
13
  puts "Receiver 1 #{message.inspect}"
14
14
  end
15
15
 
16
- sub2.on_recv do |message|
16
+ sub2.on_receive do |message|
17
17
  puts "Receiver 2 #{message.inspect}"
18
18
  end
19
19
 
20
- sub3.on_recv do |message|
20
+ sub3.on_receive do |message|
21
21
  puts "Receiver 3 #{message.inspect}"
22
22
  end
23
23
 
24
- zm_reactor.periodical_timer(500) do
24
+ EM::PeriodicTimer.new(0.5) do
25
25
  puts "Sending!"
26
26
  #Sending a hash as a message implicitly transforms it into a DripDrop::Message
27
27
  pub.send_message(:name => Time.now.to_i.to_s, :body => 'Test Payload')
@@ -4,10 +4,10 @@ Thread.abort_on_exception = true
4
4
  DripDrop::Node.new do
5
5
  z_addr = 'tcp://127.0.0.1:2200'
6
6
 
7
- zmq_pull(z_addr, :connect).on_recv do |message|
7
+ zmq_pull(z_addr, :connect).on_receive do |message|
8
8
  puts "Receiver 2 #{message.body}"
9
9
  end
10
- zmq_pull(z_addr, :connect).on_recv do |message|
10
+ zmq_pull(z_addr, :connect).on_receive do |message|
11
11
  puts "Receiver 1 #{message.body}"
12
12
  end
13
13
  push = zmq_push(z_addr, :bind)
@@ -36,13 +36,13 @@ node = DripDrop::Node.new do
36
36
  pull1 = zmq_pull("tcp://127.0.0.1:2201", :connect)
37
37
  pull2 = zmq_pull("tcp://127.0.0.1:2202", :connect)
38
38
 
39
- pull1.on_recv do |msg|
39
+ pull1.on_receive do |msg|
40
40
  puts "Pull 1 #{msg.head.inspect}"
41
41
  sleep 1
42
42
  push2.send_message(msg)
43
43
  end
44
44
 
45
- pull2.on_recv do |msg|
45
+ pull2.on_receive do |msg|
46
46
  puts "Pull 2 #{msg.head.inspect}"
47
47
  end
48
48
 
@@ -5,7 +5,7 @@ DripDrop::Node.new do
5
5
  route :xrep_server, :zmq_xrep, 'tcp://127.0.0.1:2200', :bind
6
6
  route :xreq_client, :zmq_xreq, xrep_server.address, :connect
7
7
 
8
- xrep_server.on_recv do |message,response|
8
+ xrep_server.on_receive do |message,response|
9
9
  puts "REP #{message.body}"
10
10
  response.send_message(message)
11
11
  end
@@ -2,9 +2,9 @@
2
2
  "http://www.w3.org/TR/html4/loose.dtd">
3
3
  <html>
4
4
  <head>
5
- <script src="../jquery.js"></script>
6
- <script src="../jquery.json.js"></script>
7
- <script src="../dripdrop.js"></script>
5
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
6
+ <script src="jquery.json.js"></script>
7
+ <script src="dripdrop.js"></script>
8
8
 
9
9
  <link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
10
10
  <script type="text/javascript" src="qunit.js"></script>
@@ -15,7 +15,7 @@ $(document).ready(function(){
15
15
 
16
16
  /* Test Helpers */
17
17
  function TSock() {return new DD.WebSocket('ws://localhost:2702')};
18
- function THTTP() {return new DD.HTTP('http://localhost:2703')};
18
+ function THTTP() {return new DD.HTTPClient('http://localhost:2703')};
19
19
 
20
20
  //Faked message to for a websocket
21
21
  function FakeWSMessage(ddMessage) {
@@ -85,13 +85,13 @@ module("DD.WebSocket");
85
85
  });
86
86
  });
87
87
 
88
- test("onRecv should trigger the passed in callback", function() {
88
+ test("onReceive should trigger the passed in callback", function() {
89
89
  expect(3);
90
90
 
91
91
  var expectedMsg = new DD.Message('foo');
92
92
 
93
93
  var tSock = new TSock;
94
- tSock.onRecv(function(msg) {
94
+ tSock.onReceive(function(msg) {
95
95
  ok(true, "Function executed");
96
96
  equals(msg.constructor,DD.Message, "Is a DD.Message");
97
97
  equals(msg.name, expectedMsg.name);
@@ -115,13 +115,14 @@ module("DD.WebSocket");
115
115
  module("DD.HTTP");
116
116
 
117
117
  test("Creation", function() {
118
- var dht = new DD.HTTP;
119
- equals(dht.constructor, DD.HTTP, "Constructor Match");
118
+ var dht = new DD.HTTPClient;
119
+ equals(dht.constructor, DD.HTTPClient, "Constructor Match");
120
120
  });
121
121
 
122
+ // Not sure how to mock / stub this into working
122
123
  test("Sending a message should call a get posting a JSON representation of the data",function() {
123
124
  expect(1);
124
- var tHTTP = new THTTP;
125
+ var tHTTP = THTTP;
125
126
  tHTTP.sendMessage();
126
127
  });
127
128
 
@@ -16,7 +16,7 @@ function DripDrop() {
16
16
  /* A DripDrop friendly WebSocket Object.
17
17
  This automatically converts messages to DD.Message objects.
18
18
  Additionally, this uses friendlier callback methods, closer to the DripDrop
19
- server-side API, like onOpen, onRecv, onError, and onClose. */
19
+ server-side API, like onOpen, onReceive, onError, and onClose. */
20
20
  this.WebSocket = function(url) {
21
21
  this.socket = new WebSocket(url);
22
22
 
@@ -25,7 +25,7 @@ function DripDrop() {
25
25
  return this;
26
26
  };
27
27
 
28
- this.onRecv = function(callback) {
28
+ this.onReceive = function(callback) {
29
29
  this.socket.onmessage = function(wsMessage) {
30
30
  var json = $.parseJSON(wsMessage.data)
31
31
  var message = new DD.Message(json.name, {head: json.head, body: json.body});
@@ -56,14 +56,14 @@ function DripDrop() {
56
56
  };
57
57
 
58
58
  /* A DripDrop friendly HTTP Request. */
59
- this.HTTP = function(url) {
59
+ this.HTTPClient = function(url) {
60
60
  this.url = url;
61
61
 
62
- this.onRecv = function(data) {};
62
+ this.onReceive = function(data) {};
63
63
  this.sendMessage = function() {
64
64
  var response = new this.HTTPResponse;
65
65
  $.post(this.url, function(json) {
66
- this.onRecv(new DD.Message(json.name, {head: json.head, body: json.body}));
66
+ this.onReceive(new DD.Message(json.name, {head: json.head, body: json.body}));
67
67
  });
68
68
  };
69
69
  };
@@ -0,0 +1,31 @@
1
+
2
+ (function($){$.toJSON=function(o)
3
+ {if(typeof(JSON)=='object'&&JSON.stringify)
4
+ return JSON.stringify(o);var type=typeof(o);if(o===null)
5
+ return"null";if(type=="undefined")
6
+ return undefined;if(type=="number"||type=="boolean")
7
+ return o+"";if(type=="string")
8
+ return $.quoteString(o);if(type=='object')
9
+ {if(typeof o.toJSON=="function")
10
+ return $.toJSON(o.toJSON());if(o.constructor===Date)
11
+ {var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
12
+ hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
13
+ if(o.constructor===Array)
14
+ {var ret=[];for(var i=0;i<o.length;i++)
15
+ ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
16
+ var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
17
+ name='"'+k+'"';else if(type=="string")
18
+ name=$.quoteString(k);else
19
+ continue;if(typeof o[k]=="function")
20
+ continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
21
+ return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
22
+ {if(typeof(JSON)=='object'&&JSON.parse)
23
+ return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
24
+ {if(typeof(JSON)=='object'&&JSON.parse)
25
+ return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
26
+ return eval("("+src+")");else
27
+ throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
28
+ {if(string.match(_escapeable))
29
+ {return'"'+string.replace(_escapeable,function(a)
30
+ {var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
31
+ return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);
@@ -50,10 +50,15 @@ class DripDrop
50
50
  @message_class = @opts[:message_class] || DripDrop.default_message_class
51
51
  end
52
52
 
53
- def on_recv(msg_format=:dripdrop_json,&block)
53
+ def on_receive(msg_format=:dripdrop_json,&block)
54
54
  @recv_cbak = block
55
55
  @conn = EM.start_server(@uri.host, @uri.port, HTTPEMServer, self)
56
56
  self
57
57
  end
58
+
59
+ def on_recv(*args,&block)
60
+ $stderr.write "DripDrop Warning :on_recv is deprecated in favor of :on_receive"
61
+ on_receive(*args,&block)
62
+ end
58
63
  end
59
64
  end
@@ -43,11 +43,16 @@ class DripDrop
43
43
  end
44
44
  end
45
45
 
46
- def on_recv(&block)
46
+ def on_receive(&block)
47
47
  @raw = false
48
48
  @onmessage_handler = block
49
49
  self
50
50
  end
51
+
52
+ def on_recv(&block)
53
+ $stderr.write "DripDrop Warning :on_recv is deprecated in favor of :on_receive"
54
+ on_receive(&block)
55
+ end
51
56
 
52
57
  def on_recv_raw(&block)
53
58
  @raw = true
@@ -33,10 +33,15 @@ class DripDrop
33
33
  @connection
34
34
  end
35
35
 
36
- def on_recv(msg_format=:dripdrop,&block)
36
+ def on_receive(msg_format=:dripdrop,&block)
37
37
  @recv_cbak = block
38
38
  self
39
39
  end
40
+
41
+ def on_recv(*args,&block)
42
+ $stderr.write "DripDrop Warning :on_recv is deprecated in favor of :on_receive"
43
+ on_receive(*args,&block)
44
+ end
40
45
 
41
46
  def address
42
47
  self.connection.address
@@ -261,7 +266,8 @@ class DripDrop
261
266
  @seq_counter = 0
262
267
  @promises = {}
263
268
 
264
- self.on_recv do |message|
269
+ # should never be handled by the user
270
+ self.on_receive do |message|
265
271
  begin
266
272
  seq = message.head[SEQ_CTR_KEY]
267
273
  raise "Missing Seq Counter" unless seq
@@ -179,7 +179,7 @@ class DripDrop
179
179
  zmq_handler(DripDrop::Mongrel2Handler, [ZMQ::PULL, ZMQ::PUB], addresses, [:connect, :connect], opts)
180
180
  end
181
181
 
182
- # Creates a ZMQ::SUB type socket. Can only receive messages via +on_recv+.
182
+ # Creates a ZMQ::SUB type socket. Can only receive messages via +on_receive+.
183
183
  # zmq_subscribe sockets have a +topic_filter+ option, which restricts which
184
184
  # messages they can receive. It takes a regexp as an option.
185
185
  def zmq_subscribe(address,socket_ctype,opts={},&block)
@@ -191,7 +191,7 @@ class DripDrop
191
191
  zmq_handler(DripDrop::ZMQPubHandler,ZMQ::PUB,address,socket_ctype,opts)
192
192
  end
193
193
 
194
- # Creates a ZMQ::PULL type socket. Can only receive messages via +on_recv+
194
+ # Creates a ZMQ::PULL type socket. Can only receive messages via +on_receive+
195
195
  def zmq_pull(address,socket_ctype,opts={},&block)
196
196
  zmq_handler(DripDrop::ZMQPullHandler,ZMQ::PULL,address,socket_ctype,opts)
197
197
  end
@@ -205,11 +205,11 @@ class DripDrop
205
205
  # powerful, so their functionality is currently limited. XREP sockets in DripDrop can reply
206
206
  # to the original source of the message.
207
207
  #
208
- # Receiving with XREP sockets in DripDrop is different than other types of sockets, on_recv
208
+ # Receiving with XREP sockets in DripDrop is different than other types of sockets, on_receive
209
209
  # passes 2 arguments to its callback, +message+, and +response+. A minimal example is shown below:
210
210
  #
211
211
  #
212
- # zmq_xrep(z_addr, :bind).on_recv do |message,response|
212
+ # zmq_xrep(z_addr, :bind).on_receive do |message,response|
213
213
  # response.send_message(message)
214
214
  # end
215
215
  #
@@ -223,12 +223,12 @@ class DripDrop
223
223
  end
224
224
 
225
225
  # Binds an EM websocket server connection to +address+. takes blocks for
226
- # +on_open+, +on_recv+, +on_close+ and +on_error+.
226
+ # +on_open+, +on_receive+, +on_close+ and +on_error+.
227
227
  #
228
- # For example +on_recv+ could be used to echo incoming messages thusly:
228
+ # For example +on_receive+ could be used to echo incoming messages thusly:
229
229
  # websocket_server(addr).on_open {|conn|
230
230
  # ws.send_message(:name => 'ws_open_ack')
231
- # }.on_recv {|msg,conn|
231
+ # }.on_receive {|msg,conn|
232
232
  # conn.send(msg)
233
233
  # }.on_close {|conn|
234
234
  # }.on_error {|reason,conn|
@@ -249,7 +249,7 @@ class DripDrop
249
249
  end
250
250
 
251
251
  # Starts a new Thin HTTP server listening on address.
252
- # Can have an +on_recv+ handler that gets passed +msg+ and +response+ args.
252
+ # Can have an +on_receive+ handler that gets passed +msg+ and +response+ args.
253
253
  # http_server(addr) {|msg,response| response.send_message(msg)}
254
254
  def http_server(address,opts={},&block)
255
255
  uri = URI.parse(address)
@@ -1,3 +1,3 @@
1
1
  class DripDrop
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -16,7 +16,7 @@ describe "http" do
16
16
 
17
17
  client = http_client(addr)
18
18
 
19
- server = http_server(addr).on_recv do |message,response|
19
+ server = http_server(addr).on_receive do |message,response|
20
20
  $stdout.flush
21
21
  responses << message
22
22
  response.send_message(message)
@@ -21,7 +21,7 @@ describe "websockets" do
21
21
  server.on_open do |conn|
22
22
  conn.send_message(open_message)
23
23
  seen_signatures << conn.signature
24
- end.on_recv do |message,conn|
24
+ end.on_receive do |message,conn|
25
25
  received << message
26
26
  conn.send_message(message)
27
27
  end.on_close do |conn|
@@ -38,12 +38,12 @@ describe "zmq m2" do
38
38
 
39
39
  dd = zmq_m2([addr, addr2])
40
40
 
41
- dd.on_recv do |req|
41
+ dd.on_receive do |req|
42
42
  requests << req
43
43
  dd.reply_http req, "Hello from DripDrop"
44
44
  end
45
45
 
46
- m2_recv.on_recv do |msg|
46
+ m2_recv.on_receive do |msg|
47
47
  responses << msg
48
48
  end
49
49
 
@@ -16,11 +16,11 @@ describe "zmq push/pull" do
16
16
  pull2 = zmq_pull(addr, :connect)
17
17
  pull = [pull1, pull2]
18
18
 
19
- pull1.on_recv do |message|
19
+ pull1.on_receive do |message|
20
20
  message.head['recv_sock'] = 1
21
21
  responses << message
22
22
  end
23
- pull2.on_recv do |message|
23
+ pull2.on_receive do |message|
24
24
  message.head['recv_sock'] = 2
25
25
  responses << message
26
26
  end
@@ -13,7 +13,7 @@ describe "zmq xreq/xrep" do
13
13
  rep = zmq_xrep(addr, :bind)
14
14
  req = zmq_xreq(addr, :connect)
15
15
 
16
- rep.on_recv do |message,response|
16
+ rep.on_receive do |message,response|
17
17
  recvd << {:message => message, :response => response}
18
18
 
19
19
  response.send_message :name => 'response', :body => {'orig_name' => message.name}
@@ -83,7 +83,7 @@ describe "zmq xreq/xrep" do
83
83
  req1 = zmq_xreq(addr, :connect)
84
84
  req2 = zmq_xreq(addr, :connect)
85
85
 
86
- rep.on_recv do |message,response|
86
+ rep.on_receive do |message,response|
87
87
  response.send_message(message)
88
88
  end
89
89
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dripdrop
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.10.0
5
+ version: 0.11.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Cholakian
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-12 23:00:00 -08:00
13
+ date: 2011-03-20 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -111,6 +111,7 @@ files:
111
111
  - example/xreq_xrep.rb
112
112
  - js/dripdrop.html
113
113
  - js/dripdrop.js
114
+ - js/jquery.json.js
114
115
  - js/qunit.css
115
116
  - js/qunit.js
116
117
  - lib/dripdrop.rb