Class: ServerWrapper
- Inherits:
-
Object
- Object
- ServerWrapper
- Defined in:
- lib/accu-net.rb
Overview
The wrapper for TCPServer.
Instance Method Summary collapse
-
#close ⇒ Object
Throws :close to end the serving process.
-
#initialize(port = 2000) ⇒ ServerWrapper
constructor
A new instance of ServerWrapper.
-
#listen {|msg| ... } ⇒ Object
Listens for a generic network communication and yields to a passed block (if any).
-
#listen_code ⇒ Object
Listens for a network code.
-
#listen_encrypted ⇒ Object
Listen for an encrypted connection.
-
#listen_lines ⇒ Object
Listens for multiple lines, appends them together and stops at END.
-
#send(text) ⇒ Object
Sends a plaintext message.
-
#send_code(code) ⇒ Object
Sends a code.
-
#send_encrypted(lines) ⇒ Object
Send over an encrypted multiline message.
-
#send_lines(lines) ⇒ Object
Sends multiple lines and appends END to end the send.
-
#set_password(string) ⇒ Object
Set encryption password.
Constructor Details
#initialize(port = 2000) ⇒ ServerWrapper
Returns a new instance of ServerWrapper.
34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |
Instance Method Details
#close ⇒ Object
Throws :close to end the serving process.
121 122 123 |
# File 'lib/accu-net.rb', line 121 def close throw :close end |
#listen {|msg| ... } ⇒ Object
Listens for a generic network communication and yields to a passed block (if any).
49 50 51 52 53 |
# File 'lib/accu-net.rb', line 49 def listen() msg = @server.gets.chomp yield msg if block_given? return msg end |
#listen_code ⇒ Object
Listens for a network code.
66 67 68 69 70 71 72 73 74 |
# 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 ⇒ Object
Listen for an encrypted connection.
77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
Listens for multiple lines, appends them together and stops at END.
57 58 59 60 61 62 63 |
# 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) ⇒ Object
Sends a plaintext message.
87 88 89 |
# File 'lib/accu-net.rb', line 87 def send(text) @server.puts text end |
#send_code(code) ⇒ Object
Sends a code.
110 111 112 |
# File 'lib/accu-net.rb', line 110 def send_code(code) send("C:#{code}") end |
#send_encrypted(lines) ⇒ Object
Send over an encrypted multiline message.
102 103 104 105 106 107 |
# 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) ⇒ Object
Sends multiple lines and appends END to end the send.
93 94 95 96 97 98 99 |
# 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) ⇒ Object
Set encryption password.
115 116 117 118 |
# 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 |