Class: FiverrCopy::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
# File 'lib/fiverr_copy/client.rb', line 5

def initialize(options = {})
  #options.assert_valid_keys :client_ip, :next_client, :username, :port
  @next_client = options[:next_client] ||= nil
  @username = options[:username] ||= "admin"
  @client_ip = options[:client_ip]# || raise("No client specified")
  @port = options[:port] ||= 1234
  @ready = false
  self
end

Instance Attribute Details

#client_ipObject (readonly)

Returns the value of attribute client_ip.



3
4
5
# File 'lib/fiverr_copy/client.rb', line 3

def client_ip
  @client_ip
end

#next_clientObject (readonly)

Returns the value of attribute next_client.



3
4
5
# File 'lib/fiverr_copy/client.rb', line 3

def next_client
  @next_client
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/fiverr_copy/client.rb', line 3

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/fiverr_copy/client.rb', line 3

def username
  @username
end

Class Method Details

.run!(port, next_client, filename, chunk) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fiverr_copy/client.rb', line 28

def self.run!(port, next_client, filename, chunk)
  puts "FiverrCopy client initializing"
  server = TCPServer.new(port)

  session = server.accept

  client_session = nil
  if !(next_client.nil?)
    client_session = TCPSocket.new(next_client, port)
  end

  File.open(filename, "w+") do |f|
#      Thread.start do
      while (stream = session.read(chunk.to_i)) do    
        
        f.write stream

        if !(next_client.nil?)
          client_session.write(stream)
        end
        session.puts "OK:#{stream.size}"
      end
  end
  
end

Instance Method Details

#forward?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/fiverr_copy/client.rb', line 15

def forward?
  !(@next_client.nil?)
end

#ready?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fiverr_copy/client.rb', line 19

def ready?
  @ready
end

#setup!Object



23
24
25
# File 'lib/fiverr_copy/client.rb', line 23

def setup!
  @ready = true
end