Class: SocketService

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyslim/socket_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocketService

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

#closedObject (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

#closeObject



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

#pendingSessionsObject



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

#serviceTaskObject



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

#waitForServersObject



48
49
50
51
52
# File 'lib/rubyslim/socket_service.rb', line 48

def waitForServers
  @group.list.each do |t|
    t.join
  end
end