Class: LocalMessageClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_hostname, server_port, user_name) ⇒ LocalMessageClient

Returns a new instance of LocalMessageClient.



7
8
9
10
11
12
# File 'lib/local_message/local_message_client.rb', line 7

def initialize(server_hostname, server_port, user_name)
  @server = server_hostname
  @port = server_port
  @username = user_name
  @listen_port = Random.rand(2000..30_000)
end

Instance Attribute Details

#listen_portObject (readonly)

Returns the value of attribute listen_port.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def listen_port
  @listen_port
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def server
  @server
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'lib/local_message/local_message_client.rb', line 5

def username
  @username
end

Instance Method Details

#listenObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/local_message/local_message_client.rb', line 31

def listen
  #puts "Listening on #{listen_port}..."
  server = TCPServer.open(listen_port)
  client = server.accept
  #puts "Connection open, waiting for message..."
  message = client.read
  client.close
  server.close
  message
end

#new_listen_portObject



27
28
29
# File 'lib/local_message/local_message_client.rb', line 27

def new_listen_port
  @listen_port = Random.rand(1000..30_000)
end

#registerObject



14
15
16
# File 'lib/local_message/local_message_client.rb', line 14

def register
  send("@register #{username} #{listen_port}")
end

#send(message) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/local_message/local_message_client.rb', line 18

def send(message)
  socket = TCPSocket.open(server, port)
  sleep 1
  socket.write(message)
  socket.close
  #puts "Waiting for server acknowledgement..."
  #listen
end