Class: Citrus::Common::Service::ChannelService

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/citrus/common/service/channel_service.rb

Overview

ChannelService

Defined Under Namespace

Modules: Util Classes: Channel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ ChannelService

Initialize the service

Parameters:

  • app (Object)
  • args (Hash) (defaults to: {})


266
267
268
269
270
271
272
273
# File 'lib/citrus/common/service/channel_service.rb', line 266

def initialize app, args={}
  @app = app
  @channels = {}
  @prefix = args[:prefix]
  @store = args[:store]
  @broadcast_filter = args[:broadcast_filter]
  @channel_remote = Remote::Frontend::ChannelRemote.new app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



260
261
262
# File 'lib/citrus/common/service/channel_service.rb', line 260

def app
  @app
end

#channel_remoteObject (readonly)

Returns the value of attribute channel_remote.



260
261
262
# File 'lib/citrus/common/service/channel_service.rb', line 260

def channel_remote
  @channel_remote
end

#channelsObject (readonly)

Returns the value of attribute channels.



260
261
262
# File 'lib/citrus/common/service/channel_service.rb', line 260

def channels
  @channels
end

#prefixObject (readonly)

Returns the value of attribute prefix.



260
261
262
# File 'lib/citrus/common/service/channel_service.rb', line 260

def prefix
  @prefix
end

#storeObject (readonly)

Returns the value of attribute store.



260
261
262
# File 'lib/citrus/common/service/channel_service.rb', line 260

def store
  @store
end

Instance Method Details

#broadcast(server_type, route, msg, args, &block) ⇒ Object

Broadcast message to all the connected clients

Parameters:

  • server_type (String)
  • route (String)
  • message (Hash)
  • args (Hash)


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/citrus/common/service/channel_service.rb', line 338

def broadcast server_type, route, msg, args, &block
  namespace = 'sys'
  service = 'channelRemote'
  method = 'broadcast'
  servers = @app.get_servers_by_type server_type

  unless servers && servers.length != 0
    # server list is empty
    block_given? and yield
  end

  count = servers.length
  success_flag = false

  latch = Utils::CountDownLatch.new(count) {
    unless success_flag
      block_given? and yield Exception.new 'broadcast failed'
      return
    end
    block_given? and yield nil
  }

  gen_cb = Proc.new { |server_id|
    Proc.new { |err|
      if err
        latch.done
        return
      end
      success_flag = true
      latch.done
    }
  }

  send_message = Proc.new { |server_id|
    if server_id == @app.server_id
      @channel_remote.send method, route, msg, args, &gen_cb.call
    else
      @app.rpc_invoke(server_id, {
        :namespace => namespace,
        :service => service,
        :method => method,
        :args => [route, msg, args]
      }, &gen_cb.call(server_id))
    end
  }

  args = { :type => 'broadcast', :user_args => args || {} }

  servers.each { |server|
    send_message server[:server_id]
  }
end

#create_channel(name) ⇒ Object

Create channel with name

Parameters:

  • name (String)


283
284
285
286
287
288
289
290
# File 'lib/citrus/common/service/channel_service.rb', line 283

def create_channel name
  return @channels[name] if @channels[name]

  c = Channel.new name
  add_to_store self, gen_key(self), gen_key(self, name)
  @channels[name] = c
  c
end

#destroy_channel(name) ⇒ Object

Destroy channel by name

Parameters:

  • name (String)


308
309
310
311
312
# File 'lib/citrus/common/service/channel_service.rb', line 308

def destroy_channel name
  @channels.delete name
  remove_from_store self, gen_key(self), gen_key(self, name)
  remove_all_from_store self, gen_key(self, name)
end

#get_channel(name, create = false) ⇒ Object

Get channel by name

Parameters:

  • name (String)
  • create (Boolean) (defaults to: false)


296
297
298
299
300
301
302
303
# File 'lib/citrus/common/service/channel_service.rb', line 296

def get_channel name, create=false
  channel = @channels[name]
  if !channel && create
    channel = @channels[name] = Channel.new name
    add_to_store self, gen_key(self), gen_key(self, name)
  end
  channel
end

#push_message_by_uids(route, msg, uids, args, &block) ⇒ Object

Push message by uids

Parameters:

  • route (String)
  • msg (Hash)
  • uids (Array)
  • args (Hash)


320
321
322
323
324
325
326
327
328
329
330
# File 'lib/citrus/common/service/channel_service.rb', line 320

def push_message_by_uids route, msg, uids, args, &block
  unless uids && uids.length != 0
    block_given? and yield Exception.new 'uids should not be empty'
    return
  end

  groups = {}
  uids.each { |record| add record[:uid], record[:sid], groups }

  send_message_by_group self, route, msg, groups, args, &block
end

#start(&block) ⇒ Object

Start the service



276
277
278
# File 'lib/citrus/common/service/channel_service.rb', line 276

def start &block
  restore_channel self, &block
end