Class: SocketService
- Inherits:
-
Object
- Object
- SocketService
- Defined in:
- lib/rubyslim/socket_service.rb
Instance Attribute Summary collapse
-
#closed ⇒ Object
readonly
Returns the value of attribute closed.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize ⇒ SocketService
constructor
A new instance of SocketService.
- #pendingSessions ⇒ Object
- #serve(port, &action) ⇒ Object
- #serverTask(s) ⇒ Object
- #serviceTask ⇒ Object
- #waitForServers ⇒ Object
Constructor Details
#initialize ⇒ SocketService
Returns a new instance of SocketService.
9 10 11 12 13 |
# File 'lib/rubyslim/socket_service.rb', line 9 def initialize() @ropeSocket = nil @group = ThreadGroup.new @serviceThread = nil end |
Instance Attribute Details
#closed ⇒ Object (readonly)
Returns the value of attribute closed.
7 8 9 |
# File 'lib/rubyslim/socket_service.rb', line 7 def closed @closed end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rubyslim/socket_service.rb', line 40 def close @serviceThread.kill @serviceThread = nil @ropeSocket.close waitForServers @closed = true end |
#pendingSessions ⇒ Object
23 24 25 |
# File 'lib/rubyslim/socket_service.rb', line 23 def pendingSessions @group.list.size - ((@serviceThread != nil) ? 1 : 0) end |
#serve(port, &action) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rubyslim/socket_service.rb', line 15 def serve(port, &action) @closed = false @action = action @ropeSocket = TCPServer.open(port) @serviceThread = Thread.start {serviceTask} @group.add(@serviceThread) end |
#serverTask(s) ⇒ Object
35 36 37 38 |
# File 'lib/rubyslim/socket_service.rb', line 35 def serverTask(s) @action.call(s) s.close end |
#serviceTask ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/rubyslim/socket_service.rb', line 27 def serviceTask while true Thread.start(@ropeSocket.accept) do |s| serverTask(s) end end end |
#waitForServers ⇒ Object
48 49 50 51 52 |
# File 'lib/rubyslim/socket_service.rb', line 48 def waitForServers @group.list.each do |t| t.join end end |