Class: SKKHub::SKKServer

Inherits:
Object
  • Object
show all
Defined in:
lib/skkhub.rb

Instance Method Summary collapse

Instance Method Details

#accept_clientsObject



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

def accept_clients
  server = TCPServer.open(11178) # or 1178
  loop do
    s = server.accept
    Thread.start(s) do |s2|
      begin
        yield(s2)
      ensure
        s2.shutdown
        s2.close
      end
    end
  end
end

#mainloopObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/skkhub.rb', line 9

def mainloop
  accept_clients do |s|
    while cmdbuf = s.sysread(512)
      t = case cmdbuf[0, 1]
          when '1'
            q = cmdbuf.split[0]
            q.slice!(0)
            q.force_encoding('EUC-JP')
            a = yield(q.encode('UTF-8')).map{|i|i.encode('EUC-JP')}
            a.empty? ? "4\n" : "1/#{a.join('/')}/\n"
          when '2'
            'skkservtest-0.0.1 '
          when '3'
            'host:ip: '
          end
      s.write(t)
    end
  end
end