class ServerWrapper

The wrapper for TCPServer.

Public Class Methods

new(port=2000) { |self,server| ... } click to toggle source
# File lib/accu-net.rb, line 34
def initialize(port=2000)
        @port = TCPServer.open port
        catch :close do
                loop do
                        @server = @port.accept
                        @password,@mode = "password",:double
                        yield self,@server if block_given?
                        @server.close
                end
        end
        @port.close
end

Public Instance Methods

close() click to toggle source

Throws :close to end the serving process.

# File lib/accu-net.rb, line 121
def close
        throw :close
end
listen() { |msg| ... } click to toggle source

Listens for a generic network communication and yields to a passed block (if any).

# File lib/accu-net.rb, line 49
def listen()
        msg = @server.gets.chomp
        yield msg if block_given?
        return msg
end
listen_code() click to toggle source

Listens for a network code.

# File lib/accu-net.rb, line 66
def listen_code()
        listen { |msg|
                if msg.slice(0..1) == "C:" then
                        return msg.slice(2..msg.length).to_i
                else
                        return msg.slice(0..1)
                end
        }
end
listen_encrypted() click to toggle source

Listen for an encrypted connection.

# File lib/accu-net.rb, line 77
def listen_encrypted()
        if listen_code == 3 then
                password,source = EncryptDecrypt.decrypt(@password,((listen_lines).join),@mode)
                return source
        else
                raise "Sent code was not 3!"
        end
end
listen_lines() click to toggle source

Listens for multiple lines, appends them together and stops at END.

# File lib/accu-net.rb, line 57
def listen_lines()
        lines = []
        while line = @server.gets.chomp and line != "END" do
                lines << line
        end
        lines
end
send(text) click to toggle source

Sends a plaintext message.

# File lib/accu-net.rb, line 87
def send(text)
        @server.puts text
end
send_code(code) click to toggle source

Sends a code.

# File lib/accu-net.rb, line 110
def send_code(code)
        send("C:#{code}")
end
send_encrypted(lines) click to toggle source

Send over an encrypted multiline message.

# File lib/accu-net.rb, line 102
def send_encrypted(lines)
        secret,text = EncryptDecrypt.encrypt(@password,lines,@mode)
        send_code(3)
        send(text)
        send("END")
end
send_lines(lines) click to toggle source

Sends multiple lines and appends END to end the send.

# File lib/accu-net.rb, line 93
def send_lines(lines)
        array = lines.split("\n")
        array.each do |line|
                send(line)
        end
        send("END")
end
set_password(string) click to toggle source

Set encryption password.

# File lib/accu-net.rb, line 115
def set_password(string)
        warn "Argument 1 is not a string!  Continuing anyway." if not string.is_a? String
        @password = string.to_s
end