[Nitro] An implemention of Orbjson in Nitro

James Britt james_b at neurogami.com
Fri Mar 25 01:23:23 EST 2005


I assembled some  proof-of-concept code that allows one to create create 
services in Nitro with a minimum of fuss.

Note that this code only works with the copy of Orbjson on my laptop; 
I've added/changed some methods to make the library more flexible, but 
have not released a new version.

But you should be able to read this and see how well this might work out 
in real-life.  All the code is bundled up for convenience, though the 
actual packaging would hide most of this; the user would really only 
need to write the Example class, while the rest just happens via Nitro libs.

Keep in mind that this design is predicated in the idea that all 
json-rpc calls would use the same endpoint, regardless of the service 
invoked.


#-----------------------------------------------------
require 'nitro/controller'
require 'Orbjson'


module N

   class HyperActiveOrbjson
     include Orbjson::Common

      # Must have a call to init.
      # But seems a kludgey
     Orbjson::System.init( {} )
   end

   ORBJSON = HyperActiveOrbjson.new

   module JSONRPC_Service

     def self.extend_object( o )
       self.register( o.name )
     end

     def self.register( class_name )
       Logger.debug( " register( ! #{class_name}")
       ORBJSON.add_by_class_name(   class_name ) 

     end
   end
end



# The public URL for this service would be configurable.
# My copy of run.rb has this:
# dispatcher.mount( {  'service' => ServiceController  } )
# so the endpoint is /service/jsonrpc
#
class ServiceController < N::Controller

   def jsonrpc
     post = get_raw_post_data
     render_text( ORBJSON.process( post ) )
   end

private
   # Hack.
   def get_raw_post_data
     x = ''
     context.params.each { |k, v| x = k if v == nil }
     x
   end
end

# This is what the user writes to
# expose a class as a json-rpc service
class Example
   extend N::JSONRPC_Service

   def times2( x )
     x * 2
   end
end

#-------------------------------------------------

Question: should the Example class inherit from
N::Controller ? Such services do not respond directly back to the 
client; the results a marshaled through Orbjson, as all results have to 
go back to the client as json-rpc response messages.



James Britt
-- 

http://www.ruby-doc.org
http://www.rubyxml.com
http://catapult.rubyforge.com
http://orbjson.rubyforge.com
http://ooo4r.rubyforge.com
http://www.jamesbritt.com



More information about the Nitro-general mailing list