Module: EventMachine::Wssh::Service

Included in:
Connect, Ephemeral, Server, TLS
Defined in:
lib/em/wssh/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/em/wssh/service.rb', line 5

def options
  @options
end

Instance Method Details

#daemonize!Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/em/wssh/service.rb', line 80

def daemonize!
  throw 'Cannot daemonize on Windows!' if Gem.win_platform?

  log "Going on in background..."

  f = File.open mkdir(:log), 'a'

  STDIN.reopen '/dev/null'
  STDOUT.reopen f
  STDERR.reopen f

  Process.daemon true, true
end

#daemonize?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/em/wssh/service.rb', line 94

def daemonize?
  daemonize! if options[:daemon]
end

#getoptObject



26
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
55
56
57
58
59
60
61
62
# File 'lib/em/wssh/service.rb', line 26

def getopt
  require 'getoptlong'
  opts = GetoptLong.new(
    ['-l', '--listen', GetoptLong::REQUIRED_ARGUMENT],
    ['-b', '--base', GetoptLong::REQUIRED_ARGUMENT],
    ['-d', '--daemon', GetoptLong::NO_ARGUMENT],
    ['-a', '--all', GetoptLong::NO_ARGUMENT],
    ['-v', '--version', GetoptLong::NO_ARGUMENT],
    ['-p', '--ping', GetoptLong::OPTIONAL_ARGUMENT],
  )
  begin
    opts.each do |opt, arg|
      case opt
      when '-d'
        options[:daemon]=true
      when '-l'
        options[:port]=arg
      when '-b'
        options[:base]=File.expand_path arg
      when '-a'
        options[:host]='0.0.0.0'
      when '-p'
        arg=arg.to_i
        options[:ping]= arg<1 ? 50 : arg
      when '-v'
        puts VERSION
        exit 1
      end
    end
  rescue
    help
  end
  args=options[:args]
  args=args.nil? ? [] : [args] unless Array===args
  help if args.length!=ARGV.length
  args.each{|arg| options[arg]=ARGV.shift}
end

#go!Object



112
113
114
115
116
117
118
# File 'lib/em/wssh/service.rb', line 112

def go!
  getopt
  daemonize?
  log "Listening on #{options[:host]}:#{options[:port]}"
  pid
  loop!
end

#helptionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/em/wssh/service.rb', line 12

def helptions
  puts <<-EOF

-a --all         Listen to all interfaces
-b --base=dir    Set home directory
-d --daemon      Run daemonized
-h --help        Show this help
-l --listen=port Listen to port
-p --ping[=sec]  Periodic ping
-v --version     Show version
EOF
  exit 1
end

#homebaseObject



64
65
66
67
68
# File 'lib/em/wssh/service.rb', line 64

def homebase
  x = File.expand_path '..', __FILE__
  x = File.dirname x until File.exists? File.join x, 'Gemfile'
  x
end

#log(*msg) ⇒ Object



7
8
9
10
# File 'lib/em/wssh/service.rb', line 7

def log *msg
  msg.unshift "[#{Time.now}]"
  print msg*' '+"\n"
end

#loop!Object



106
107
108
109
110
# File 'lib/em/wssh/service.rb', line 106

def loop!
  self::Need.each{|f| require f}
  STDOUT.sync=true
  EM.run{ listen! }
end

#mkdir(sym) ⇒ Object



74
75
76
77
78
# File 'lib/em/wssh/service.rb', line 74

def mkdir(sym)
  require 'fileutils'
  FileUtils.mkdir_p File.dirname file=(path sym)
  file
end

#path(sym) ⇒ Object



70
71
72
# File 'lib/em/wssh/service.rb', line 70

def path(sym)
  File.join options[:base]||=homebase, options[sym]
end

#pidObject



98
99
100
101
102
103
104
# File 'lib/em/wssh/service.rb', line 98

def pid
  File.write p=mkdir(:pid), $$
  at_exit do
    log "Exiting..."
    File.unlink p
  end
end