Class: Dorsal::ImplementationServer

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped, Privates
Defined in:
lib/dorsal/implementation.rb

Overview

Note:

should NOT be instantiate

Note:

this classe is made to be instantiate as a ring server

the Ring Server DRbObject Implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Privates

#daemonize, #start, #status, #stop

Constructor Details

#initialize(_options = {}) ⇒ ImplementationServer

Note:

:description (default) ‘Dorsal::DEFAULT_RINGSERVER_DESCRIPTION’

Note:

:debug (default) ‘Dorsal::DEFAULT_DEBUG’

Note:

:host (default) ‘Dorsal::DEFAULT_HOST’

Note:

:port (default) ‘Dorsal::DEFAULT_PORT’

Note:

:dir (default) ‘Dorsal::DEFAULT_DIR’

Note:

:name (default) ‘Dorsal::DEFAULT_RINGSERVER_NAME’

Note:

:uri rule ‘druby://(:host):(:port)’

Note:

:pid_file rule ‘(:dir)/(:name).pid’

Note:

DO NOT USE DIRECTLY

the contructor of the Ring Server

Parameters:

  • _options (Hash) (defaults to: {})

    the params of the constructor, keys must be symbols

Options Hash (_options):

  • :description (String)

    the description of ring server

  • :debug (TrueClass, FalseClass)

    the deubg mode

  • :host (String)

    the host for ring server and services

  • :port (String)

    the port for the ring server

  • :dir (String)

    the writable path for pids files

  • :name (String)

    the ring server name

  • :uri (String)

    the defined uri for ring server

  • :pid_file (String)

    the defined pid_file for ring server



46
47
48
49
50
51
52
53
54
# File 'lib/dorsal/implementation.rb', line 46

def initialize(_options = {})
  @options = Methodic::get_options(_options)
  @options.specify_defaults_values :name => 'ringserver',
  :host => 'localhost',
  :debug => false,
  :dir => '/tmp/dorsal'
  @options.merge
  @data ={}
end

Instance Attribute Details

#dataObject (readonly)

Note:

for debug only



25
26
27
# File 'lib/dorsal/implementation.rb', line 25

def data
  @data
end

Instance Method Details

#bind_to_service(_options = {}) ⇒ DRbObject?

Note:

access by Dorsal::Controller::new.bind_to_ring.bind_to_service

bind to a service from the ring server

Examples:

usage

Dorsal::Controller::new.bind_to_ring.bind_to_service :name => 'service'

Parameters:

  • _options (Hash) (defaults to: {})

    the params of the constructor, keys must be symbols

Options Hash (_options):

  • :name (String)

    the name of the service

Returns:

  • (DRbObject, nil)

    the Distributed Service, nil if service not in the Ring



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dorsal/implementation.rb', line 116

def bind_to_service(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :name
  options.validate
  if list_services.include?(options[:name]) then
    DRb.start_service
    return DRbObject.new nil, @data[options[:name]][:uri]
  else
    return nil
  end
end

#destroy_service(_options = {}) ⇒ TrueClass, FalseClass

Note:

access by Dorsal::Controller::new.bind_to_ring.destroy_service

stop a service in the ring

Examples:

usage

Dorsal::Controller::new.bind_to_ring.destroy_service :name => 'service'

Parameters:

  • _options (Hash) (defaults to: {})

    the params of the constructor, keys must be symbols

Options Hash (_options):

  • :name (String)

    the name of the service

Returns:

  • (TrueClass, FalseClass)

    true if really stop, false if already down



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dorsal/implementation.rb', line 92

def destroy_service(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :name
  options.validate
  if @data.include? options[:name] then
    options[:pid_file] = @data[options[:name]][:pid_file]
    options[:description] = @data[options[:name]][:description]
    if stop(options) then
      @data.delete(options[:name])  
      return true
    end
    return false
  end
  return false
end

#list_servicesHash

Note:

access by Dorsal::Controller::new.bind_to_ring.list_services

list the services from the ring server

Examples:

usage

Dorsal::Controller::new.bind_to_ring.list_services

Returns:

  • (Hash)

    the structured list of services in the ring



133
134
135
# File 'lib/dorsal/implementation.rb', line 133

def list_services
  return @data
end

#start_service(_options = {}) ⇒ Fixnum, FalseClass

Note:

access by Dorsal::Controller::new.bind_to_ring.start_service

start a service from the ring server

Examples:

usage

Dorsal::Controller::new.bind_to_ring.start_service :name => 'service', :description => 'a service', :object => MyService::new

Parameters:

  • _options (Hash) (defaults to: {})

    the params of the constructor, keys must be symbols

Options Hash (_options):

  • :name (String)

    the name of the service

  • :description (String)

    the long name of the service, use for $0

  • :object (Object)

    an object to be served by DRb

Returns:

  • (Fixnum, FalseClass)

    the pid of the process who host the DRb service, false if already started



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dorsal/implementation.rb', line 66

def start_service(_options = {})
  options = Methodic::get_options(_options)
  options.specify_presences_of :name, :description, :object
  options.validate
  unless @data.include?(options[:name]) then
  options[:pid_file] = "#{@options[:dir]}/service-#{options[:name]}.pid"
  options[:uri] = "druby://#{@options[:host]}:#{get_free_port(40000,50000)}"
    @data[options[:name]] = { :description => options[:description] , :pid_file => options[:pid_file], :uri => options[:uri] }
    return start(options) do
      require 'drb'
      options[:object].extend DRb::DRbUndumped
      DRb.start_service(options[:uri], options[:object])
      DRb.thread.join
    end
  else
    return false
  end 
end