Class: Butler::Remote::Server

Inherits:
Object
  • Object
show all
Includes:
Log::Comfort
Defined in:
lib/butler/remote/server.rb

Instance Attribute Summary collapse

Attributes included from Log::Comfort

#logger

Instance Method Summary collapse

Methods included from Log::Comfort

#debug, #error, #exception, #fail, #info, #log, #warn

Constructor Details

#initialize(bot) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
16
17
18
# File 'lib/butler/remote/server.rb', line 9

def initialize(bot)
  @bot      = bot
  @host     = nil
  @port     = nil
  @clients  = []
  @server   = nil
  @logger   = nil
  @accepter = nil
  @lock     = Mutex.new
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/butler/remote/server.rb', line 6

def host
  @host
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/butler/remote/server.rb', line 7

def port
  @port
end

Instance Method Details

#accepterObject



74
75
76
77
78
79
80
81
82
# File 'lib/butler/remote/server.rb', line 74

def accepter
  while socket = @server.accept
    @lock.synchronize {
      @clients << Connection.open_thread(self, socket)
    }
  end
rescue => e
  exception(e)
end

#disconnected(connection) ⇒ Object



64
65
66
67
68
# File 'lib/butler/remote/server.rb', line 64

def disconnected(connection)
  @lock.synchronize {
    @clients.delete(connection)
  }
end

#dispatch(*args) ⇒ Object



56
57
58
# File 'lib/butler/remote/server.rb', line 56

def dispatch(*args)
  @bot.dispatch(*args)
end

#login(*args) ⇒ Object



52
53
54
# File 'lib/butler/remote/server.rb', line 52

def (*args)
  @bot.access.(*args)
end

#myselfObject



60
61
62
# File 'lib/butler/remote/server.rb', line 60

def myself
  @bot.myself
end

#running?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/butler/remote/server.rb', line 70

def running?
  @accepter ? true : false
end

#start(host = @host, port = @port) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/butler/remote/server.rb', line 33

def start(host=@host, port=@port)
  if @host != host || @port != port then
    @host   = host.freeze
    @port   = port
    @server = TCPServer.new(@host, @port)
    stop
  end
  @accepter ||= Thread.new(&method(:accepter))
end

#stopObject



43
44
45
46
47
48
49
50
# File 'lib/butler/remote/server.rb', line 43

def stop
  @lock.synchronize {
    @accepter.kill if @accepter
    @accepter = nil
    @clients.each { |client| client.quit("Server terminated") }
    @clients  = []
  }
end