Class: CodinRep::MockTimeClock

Inherits:
Object
  • Object
show all
Includes:
CodinRep
Defined in:
lib/codin_rep/mock_time_clock.rb

Constant Summary

Constants included from CodinRep

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CodinRep

included

Constructor Details

#initializeMockTimeClock



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/codin_rep/mock_time_clock.rb', line 31

def initialize
  @data = OpenStruct.new
  @ip = '127.0.0.1'
  @server = TCPServer.new(@ip, 0)
  @running = false
  @served_connections = 0
  @threads = []
  @threadsMutex = Mutex.new
  @served_connections = 0
  @time = true
  @is_sending_records = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



29
30
31
# File 'lib/codin_rep/mock_time_clock.rb', line 29

def data
  @data
end

#ipObject (readonly)

Returns the value of attribute ip.



29
30
31
# File 'lib/codin_rep/mock_time_clock.rb', line 29

def ip
  @ip
end

Instance Method Details

#running?Boolean



99
100
101
# File 'lib/codin_rep/mock_time_clock.rb', line 99

def running?
  @running
end

#serve(socket) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/codin_rep/mock_time_clock.rb', line 48

def serve(socket)
  Thread.new do
    Thread.current.abort_on_exception = true
    @threadsMutex.synchronize {
      @threads << Thread.current
      @served_connections += 1
    }
    begin
      raw_command = []
      @keep_connected = true
      @first_connection = true
      # Read command type
      while @keep_connected && raw_command = read_from_socket_with_timeout(socket, 9)
        response = process_command(raw_command, socket)
        socket.write response
        @first_connection = false
      end
    ensure
      socket.close unless socket.closed?
      @threadsMutex.synchronize {
        @threads.delete(Thread.current)
      }
    end
  end
end

#startObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/codin_rep/mock_time_clock.rb', line 74

def start
  @server_thread = Thread.new do
    Thread.current.abort_on_exception = true
    @running = true
    while @running
      break if @server.closed?
      socket = @server.accept
      serve(socket)
    end
  end
  self
end

#stopObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/codin_rep/mock_time_clock.rb', line 87

def stop
  @threadsMutex.synchronize {
    @threads.each{|thread| thread.kill}
    @threads = []
  }
  @server_thread.kill if @server_thread
  @server.close unless @server.closed?
  @server_thread.join
  @running = false
  true
end

#tcp_portObject



44
45
46
# File 'lib/codin_rep/mock_time_clock.rb', line 44

def tcp_port
  @server.addr[1]
end