lib/arborist/client.rb in arborist-0.1.0 vs lib/arborist/client.rb in arborist-0.2.0.pre20170519125456

- old
+ new

@@ -1,12 +1,14 @@ # -*- ruby -*- #encoding: utf-8 -require 'arborist' unless defined?( Arborist ) require 'msgpack' +require 'arborist' unless defined?( Arborist ) +require 'arborist/tree_api' + # Unified Arborist Manager client for both the Tree and Event APIs class Arborist::Client extend Loggability # The version of the client. @@ -28,14 +30,14 @@ ###### public ###### - # The ZMQ URI required to speak to the Arborist tree API. + # The ZeroMQ URI required to speak to the Arborist tree API. attr_accessor :tree_api_url - # The ZMQ URI required to speak to the Arborist event API. + # The ZeroMQ URI required to speak to the Arborist event API. attr_accessor :event_api_url # # High-level methods @@ -85,11 +87,11 @@ end ### Return the manager's current status as a hash. def make_status_request - return self.pack_message( :status ) + return Arborist::TreeAPI.request( :status ) end ### Return the manager's current node tree. def list( **args ) @@ -103,28 +105,28 @@ header = {} self.log.debug "From is: %p" % [ from ] header[:from] = from if from header[:depth] = depth if depth - return self.pack_message( :list, header ) + return Arborist::TreeAPI.request( :list, header ) end ### Return the manager's current node tree. - def fetch( criteria={}, **args ) - request = self.make_fetch_request( criteria, **args ) + def fetch( criteria={}, options={} ) + request = self.make_fetch_request( criteria, **options ) return self.send_tree_api_request( request ) end ### Return the manager's current node tree. def make_fetch_request( criteria, include_down: false, properties: :all, exclude: {} ) header = {} header[ :include_down ] = true if include_down header[ :return ] = properties if properties != :all - return self.pack_message( :fetch, header, [ criteria, exclude ] ) + return Arborist::TreeAPI.request( :fetch, header, [ criteria, exclude ] ) end ### Update the identified nodes in the manager with the specified data. def update( *args ) @@ -134,19 +136,19 @@ end ### Update the identified nodes in the manager with the specified data. def make_update_request( data ) - return self.pack_message( :update, nil, data ) + return Arborist::TreeAPI.request( :update, nil, data ) end ### Add a subscription def subscribe( **args ) request = self.make_subscribe_request( **args ) response = self.send_tree_api_request( request ) - return response.first + return response['id'] end ### Make a subscription request for the specified +criteria+, +identifier+, and +event_type+. def make_subscribe_request( criteria: {}, identifier: nil, event_type: nil, exclude: {} ) @@ -154,11 +156,11 @@ [ identifier, event_type, criteria ] header = {} header[ :identifier ] = identifier if identifier header[ :event_type ] = event_type - return self.pack_message( :subscribe, header, [ criteria, exclude ] ) + return Arborist::TreeAPI.request( :subscribe, header, [ criteria, exclude ] ) end ### Remove a subscription def unsubscribe( *args ) @@ -170,11 +172,11 @@ ### Remove the subscription with the specified +subid+. def make_unsubscribe_request( subid ) self.log.debug "Making unsubscribe request for subid: %s" % [ subid ] - return self.pack_message( :unsubscribe, subscription_id: subid ) + return Arborist::TreeAPI.request( :unsubscribe, subscription_id: subid ) end ### Remove a node def prune( *args ) @@ -186,11 +188,11 @@ ### Remove the node with the specified +identfier+. def make_prune_request( identifier ) self.log.debug "Making prune request for identifier: %s" % [ identifier ] - return self.pack_message( :prune, identifier: identifier ) + return Arborist::TreeAPI.request( :prune, identifier: identifier ) end ### Add a new node to the tree. def graft( *args ) @@ -211,11 +213,11 @@ identifier: identifier, parent: parent, type: type } - return self.pack_message( :graft, header, attributes ) + return Arborist::TreeAPI.request( :graft, header, attributes ) end ### Modify operational attributes of a node. def modify( *args ) @@ -227,24 +229,24 @@ ### Modify the operations +attributes+ of the node with the specified +identifier+. def make_modify_request( identifier, attributes={} ) self.log.debug "Making modify request for identifer: %s" % [ identifier ] - return self.pack_message( :modify, {identifier: identifier}, attributes ) + return Arborist::TreeAPI.request( :modify, {identifier: identifier}, attributes ) end ### Send the packed +request+ via the Tree API socket, raise an error on ### unsuccessful response, and return the response body. def send_tree_api_request( request ) self.log.debug "Sending request: %p" % [ request ] - self.tree_api.send( request ) + request.send_to( self.tree_api ) - res = self.tree_api.recv + res = CZTop::Message.receive_from( self.tree_api ) self.log.debug "Received response: %p" % [ res ] - header, body = self.unpack_message( res ) + header, body = Arborist::TreeAPI.decode( res ) unless header[ 'success' ] raise "Arborist manager said: %s" % [ header['reason'] ] end return body @@ -253,43 +255,21 @@ # # Utility methods # - ### Format ruby +data+ for communicating with the Arborist manager. - def pack_message( verb, *data ) - header = data.shift || {} - body = data.shift - - header.merge!( action: verb, version: API_VERSION ) - - self.log.debug "Packing message; header: %p, body: %p" % [ header, body ] - - return MessagePack.pack([ header, body ]) - end - - - ### De-serialize an Arborist manager response. - def unpack_message( msg ) - return MessagePack.unpack( msg ) - end - - - ### Return a ZMQ REQ socket connected to the manager's tree API, instantiating + ### Return a ZeroMQ REQ socket connected to the manager's tree API, instantiating ### it if necessary. def tree_api return @tree_api ||= self.make_tree_api_socket end ### Create a new ZMQ REQ socket connected to the manager's tree API. def make_tree_api_socket self.log.info "Connecting to the tree socket %p" % [ self.tree_api_url ] - sock = Arborist.zmq_context.socket( :REQ ) - sock.connect( self.tree_api_url ) - - return sock + return CZTop::Socket::REQ.new( self.tree_api_url ) end ### Return a ZMQ SUB socket connected to the manager's event API, instantiating ### it if necessary. @@ -299,12 +279,9 @@ ### Create a new ZMQ SUB socket connected to the manager's event API. def make_event_api_socket self.log.info "Connecting to the event socket %p" % [ self.event_api_url ] - sock = Arborist.zmq_context.socket( :SUB ) - sock.connect( self.event_api_url ) - - return sock + return CZTop::Socket::SUB.new( self.event_api_url ) end end # class Arborist::Client