Class: Client
- Inherits:
-
Object
- Object
- Client
- Defined in:
- client/client.rb
Overview
This class is used connect and run a client that connects using a TCP Socket.
Direct Known Subclasses
Instance Method Summary collapse
-
#close ⇒ NilClass
Close allows for the socket to be closed.
-
#connect ⇒ NilClass
Connects the socket to the early initialized port and ip.
-
#exit_threads ⇒ NilClass
Exits the threads.
-
#initialize(ip, port, username = 'ruby', reciever = 'all') ⇒ NilClass
constructor
The initialize method assigns all variables, both username and reciever and not required and default to ruby and all respectively.
-
#make_threads ⇒ ThreadGroup
Makes an array of threads to run.
-
#recieve_thread ⇒ Thread
Creates the recieve thread, this listens to the server for traffic.
-
#run ⇒ NilClass
Runs the client of the server.
-
#send_thread ⇒ Thread
Creates the sender thread, this waits for a user input.
Constructor Details
#initialize(ip, port, username = 'ruby', reciever = 'all') ⇒ NilClass
The initialize method assigns all variables, both username and reciever and not required and default to ruby and all respectively. During this the Socket is also initialized.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'client/client.rb', line 25 def initialize(ip, port, username = 'ruby', reciever = 'all') @socket = Socket.new Socket::AF_INET, Socket::SOCK_STREAM @ip = ip @port = port @username = username @reciever = reciever @serializer = JadeSerializer.new @deserializer = JadeDeserializer.new @threads = Array.new end |
Instance Method Details
#close ⇒ NilClass
Close allows for the socket to be closed.
71 72 73 74 75 76 77 |
# File 'client/client.rb', line 71 def close exit_threads @socket.close_read @socket.close_write @socket.close nil end |
#connect ⇒ NilClass
Connects the socket to the early initialized port and ip.
46 47 48 49 |
# File 'client/client.rb', line 46 def connect @socket.connect Socket.pack_sockaddr_in(@port, @ip) nil end |
#exit_threads ⇒ NilClass
Exits the threads
133 134 135 |
# File 'client/client.rb', line 133 def exit_threads @threads.each(&:exit) end |
#make_threads ⇒ ThreadGroup
Makes an array of threads to run.
84 85 86 87 |
# File 'client/client.rb', line 84 def make_threads @threads << recieve_thread @threads << send_thread end |
#recieve_thread ⇒ Thread
Creates the recieve thread, this listens to the server for traffic. If there is a message sent to the server it is converted to a JadeObject. This is done via the JadeDeserializer. The JadeObject is then converted to a packet and its data is put to the console.
97 98 99 100 101 102 103 104 105 106 |
# File 'client/client.rb', line 97 def recieve_thread Thread.new { until @socket.closed? output = @socket.gets jade_object = @deserializer.from_str(output) packet = jade_object.to_packet puts packet.to_str if packet.pertains_to_user?(@username) end } end |
#run ⇒ NilClass
Runs the client of the server. Starts by sending the clients username to the server, this is required by the server.
57 58 59 60 61 62 63 64 |
# File 'client/client.rb', line 57 def run connect @socket.puts @username make_threads @threads.each(&:join) nil end |
#send_thread ⇒ Thread
Creates the sender thread, this waits for a user input. When there is a user input it is converted to a Packet. The Packet is then converted to a JadeObject. The JadeObject is then serialized to a string and sent to the server.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'client/client.rb', line 116 def send_thread Thread.new { until @socket.closed? input = gets packet = Packet.new(@username, @reciever, 'data', input) jade_object = packet.to_jade_object jade_string = @serializer.to_str(jade_object) @socket.puts jade_string end } end |