Class: Butler::Remote::Connection

Inherits:
Object
  • Object
show all
Includes:
Log::Comfort
Defined in:
lib/butler/remote/connection.rb

Instance Attribute Summary collapse

Attributes included from Log::Comfort

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log::Comfort

#debug, #error, #exception, #fail, #info, #log, #warn

Constructor Details

#initialize(server, socket) ⇒ Connection

Returns a new instance of Connection.



35
36
37
38
39
40
# File 'lib/butler/remote/connection.rb', line 35

def initialize(server, socket)
	@server = server
	@socket = socket
	@user   = nil
	@logger = nil
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



33
34
35
# File 'lib/butler/remote/connection.rb', line 33

def user
  @user
end

Class Method Details

.open(server, socket) ⇒ Object



25
26
27
28
29
# File 'lib/butler/remote/connection.rb', line 25

def self.open(server, socket)
	connection = new(server, socket)
	connection.start
	connection
end

.open_thread(server, socket) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/butler/remote/connection.rb', line 16

def self.open_thread(server, socket)
	connection = new(server, socket)
	Thread.new {
		connection.start
		connection.read_loop
	}
	connection
end

Instance Method Details

#closeObject



108
109
110
# File 'lib/butler/remote/connection.rb', line 108

def close
	@socket.close unless @socket.closed?
end

#closed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/butler/remote/connection.rb', line 104

def closed?
	@socket.closed?
end

#puts(*text) ⇒ Object



94
95
96
# File 'lib/butler/remote/connection.rb', line 94

def puts(*text)
	@socket.puts(*text)
end

#quit(reason = nil) ⇒ Object



98
99
100
101
102
# File 'lib/butler/remote/connection.rb', line 98

def quit(reason=nil)
	puts reason if reason
	close
	@server.disconnected(self)
end

#read_loopObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/butler/remote/connection.rb', line 71

def read_loop
	while line = @socket.gets
		line.chomp!
		p line
		break if line.empty?
		begin
			@server.dispatch(Message.new(@server, self, @user, line))

		# on these errors we got to get out of the loop
		rescue Errno::EPIPE, SocketError
			raise
		# all others should not put the read-thread in any danger
		rescue Exception => e
			exception(e)
		end
	end
rescue Exception => e
	exception(e)
ensure
	@server.disconnected(self)
	close
end

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/butler/remote/connection.rb', line 42

def start
	puts("Please identify yourself")
	tries    = 3
	access   = nil
	username = nil
	timeout(180) {
		tries.times { |i|
			@socket.print("Username: ")
			username = @socket.gets.chomp
			@socket.print("Password: ")
			password = @socket.gets.chomp
			access   = @server.(username, password)
			break if access
			sleep(i)
			puts("Failed to authenticate, please try again.")
		}
	}
	return quit("Failed to authenticate") unless access
	puts "You're logged in, welcome #{username}."
	puts "To terminate the session, enter an empty line."
	puts ""
	@user = User.new(username, access)
rescue Timeout::Error
	quit("Login timed out.")
rescue Exception => e
	exception(e)
	quit(nil)
end