Class: NetService

Inherits:
Application show all
Includes:
DRbUndumped
Defined in:
lib/appswarm/net_service.rb

Instance Attribute Summary collapse

Attributes inherited from Application

#cluster, #running

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Application

#appswarmBaseDir, #config, #getApp, #getAppDataPath, #getAppDataPaths, #getAppDataWritePath, #getAppDir, getAppDir, #getAppId, #getAppName, #getService, #info, #log, #monitor, protect, provides, #provides?, providing, #providingAPIs, #quit, #quit?, requirements, requires, #retrieveConfig, #shortName, #startup, #warning, #withApp, #withAppFork

Constructor Details

#initialize(cluster, options) ⇒ NetService

Returns a new instance of NetService.



9
10
11
12
13
14
15
# File 'lib/appswarm/net_service.rb', line 9

def initialize(cluster,options)
  super
  @port=options[:port]
  @uri="druby://127.0.0.1:#{@port}"
  @drb=nil
  @valid=false
end

Instance Attribute Details

#validObject (readonly)

Returns the value of attribute valid.



7
8
9
# File 'lib/appswarm/net_service.rb', line 7

def valid
  @valid
end

Class Method Details

.checkPort(port) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/appswarm/net_service.rb', line 59

def self.checkPort(port)
  begin
    p=TCPServer.new('127.0.0.1',port)
    p.close
    return true
  rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR, Errno::EAFNOSUPPORT,Errno::EADDRINUSE
    return false
  end
end

Instance Method Details

#cronObject



56
57
# File 'lib/appswarm/net_service.rb', line 56

def cron
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appswarm/net_service.rb', line 27

def run
  assert{@drb.nil?}
  @thread=Thread.new {
    begin
      loop do
       begin
          this=self
          log "NetServer starting #{@uri}"
          @drb=DRb.start_service(@uri,this)
          log "NetServer started"
          @valid=true
          break
        rescue Errno::EADDRINUSE=>e
          log "#{self.class} could not be started - port blocked"
          sleep(20)
        end
      end
    
      loop do
        cron
        sleep(10)
      end
    rescue Exception=>e
      pp e.backtrace
      #pp caller
    end
  } 
end

#shutdownObject



16
17
18
19
20
21
22
23
# File 'lib/appswarm/net_service.rb', line 16

def shutdown
  assert{@valid}
  log "NetServer Shutting down service #{@uri}"
  @drb.stop_service if @drb
  @thread.kill if @thread
  log "NetServer Shut down service #{@uri}"
  assert { NetService::checkPort(@port) }
end

#stopObject



24
25
26
# File 'lib/appswarm/net_service.rb', line 24

def stop
  shutdown
end