Class: Carnivore::UnixSocket::Util::Server
- Inherits:
-
Object
- Object
- Carnivore::UnixSocket::Util::Server
- Includes:
- Carnivore::Utils::Logging, Zoidberg::SoftShell, Zoidberg::Supervise
- Defined in:
- lib/carnivore-unixsocket/util/socket_server.rb
Instance Attribute Summary collapse
- #connections ⇒ Array<IO> readonly
- #notifier ⇒ IO readonly
-
#path ⇒ String
readonly
Socket path.
- #server ⇒ UNIXServer readonly
- #source ⇒ Source readonly
- #waker ⇒ IO readonly
Instance Method Summary collapse
-
#add_connection(con) ⇒ NilClass
Add a new connection.
-
#initialize(args = {}) ⇒ self
constructor
Create a new server.
-
#remove_connection(con) ⇒ NilClass
Remove a connection.
-
#setup_server! ⇒ UNIXServer
Setup the path for the server and create new server.
-
#start_collector! ⇒ Object
Start the message collector loop.
-
#start_server! ⇒ Object
Start the server listener loop.
Constructor Details
#initialize(args = {}) ⇒ self
Create a new server
31 32 33 34 35 36 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 31 def initialize(args={}) @path = ::File.(args[:path]) @source = args[:source] @connections = [] @waker, @notifier = IO.pipe end |
Instance Attribute Details
#connections ⇒ Array<IO> (readonly)
24 25 26 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 24 def connections @connections end |
#notifier ⇒ IO (readonly)
22 23 24 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 22 def notifier @notifier end |
#path ⇒ String (readonly)
Returns socket path.
14 15 16 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 14 def path @path end |
#server ⇒ UNIXServer (readonly)
16 17 18 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 16 def server @server end |
#source ⇒ Source (readonly)
18 19 20 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 18 def source @source end |
#waker ⇒ IO (readonly)
20 21 22 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 20 def waker @waker end |
Instance Method Details
#add_connection(con) ⇒ NilClass
Add a new connection
42 43 44 45 46 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 42 def add_connection(con) @connections.push(con) notifier.write '-' nil end |
#remove_connection(con) ⇒ NilClass
Remove a connection
52 53 54 55 56 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 52 def remove_connection(con) @connections.delete(con) notifier.write '-' nil end |
#setup_server! ⇒ UNIXServer
Setup the path for the server and create new server
90 91 92 93 94 95 96 97 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 90 def setup_server! unless(@server) if(::File.exists?(path)) ::File.delete(path) end @server = UNIXServer.new(path) end end |
#start_collector! ⇒ Object
Start the message collector loop
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 70 def start_collector! loop do IO.select(current_self.connections + [waker]).flatten.compact.each do |sock| if(sock == waker) sock.read else line = sock.gets if(line) source.signal(:message, line.strip) else sock.close end end end end end |
#start_server! ⇒ Object
Start the server listener loop
59 60 61 62 63 64 65 66 67 |
# File 'lib/carnivore-unixsocket/util/socket_server.rb', line 59 def start_server! setup_server! loop do debug 'Waiting for new connection to socket' connection = server.accept debug 'Received new connection to socket, loading in...' add_connection(connection) end end |