Class: Bridge::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/bridge-ruby.rb

Defined Under Namespace

Classes: SystemService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bridge

:port => nil

An integer specifying the port of the Bridge

server to connect to. Overrides +:redirector+ when both +:host+ and
+:port+ are specified.


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bridge-ruby.rb', line 69

def initialize(options = {})

  # Set default options
  @options = {
    :redirector => 'http://redirector.getbridge.com',
    :secure_redirector => 'https://redirector.getbridge.com',
    :secure => false,
    :reconnect  => true,
    :log  => 2, # 0 for no output
  }

  @options = @options.merge(options)

  if @options[:secure]
    @options[:redirector] = @options[:secure_redirector]
  end
  
  Util.set_log_level(@options[:log])
  
  @store = {}
  # Initialize system service call
  @store['system'] = SystemService.new(self)
  
  # Indicates whether server is connected and handshaken
  @is_ready = false
  
  # Create connection object
  @connection = Connection.new(self)
  
  # Store event handlers
  @events = {}

  @context = nil
end

Instance Attribute Details

#connectionObject

:nodoc:



34
35
36
# File 'lib/bridge-ruby.rb', line 34

def connection
  @connection
end

#contextObject

:nodoc:



34
35
36
# File 'lib/bridge-ruby.rb', line 34

def context
  @context
end

#is_readyObject

:nodoc:



34
35
36
# File 'lib/bridge-ruby.rb', line 34

def is_ready
  @is_ready
end

#optionsObject

:nodoc:



34
35
36
# File 'lib/bridge-ruby.rb', line 34

def options
  @options
end

#storeObject

:nodoc:



34
35
36
# File 'lib/bridge-ruby.rb', line 34

def store
  @store
end

Instance Method Details

#connect(&callback) ⇒ Object

:call-seq:

connect { block }

Starts the connection to the Bridge server.

If a block is given, calls the given block when Bridge is connected

and ready.


402
403
404
405
406
# File 'lib/bridge-ruby.rb', line 402

def connect &callback
  self.ready &callback if callback
  @connection.start
  return self
end

#emit(name, args = []) ⇒ Object

:nodoc:



166
167
168
169
170
171
172
# File 'lib/bridge-ruby.rb', line 166

def emit name, args=[] #:nodoc:
  if @events.key? name
    @events[name].each do |fn|
      fn.call *args
    end
  end
end

#execute(address, args) ⇒ Object

:nodoc:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bridge-ruby.rb', line 104

def execute address, args #:nodoc:
  # Retrieve stored handler
  obj = @store[address[2]]
  # Retrieve function in handler
  func = obj.method(address[3])
  if func
    last = args.last
    # If last argument is callable and function arity is one less than
    #   args length, pass in as block
    if (last.is_a? Proc) and func.arity == args.length - 1
      args.pop
      func.call *args do |*args, &blk|
        args << blk if blk
        last.call *args
      end
    else
      begin
        func.call *args
      rescue StandardError => err
        Util.error err
        Util.error "Exception while calling #{address[3]}(#{args})"
      end
    end
  else
    Util.warn "Could not find object to handle, #{address}"
  end
end

#get_channel(name, &callback) ⇒ Object

:call-seq:

get_channel(name) -> channel
get_channel(name) { |channel, name| block }

Retrives a channel from Bridge with the given name.

Calling a method on the channel object will result in the given

method being executed on all clients that have been joined to the
channel.

Note that if the requested channel does not exist or is empty, an

object is still returned, however attempts to make method calls on
the object will result in a remote error.

Returns the requested channel.

If a block is given, calls the given block with the requested channel

and channel name.

Attributes

name

The name of the Bridge channel being requested



309
310
311
312
313
314
315
316
# File 'lib/bridge-ruby.rb', line 309

def get_channel name, &callback
  # Send GETCHANNEL command in order to establih link for channel if
  #   client is not member
  @connection.send_command(:GETCHANNEL, {:name => name})
  ref = Reference.new(self, ['channel', name, "channel:#{name}"])
  callback.call(ref, name) if callback
  return ref
end

#get_client(id) ⇒ Object



282
283
284
# File 'lib/bridge-ruby.rb', line 282

def get_client id
  return Client.new(self, id)
end

#get_service(name, &callback) ⇒ Object

:call-seq:

get_service(name) -> service
get_service(name) { |service, name| block }

Retrives a service published to Bridge with the given name.

If multiple Bridge clients have a published a service, the service is

retrieved from one of the publishers round-robin.

Note that if the requested service does not exist, an object is still

returned, however attempts to make method calls on the object will
result in a remote error.

Returns the requested service.

If a block is given, calls the given block with the requested service

and service name.

Attributes

name

The name of the Bridge service being requested



276
277
278
279
280
# File 'lib/bridge-ruby.rb', line 276

def get_service name, &callback
  ref = Reference.new(self, ['named', name, name])
  callback.call(ref, name) if callback
  return ref
end

#join_channel(name, handler, writeable = true, &callback) ⇒ Object

handler

A remote object, ruby object or module to handle method

calls from the channel
writeable

Whether the handler’s creator may write to the channel



341
342
343
344
345
346
347
348
349
350
# File 'lib/bridge-ruby.rb', line 341

def join_channel name, handler, writeable = true, &callback
  @connection.send_command(
    :JOINCHANNEL,
    { :name => name,
      :handler => Serializer.serialize(self, handler),
      :callback => Serializer.serialize(self, callback),
      :writeable => writeable
    }
  )
end

#leave_channel(channel, handler, &callback) ⇒ Object

:call-seq:

leave_channel(name, handler)
leave_channel(name, handler) { |name| block }

Leaves a Bridge channel with the given name and handler object.

The given handler can be a remote object, in which case the Bridge

client that created the remote object will be removed from the
channel.

If a block is given, calls the given block with the name of the

channel left.

Attributes

name

The name of the Bridge channel to leave

handler

A remote object, ruby object or module that was used to

handle moethod calls from the channel


371
372
373
374
375
376
377
378
# File 'lib/bridge-ruby.rb', line 371

def leave_channel channel, handler, &callback
  @connection.send_command(
    :LEAVECHANNEL,
    { :name => name,
      :handler => Serializer.serialize(self, handler),
    :callback => Serializer.serialize(self, callback)}
  )
end

#on(name, &fn) ⇒ Object

:call-seq:

on(name) { |*args| block }

Adds the given block as a handler for the event specified by

<tt>name</tt>. Calling multiple times will result in multiple
handlers being attached to the event

Attributes

name

The name of the event for the given block to listen to

Events

List of events Bridge emits

'ready' ()

Bridge is connected and ready. Not emitted on

reconnects
'remoteError' (error_message)

A remote error has occurred

in Bridge. The error message is provided as a parameter


159
160
161
162
163
164
# File 'lib/bridge-ruby.rb', line 159

def on name, &fn
  if !@events.key? name
    @events[name] = [];
  end
  @events[name] << fn
end

#publish_service(name, handler, &callback) ⇒ Object

:call-seq:

publish_service(name, handler) { |name| block }

Publishes a ruby object or module as a Bridge service with the given

name.

If a block is given, calls the given block with the name of the

published service upon publish success

Attributes

name

The name of the Bridge service the handler will be published

with
handler

A ruby object or module to publish



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bridge-ruby.rb', line 195

def publish_service name, handler, &callback
  if name == 'system'
    Util.error("Invalid service name: #{name}")
  else
    @store[name] = handler
    @connection.send_command(
      :JOINWORKERPOOL,
      { :name => name,
        :callback => Serializer.serialize(self, callback)}
    )
  end
end

#ready(&callback) ⇒ Object

:call-seq:

ready { block }

Calls the given block when Bridge is connected and ready. Calls the given block immediately if Bridge is already ready.



386
387
388
389
390
391
392
# File 'lib/bridge-ruby.rb', line 386

def ready &callback
  if @is_ready
    callback.call
  else
    on 'ready', &callback
  end
end

#send(args, destination) ⇒ Object

:nodoc:



174
175
176
177
178
# File 'lib/bridge-ruby.rb', line 174

def send args, destination #:nodoc:
  @connection.send_command(:SEND,
                           { :args => Serializer.serialize(self, args),
                             :destination => destination })
end

#store_object(handler, ops) ⇒ Object

:nodoc:



132
133
134
135
136
137
138
# File 'lib/bridge-ruby.rb', line 132

def store_object handler, ops #:nodoc:
  # Generate random id for callback being stored
  name = Util.generate_guid
  @store[name] = handler
  # Return reference to stored callback
  Reference.new(self, ['client', @connection.client_id, name], ops)
end

#store_service(name, handler) ⇒ Object

:call-seq:

store_service(name, handler)

Stores a Ruby object or module as a Bridge service with the given

name.

Attributes

name

The name of the Bridge service the handler will be stored

under
handler

A Ruby object or module to store



220
221
222
223
224
225
226
# File 'lib/bridge-ruby.rb', line 220

def store_service name, handler
  if name == 'system'
    Util.error("Invalid service name: #{name}")
  else
    @store[name] = handler
  end
end

#unpublish_service(name, &callback) ⇒ Object

:call-seq:

unpublish_service(name, handler) { |name| block }

Stops publishing a ruby object or module as a Bridge service with the

given name.

If a block is given, calls the given block with the name of the

unpublished service.

Attributes

name

The name of the Bridge service that will no longer be

published


242
243
244
245
246
247
248
249
250
251
252
# File 'lib/bridge-ruby.rb', line 242

def unpublish_service name, &callback
  if name == 'system'
    Util.error("Invalid service name: #{name}")
  else
    @connection.send_command(
      :LEAVEWORKERPOOL,
      { :name => name,
        :callback => Serializer.serialize(self, callback)}
    )
  end
end