Skip to content

Commit f6344cf

Browse files
committed
Add classes of commands
1 parent 0f9e5c5 commit f6344cf

File tree

3 files changed

+168
-35
lines changed

3 files changed

+168
-35
lines changed

Diff for: Client.rb

+18-13
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ def send_command(command, args='')
88
s = TCPSocket.open(hostname, port)
99
msg = merge_command(command, args)
1010
send_all(s, msg)
11-
puts recv_all(s)
11+
rs = recv_all(s)
12+
puts rs
1213
s.close
14+
15+
return rs
1316
end
1417

15-
send_command('random')
16-
send_command('random')
17-
send_command('color')
18-
send_command('color')
19-
send_command('color')
20-
send_command('timenow')
21-
send_command('uuid')
22-
send_command('md5', '0123456')
23-
send_command('sha256', '0123456')
24-
send_command('valute_USD')
25-
send_command('valute','USD')
26-
send_command('valute', 'EUR')
2718

19+
if __FILE__ == $0 then
20+
send_command('random')
21+
send_command('random')
22+
send_command('color')
23+
send_command('color')
24+
send_command('color')
25+
send_command('timenow')
26+
send_command('uuid')
27+
send_command('md5', '0123456')
28+
send_command('sha256', '0123456')
29+
send_command('valute_USD')
30+
send_command('valute','USD')
31+
send_command('valute', 'EUR')
32+
end

Diff for: common.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
def recv_all(s)
2-
raw_msg_len = s.read(8).unpack('Q').first # Unpack is 8 byte to number
3-
return s.read(raw_msg_len)
2+
begin
3+
raw_msg_len = s.read(8).unpack('Q').first # Unpack is 8 byte to number
4+
return s.read(raw_msg_len)
5+
rescue
6+
return ''
7+
end
48
end
59

610
def send_all(s, msg)
7-
msg = msg.to_s
8-
s.write [msg.length].pack('Q') + msg # Q is 8 byte (long long int)
11+
begin
12+
msg = msg.to_s
13+
s.write [msg.length].pack('Q') + msg # Q is 8 byte (long long int)
14+
rescue
15+
return
16+
end
917
end
1018

1119
def parse_command(msg)
@@ -15,5 +23,8 @@ def parse_command(msg)
1523
end
1624

1725
def merge_command(command, args)
26+
if args.nil?
27+
args = ''
28+
end
1829
return command.ljust(10) + args
1930
end

Diff for: server_commands.rb

+135-18
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,161 @@
33
require 'securerandom'
44
require 'digest'
55

6-
# class Server_commands
7-
# end
6+
class BaseCommand
7+
def run(args)
8+
raise NotImplementedError.new ('Not Implemented')
9+
end
10+
11+
def get_name
12+
raise NotImplementedError.new ('Not Implemented')
13+
end
14+
15+
def get_description
16+
raise NotImplementedError.new ('Not Implemented')
17+
end
18+
end
19+
20+
21+
22+
class RandomCommand < BaseCommand
23+
def run(args='')
24+
rand(0...1000000)
25+
end
26+
27+
def get_name
28+
'random'
29+
end
30+
31+
def get_description
32+
'Return Random Value'
33+
end
34+
end
835

9-
def get_currency_exchange(currency)
10-
url = "http://www.cbr.ru/scripts/XML_daily.asp"
36+
class TimeNowCommand < BaseCommand
37+
def run(args='')
38+
Time.now.ctime
39+
end
1140

12-
doc = Nokogiri::XML(URI.open(url))
41+
def get_name
42+
'timenow'
43+
end
1344

14-
doc.css('Valute').each do |node|
15-
ccy = node.css('CharCode').text
16-
value = node.css('Value').text
17-
return value.sub(',', '.').to_f.round(2).to_s if ccy == currency
45+
def get_description
46+
'Return Current Time'
47+
end
48+
end
49+
50+
class ColorCommand < BaseCommand
51+
def run(args='')
52+
"#%06X" % (rand * 0xffffff)
53+
end
54+
55+
def get_name
56+
'color'
57+
end
58+
59+
def get_description
60+
'Return random color in hex'
61+
end
62+
end
63+
64+
class UUidCommand < BaseCommand
65+
def run(args='')
66+
SecureRandom.uuid
67+
end
68+
69+
def get_name
70+
'uuid'
71+
end
72+
73+
def get_description
74+
'Return uuid'
75+
end
76+
end
77+
78+
class MD5Command < BaseCommand
79+
def run(args='')
80+
Digest::MD5.hexdigest(args)
81+
end
82+
83+
def get_name
84+
'md5'
85+
end
86+
87+
def get_description
88+
'Return md5'
89+
end
90+
end
91+
92+
class SHA254Command < BaseCommand
93+
def run(args='')
94+
Digest::SHA256.hexdigest(args)
95+
end
96+
97+
def get_name
98+
'sha256'
99+
end
100+
101+
def get_description
102+
'Return sha256'
103+
end
104+
end
105+
106+
class ValuteCommand < BaseCommand
107+
def run(args='')
108+
get_currency_exchange(args)
109+
end
110+
111+
def get_name
112+
'valute'
113+
end
114+
115+
def get_description
116+
'Return Valute'
117+
end
118+
119+
private def get_currency_exchange(currency)
120+
url = "http://www.cbr.ru/scripts/XML_daily.asp"
121+
122+
doc = Nokogiri::XML(URI.open(url))
123+
124+
doc.css('Valute').each do |node|
125+
ccy = node.css('CharCode').text
126+
value = node.css('Value').text
127+
return value.sub(',', '.').to_f.round(2).to_s if ccy == currency
128+
end
129+
return "Not found currency #{currency}"
18130
end
19-
return "Not found currency #{currency}"
20131
end
21132

22133

23134
def process_command(command, args)
24135
rs = ''
25136
case command
26137
when 'random' then
27-
rs = rand(0...1000000)
138+
rs = RandomCommand.new.run(0...1000000)
28139
when 'timenow' then
29-
rs = Time.now.ctime
140+
rs = TimeNowCommand.new.run
30141
when 'color' then
31-
rs = "#%06X" % (rand * 0xffffff)
142+
rs = ColorCommand.new.run
32143
when 'uuid' then
33-
rs = SecureRandom.uuid
144+
rs = UUidCommand.new.run
34145
when 'md5' then
35-
rs = Digest::MD5.hexdigest(args)
146+
rs = MD5Command.new.run(args)
36147
when 'sha256' then
37-
rs = Digest::SHA256.hexdigest(args)
148+
rs = SHA254Command.new.run(args)
38149
when 'valute' then
39-
rs = get_currency_exchange(args)
150+
rs = ValuteCommand.new.run(args)
40151
when 'valute_USD' then
41-
rs = get_currency_exchange('USD')
152+
rs = ValuteCommand.new.run('USD')
42153
else
43154
rs = "Unrecognized command '#{command}'"
44155
end
45156
return rs
46157
end
158+
159+
160+
if __FILE__ == $0 then
161+
command = RandomCommand.new
162+
puts command.run
163+
end

0 commit comments

Comments
 (0)