Permalink
Browse files

wrote hurricane convience classes and updated examples

  • Loading branch information...
1 parent 8ffc39a commit 528e6dfabe85571640ddd825511e2a4771a83859 @icheishvili icheishvili committed Jan 19, 2012
Showing with 142 additions and 27 deletions.
  1. +2 −2 drivers/ruby/erl_codec.rb
  2. +107 −0 drivers/ruby/hurricane.rb
  3. +19 −5 examples/tcp_example.rb
  4. +14 −20 examples/tcp_server_example.rb
@@ -1149,7 +1149,7 @@ def close()
end
# Receive one message from Hurricane.
- def recv()
+ def do_recv()
message_len = @stream.read(4)
if message_len.bytesize() < 4
@@ -1163,7 +1163,7 @@ def recv()
end
# Send one message to Hurricane.
- def send(message)
+ def do_send(message)
@stream_wrapper.clear()
Erlang::encode(message, @stream_wrapper)
@stream.write([@stream_wrapper.data.bytesize()].pack('N'))
View
@@ -0,0 +1,107 @@
+$:.push(File.dirname(File.expand_path(__FILE__)))
+
+require 'erl_codec'
+
+# Defines the module where all Hurricane-related logic will go.
+module Hurricane
+end
+
+# Implements a message type that can be used to message easily with
+# Hurricane.
+class Hurricane::Message
+
+ attr_accessor :type, :destination, :tag, :data, :timeout
+
+ # Initializes the object with default values.
+ def initialize()
+ @type = ''
+ @destination = ''
+ @tag = ''
+ @data = ''
+ @timeout = 10000
+ end
+
+ # Return a human-readble representation of the object.
+ def to_s()
+ {
+ 'type' => @type,
+ 'destination' => @destination,
+ 'tag' => @tag,
+ 'data' => @data,
+ 'timeout' => @timeout
+ }.to_s()
+ end
+
+ # Compares self to another object for equality.
+ def eql?(other)
+ @type.eql?(other.type) &&
+ @destination.eql?(other.destination) &&
+ @tag.eql?(other.tag) &&
+ @data.eql?(other.data) &&
+ @timeout.eql?(other.timeout)
+ end
+
+ # Uses eql?() to compare self to another object for equality.
+ def ==(other)
+ eql?(other)
+ end
+
+end
+
+
+# Extends the Erlang Gateway to provide Hurricane-specific
+# functionality.
+class Hurricane::Gateway < Erlang::Gateway
+
+ # Pass along all arguments to the parent class.
+ def initialize(*args)
+ super(*args)
+ end
+
+ # Register with a named group in the Hurricane system.
+ def register_server(name)
+ do_send(Erlang::Tuple.new([
+ Erlang::Atom.new('register_with_group'),
+ Erlang::Atom.new(name)]))
+ end
+
+ # When running over Standard I/O, indicate that the process has
+ # successfully started up and is ready to receive requests.
+ def send_ready_signal()
+ do_send(Erlang::Tuple.new([Erlang::Atom.new('ready')]))
+ end
+
+ # Receive the next Hurricane message, turn it into a Message object.
+ def do_recv()
+ data = super()
+ message = Hurricane::Message.new()
+ message.type = data.data[0].name
+ message.destination = data.data[1]
+ message.tag = data.data[2]
+ message.data = data.data[3]
+ message
+ end
+
+ # Turn a message object into a Hurricane message and send it.
+ def do_send(message)
+ if message.instance_of?(Hurricane::Message)
+ destination = message.destination
+ if destination.instance_of?(String)
+ destination = Erlang::Atom.new(destination)
+ end
+
+ data = Erlang::Tuple.new([
+ Erlang::Atom.new(message.type),
+ destination,
+ message.tag,
+ message.data,
+ message.timeout])
+ else
+ data = message
+ end
+
+ super(data)
+ end
+
+end
+
View
@@ -1,11 +1,25 @@
#!/usr/bin/env ruby
-$:.push(File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'drivers', 'ruby'))
+$:.push(
+ File.join(
+ File.dirname(File.dirname(File.expand_path(__FILE__))),
+ 'drivers',
+ 'ruby'
+ )
+)
+
require 'erl_codec'
+require 'hurricane'
-s = Erlang::SocketWrapper.new('localhost', 3308)
-gateway = Erlang::Gateway.new(s)
+gateway = Hurricane::Gateway.new(
+ Erlang::SocketWrapper.new('localhost', 3307))
loop do
- gateway.send(Erlang::Tuple.new([Erlang::Atom.new('request'), Erlang::Atom.new('time_server'), Erlang::Atom.new('time_message'), nil]))
- puts gateway.recv().inspect()
+ request = Hurricane::Message.new()
+ request.type = 'request'
+ request.destination = 'time_server'
+ request.tag = 0
+ request.data = nil
+
+ gateway.do_send(request)
+ puts gateway.do_recv().inspect()
end
@@ -7,27 +7,21 @@
'ruby'
)
)
+
require 'erl_codec'
+require 'hurricane'
-gateway = Erlang::Gateway.new(Erlang::SocketWrapper.new('localhost', 3307))
-gateway.send(
- Erlang::Tuple.new(
- [
- Erlang::Atom.new('register_with_group'),
- Erlang::Atom.new('time_server')
- ]
- )
-)
+gateway = Hurricane::Gateway.new(
+ Erlang::SocketWrapper.new('localhost', 3307))
+gateway.register_server('time_server')
loop do
- message = gateway.recv()
- gateway.send(
- Erlang::Tuple.new(
- [
- Erlang::Atom.new('response'),
- message.data[1],
- message.data[2],
- Time.new().inspect()
- ]
- )
- )
+ request = gateway.do_recv()
+
+ response = Hurricane::Message.new()
+ response.type = 'response'
+ response.destination = request.destination
+ response.tag = request.tag
+ response.data = Time.new.inspect()
+
+ gateway.do_send(response)
end

0 comments on commit 528e6df

Please sign in to comment.