-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rb
201 lines (172 loc) · 4.55 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require './command_handler'
require './data_structures'
class Client
SUCCESS = "200 OK"
BAD_REQUEST = "400 BAD REQUEST"
MAX_CHANNELS = 32
@@count = 0
@@instances = {
controller: [], display: [], eeg: [], unset: []
}
@@instances_by_index = {}
attr_reader :index, :config, :type
def is_displaying?
@is_displaying
end
def initialize(socket)
update_last_alive
set_role "UNSET"
@socket = socket
@index = @@count += 1
@@instances_by_index[@index] = self
@config = EDFConfig.new
@handler = CommandHandler.new self
puts "Accepting connection #{@index} at #{@last_alive}"
loop {
@handler.parse socket.readline.strip
break if @finished
}
socket.close
end
def hello
execute { SUCCESS }
end
def role
execute { [ SUCCESS, @type ] }
end
def control
execute {
next controller_already_set unless @@instances[:controller].empty?
set_role "CONTROLLER"
}
end
def eeg
execute { set_role "EEG" }
end
def display
execute {
res = set_role "DISPLAY"
@is_displaying = true if res == SUCCESS
res
}
end
def status
execute {
res = []
@@instances.each { |type,clients|
indexes = clients.collect { |c| c.index }
res << "#{type}: #{indexes.join(', ')}"
}
[ SUCCESS, res.join("\r\n") ]
}
end
def get_header(client_id)
execute {
next type_request_invalid_to_role "getheader", "DISPLAY" unless @type == "DISPLAY"
c = client_by_id client_id
next c.type_request_invalid_to_role "getheader", "EEG" unless c.type == "EEG"
[ SUCCESS, c.config.to_s ]
}
end
def set_header(headers)
execute {
next type_request_invalid_to_role "setheader", "EEG" unless @type == "EEG"
headers = Hash[*headers]
headers[:start_time] = Time.now
@config.header ||= EDFHeader.new
@config.header.update_attributes headers
SUCCESS
}
end
def set_channel_header(channel, headers)
execute {
next type_request_invalid_to_role "setcheader", "EEG" unless @type == "EEG"
@config.channel_headers[channel.to_i] ||= EDFChannelHeader.new
@config.channel_headers[channel.to_i].update_attributes Hash[*headers]
SUCCESS
}
end
def data_frame(packet_counter, channel_count, data)
execute {
next invalid_channel_count(channel_count) unless channel_count.to_i.between? 0, MAX_CHANNELS
@@instances[:display].each { |c|
c.send_message "! 0 #{data.join ' '}\r\n" if c.is_displaying?
}
SUCCESS
}
end
def watch(state)
execute {
next invalid_command "watch" unless @type == "DISPLAY"
@is_displaying = state
SUCCESS
}
end
def go(recv_index, msg="go")
execute {
next type_request_invalid_to_role "go", "CONTROLLER" unless @type == "CONTROLLER"
receiver = client_by_id recv_index
puts "Sending [#{msg}] from #{@index} to #{receiver.index}"
receiver.send_message msg
SUCCESS
}
end
def close
@@instances[@type.downcase.to_sym].delete self
@@instances_by_index.delete self.index
@finished = true
end
def send(status_line, response=nil)
send_message status_line
send_message response unless response.nil?
end
def send_message(msg)
@socket.print "#{msg}\r\n"
update_last_alive
end
def type_request_invalid_to_role(type, role)
[
BAD_REQUEST,
"Only #{role} accepts #{type} request. #{@index} role is #{@type}."
]
end
private
def update_last_alive
@last_alive = Time.now
end
def execute
begin
status, message = yield
send status, message
rescue Exception => e
send "ERROR", e.message
end
end
def invalid_command(cmd)
[ BAD_REQUEST, "#{cmd} command cannot be sent to client with role #{@type}" ]
end
def controller_already_set
index = @@instances[:controller].first.index
[ BAD_REQUEST, "controller already set to client ##{index}" ]
end
def set_role(role)
unset = (@type.nil? or @type == "UNSET")
return [ BAD_REQUEST, "role already set to #{@type}" ] unless unset
type = role.downcase.to_sym
@@instances[type] << self
@@instances[:unset].delete self unless type == :unset
@type = role
SUCCESS
end
def invalid_channel_count(channel_count)
[
BAD_REQUEST,
"Channel count [#{channel_count}] should be between 0 and #{MAX_CHANNELS}"
]
end
def client_by_id(id)
id = id.to_i
raise "Client #{id} does not exist" unless @@instances_by_index.include? id
@@instances_by_index[id]
end
end