-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetsocket.rb
62 lines (58 loc) · 1.54 KB
/
netsocket.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
#encoding: utf-8
require "socket"
if Gem::Platform.local.os == "darwin"
CurrentPath = "/Users/zxy/current"
command_path = "/Users/zxy/command"
else
CurrentPath = "/root/current"
command_path = "/root"
end
def net_status
File.open CurrentPath do |f|
return f.read
end
end
port = 12321
server = TCPServer.new port
loop do
Thread.start(server.accept) do |client|
loop do
client_ip = client.peeraddr[3]
if client_ip =~ /192.168.\d{1,2}.100/
num = client_ip.split(".")[2].to_i
else
num = 0
end
command = client.gets.chomp.strip
cstatus = net_status.chomp.strip
if command == "net"
if cstatus.include?("net")
fpath = command_path+"/allnet"
else
fpath = command_path+"/net"+num.to_s
end
%x(#{fpath}) if File.exist?(fpath)
client.puts $?==0 ? "net_ok" : "net_error"
client.flush
elsif command == "school"
neibor_num = num%2==0 ? num-1 : num+1
if cstatus.include?("net") and cstatus!="net"+num.to_s
fpath = command_path+"/net"+neibor_num.to_s
else
fpath = command_path+"/school"
end
%x(#{fpath}) if File.exist?(fpath)
client.puts $?==0 ? "school_ok" : "school_error"
client.flush
elsif command == "current"
client.puts net_status.chomp.strip
client.flush
puts "current status is "+net_status
elsif command == "close"
puts "#{client_ip} closed"
client.close
break
end
end
end
end