Class: ClientWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/accu-net.rb

Overview

The wrapper for TCPClient.

Instance Method Summary collapse

Constructor Details

#initialize(port = 2000, ip = "127.0.0.1") {|_self, @client| ... } ⇒ ClientWrapper

Returns a new instance of ClientWrapper.

Yields:

  • (_self, @client)

Yield Parameters:

  • _self (ClientWrapper)

    the object that the method was called on



122
123
124
125
126
127
# File 'lib/accu-net.rb', line 122

def initialize(port=2000,ip="127.0.0.1")
  @client = TCPSocket.open(ip, port)
  @password,@mode = "password",:double
  yield self,@client if block_given?
  @client.close
end

Instance Method Details

#listen {|msg| ... } ⇒ Object

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

Yields:

  • (msg)


131
132
133
134
# File 'lib/accu-net.rb', line 131

def listen()
  msg = @client.gets.chomp
  yield msg
end

#listen_codeObject

Listens for a network code.



147
148
149
150
151
152
153
154
155
# File 'lib/accu-net.rb', line 147

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_linesObject

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



138
139
140
141
142
143
144
# File 'lib/accu-net.rb', line 138

def listen_lines()
  lines = []
  while line = @client.gets.chomp and line != "END" do
    lines << line
  end
  lines
end

#send(text) ⇒ Object

Sends a plaintext message.



158
159
160
# File 'lib/accu-net.rb', line 158

def send(text)
  @client.puts text
end

#send_code(code) ⇒ Object

Sends a code.



181
182
183
# File 'lib/accu-net.rb', line 181

def send_code(code)
  send("C:#{code}")
end

#send_encrypted(lines) ⇒ Object

Send over an encrypted multiline message.



173
174
175
176
177
178
# File 'lib/accu-net.rb', line 173

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.



164
165
166
167
168
169
170
# File 'lib/accu-net.rb', line 164

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.



186
187
188
189
# File 'lib/accu-net.rb', line 186

def set_password(string)
  warn "Argument 1 is not a string!  Continuing anyway." if not string.is_a? String
  @password = string.to_s
end