Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed up message types. Can interface with NowJS now

  • Loading branch information...
commit bea592f3b210fafd15b6f7de06a85290848c0e66 2 parents ba96e02 + 491d44c
Sridatta Thatipamala sridatta authored
Showing with 62 additions and 26 deletions.
  1. +10 −4 examples/simple/simple.rb
  2. +13 −3 lib/ruby-now.rb
  3. +39 −19 lib/util.rb
14 examples/simple/simple.rb
View
@@ -2,9 +2,15 @@
EventMachine::run {
EventMachine::start_server "127.0.0.1", 8081, RubyNow
- RubyNow.registerFunction('aFunctionObject', lambda{|*a|
- puts a.join(' ')})
- RubyNow.registerFunction('otherFunc', lambda{|cb, *a|
- cb.call(a.join(' '))})
+ RubyNow.registerFunction('a', lambda{|*a|
+ puts a.join(' ')})
+ RubyNow.registerFunction('b', lambda{|cb, *a|
+ cb.call(a.join(' '))})
+
+ RubyNow.registerFunction('c', lambda{|cb|
+ fn = RubyNow.getGroupFunction("everyone", "now.callMe")
+ fn.call(cb)
+ })
+
puts 'Running TCP server on port 8081. Awaiting orders.'
}
16 lib/ruby-now.rb
View
@@ -10,21 +10,31 @@ module RubyNow
def self.conn
@@conn
end
- def self.remoteCall name
- Util.remoteCall name
+
+ def self.getUserFunction user, fqn
+ Util.makeUsercaller user, fqn
+ end
+
+ def self.getGroupFunction group, fqn
+ Util.makeMulticaller group, fqn
end
+
def self.registerFunction name, block
Util.registerFunction name, block
end
+
def initialize
@@conn.push self
end
+
def post_init
- send_data Util.functions.keys.to_json
+ #send_data Util.functions.keys.to_json
end
+
def receive_data data
Util.processData(JSON.parse data)
end
+
def unbind
@@conn.delete self
end
58 lib/util.rb
View
@@ -16,40 +16,60 @@ def self.processData hash
case hash['type']
when 'rfc'
self.callFunc(@@functions[hash['fqn']], hash['args'])
+ when 'closurecall'
+ self.callFunc(@@closures[hash['fqn']], hash['args']);
when 'new'
+ self.send_data({"type" => "functionList",
+ "functions" => @@functions.keys}.to_json)
puts "Accepted new connection"
else
puts 'unidentified json, json'
end
end
+ def self.serializeArgs args
+ args.collect {|a|
+ ((a.is_a?(Hash) && a.has_key?('fqn')) ?
+ (@@functions.has_key?(a['fqn']) ?
+ @@functions[a['fqn']] :
+ (@@closures.has_key?(a['fqn']) ?
+ @@closures[a['fqn']] :
+ self.makeClosure(a['fqn']))):
+ (a.is_a?(Proc) ?
+ (((@@closures[a.to_s] = a) && false) ||
+ {"fqn" => a.to_s}) :
+ a))
+ }
+ end
+
def self.callFunc func, args
- func.call *(args.collect{|a|
- ((a.is_a?(Hash) && a.has_key?('fqn')) ?
- (@@functions.has_key?(a['fqn']) ?
- @@functions[a['fqn']] :
- (@@closures.has_key?(a['fqn']) ?
- @@closures[a['fqn']] :
- self.makeClosure(a['fqn']))):
- (a.is_a?(Proc) ?
- (((@@closures[a.to_s] = a) && true) ||
- {"fqn" => a.to_s}) :
- a))
- });
+ func.call *(self.serializeArgs args)
end
- def self.doMulticall group, fqn, args
- self.send_data({"type" => "multicall",
- "fqn" => fqn,
- "args" => args}.to_json)
+ def self.makeUsercaller user, fqn
+ lambda{|*args|
+ self.send_data({ "type" => "usercall",
+ "id" => user,
+ "fqn" => fqn,
+ "args" => (self.serializeArgs args)}.to_json)
+ }
+ end
+
+ def self.makeMulticaller group, fqn
+ lambda{|*args|
+ self.send_data({ "type" => "multicall",
+ "groupName" => group,
+ "fqn" => fqn,
+ "args" => (self.serializeArgs args)}.to_json)
+ }
end
def self.makeClosure a
- (lambda{|*args|
+ lambda{|*args|
self.send_data({ "type" => "closurecall",
"fqn" => a,
- "args" => args}.to_json)
- })
+ "args" => (self.serializeArgs args)}.to_json)
+ }
end
def self.send_data str
Please sign in to comment.
Something went wrong with that request. Please try again.