Class: Simbiotes::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(server = TCPSocket.open( Simbiotes.configuration.server, Simbiotes.configuration.server_port )) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
# File 'lib/simbiotes/server.rb', line 8

def initialize( server = TCPSocket.open( Simbiotes.configuration.server, Simbiotes.configuration.server_port ) )
  @server = server
  @localport = Simbiotes.configuration.local_port
  listen_remote
  listen_local
end

Instance Method Details

#closeObject



20
21
22
# File 'lib/simbiotes/server.rb', line 20

def close
  @server.close
end

#listen_localObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simbiotes/server.rb', line 48

def listen_local
  begin
    server = TCPServer.open(@localport)
    loop do
      Thread.fork(server.accept) do |client| 
        s = client.gets
        #puts s
        self.send(s)
      end
    end
  rescue => e
    puts e
    listen_local
  end
end

#listen_remoteObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simbiotes/server.rb', line 24

def listen_remote
  begin
    Thread.new do
      loop do
        msg = @server.gets
        puts msg
        msg_hash = Simbiotes::Parse.message(msg)
        r = Simbiotes::Insert.new(msg_hash)
        puts "Message received": msg_hash
        if r.action == "set-request-ack" || r.action == "set-complete" || r.action == "get-ack"
          r.save
          unless Simbiotes.configuration.local_logging == false
            r.save_log
          end
        end
      end
    end
  rescue => e
    puts e
    listen_remote
  end
  
end

#send(msg) ⇒ Object



15
16
17
18
# File 'lib/simbiotes/server.rb', line 15

def send(msg)
  puts "Message sent: #{msg}"
  @server.puts( msg )
end