Class: Client

Inherits:
Object
  • Object
show all
Defined in:
client/client.rb

Overview

This class is used connect and run a client that connects using a TCP Socket.

Author:

  • Joseph Beck

Direct Known Subclasses

Game

Instance Method Summary collapse

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.

Parameters:

  • (String, Integer, String, String)


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

#closeNilClass

Close allows for the socket to be closed.

Parameters:

  • (NilClass)

Returns:

  • (NilClass)


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

#connectNilClass

Connects the socket to the early initialized port and ip.

Parameters:

  • (NilClass)

Returns:

  • (NilClass)


46
47
48
49
# File 'client/client.rb', line 46

def connect
  @socket.connect Socket.pack_sockaddr_in(@port, @ip)
  nil
end

#exit_threadsNilClass

Exits the threads

Parameters:

  • (NilClass)

Returns:

  • (NilClass)


133
134
135
# File 'client/client.rb', line 133

def exit_threads
  @threads.each(&:exit)
end

#make_threadsThreadGroup

Makes an array of threads to run.

Parameters:

  • (NilClass)

Returns:

  • (ThreadGroup)


84
85
86
87
# File 'client/client.rb', line 84

def make_threads
  @threads << recieve_thread
  @threads << send_thread
end

#recieve_threadThread

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.

Parameters:

  • (NilClass)

Returns:

  • (Thread)


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

#runNilClass

Runs the client of the server. Starts by sending the clients username to the server, this is required by the server.

Parameters:

  • (NilClass)

Returns:

  • (NilClass)


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_threadThread

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.

Parameters:

  • (NilClass)

Returns:

  • (Thread)


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