Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
206 ligi 1
# mailto: ligi atttt ligi dotttt de
2
require 'socket'
3
require 'fcntl'
4
 
5
class IPSocket
6
  def portInfo(out = STDOUT)
7
    out.puts "local:  #{addr[1]}\n" +
8
      "remote: #{peeraddr[1]}"
9
  end
10
end
11
 
12
 
13
 
14
if ARGV.length!=1 || !ARGV[0].include?(":")
15
  puts "USAGE:"
16
  puts "$> ruby messenger.rb [ip:port]"
17
  puts ""
18
  puts "using default address"
19
  host="0"
20
  port=9876
21
else
22
  host=ARGV[0].split(":").first
23
  port=ARGV[0].split(":").last.to_i  
24
end
25
 
26
 
27
 
28
puts "starting server at ip " + host + " port:" + port.to_s
29
serverSocket = TCPServer.new(host,port)
30
 
31
slots={ }
32
server = Thread.start {
33
  while (clientSocket = serverSocket.accept)
34
    Thread.start {
35
      clientSocket_= clientSocket
36
      clientSocket_.portInfo
37
 
38
 
39
      while true
40
 
41
      begin
42
 
43
        command=clientSocket_.gets
44
        puts command
45
        case
46
        when command =~ /new/
47
          clientSocket_.puts "new"
48
          if slots[command.delete("\r\n").split(":").last].nil?
49
            slots[command.delete("\r\n").split(":").last]={:from=>clientSocket_ , :to=>nil}
50
            break
51
          else
52
             clientSocket_.puts "slot exists"
53
          end
54
        when command =~ /conn/
55
 
56
          case
57
          when slots[command.delete("\r\n").split(":").last].nil?
58
            clientSocket_.puts "slot not found"
59
          when !slots[command.delete("\r\n").split(":").last][:to].nil?
60
            clientSocket_.puts "slot has partner"
61
          else
62
            clientSocket_.puts "conn"
63
            slots[command.delete("\r\n").split(":").last][:to]=clientSocket_
64
 
65
            break
66
          end
67
 
68
        when command =~ /all/
69
#          clientSocket_.puts slots.keys
70
          if slots=={}
71
              clientSocket_.puts "none"
72
          else
73
          slots.keys.each_with_index {|slot,i|
74
              clientSocket_.puts i.to_s + " | " + slot + ((slots[slot][:to].nil?)?"| free":"| connected")
75
            }
76
          end
77
 
78
 
79
        else
80
          clientSocket_.puts "Unknown command"
81
 
82
      end
83
 
84
        rescue =>e
85
        puts "err" + e
86
      end
87
      end
88
    }
89
end
90
}
91
 
92
while true
93
slots.each { |slot_name,hash|
94
 
95
#puts " processing " + slot_name
96
 
97
 
98
if !hash[:to].nil?
99
begin
100
 
101
got=hash[:to].read_nonblock(3000)
102
puts slot_name +"<"+ got
103
hash[:from].write got
104
hash[:to].flush
105
 
106
rescue  Errno::EAGAIN => e
107
rescue =>e
108
 slots[slot_name][:to]=nil
109
  p e
110
 
111
end
112
end
113
 
114
begin
115
 
116
got=hash[:from].read_nonblock(3000)
117
puts slot_name +">"+ got
118
hash[:to].write got if !hash[:to].nil?
119
hash[:to].flush  if !hash[:to].nil?
120
 
121
rescue  Errno::EAGAIN => e
122
rescue =>e
123
hash[:to].close if !hash[:to].nil?
124
 slots.delete(slot_name)
125
  p e
126
 
127
end
128
 
129
#
130
}
131
sleep 0.00005
132
 
133
 
134
 
135
end