Class: Control::TelnetServer

Inherits:
Deferred
  • Object
show all
Defined in:
lib/automate-em/interfaces/OLD CODE/telnet/telnet.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Deferred

#connection_completed, #post_init, #receive_data, #unbind

Constructor Details

#initialize(*args) ⇒ TelnetServer

Returns a new instance of TelnetServer.



11
12
13
14
15
# File 'lib/automate-em/interfaces/OLD CODE/telnet/telnet.rb', line 11

def initialize(*args)
	super
	
	@input_lock = Mutex.new
end

Class Method Details

.startObject



17
18
19
20
# File 'lib/automate-em/interfaces/OLD CODE/telnet/telnet.rb', line 17

def self.start
	EventMachine::start_server "127.0.0.1", 23, TelnetServer
	System.logger.info 'running telnet server on 23'
end

Instance Method Details

#notify(mod_sym, stat_sym, data) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/automate-em/interfaces/OLD CODE/telnet/telnet.rb', line 83

def notify(mod_sym, stat_sym, data)
	send_line("\r\nStatus: #{mod_sym}:#{stat_sym}==#{data}", :green)
	send_prompt("> ", :green)
	@input_lock.synchronize {
		send(@input) if !@input.empty?
	}
end

#receivedObject



22
23
24
25
26
27
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/automate-em/interfaces/OLD CODE/telnet/telnet.rb', line 22

def received
	@input_lock.synchronize {
		data = @receive_queue.pop(true)		
		if data == "\b"
			if @input.length > 0
				send_data " \b"
				@input.chop!
			else
				send_data " "
			end
		elsif data =~ /.*\r\n$/

			#
			# TODO:: For linux we need to chop! twice here
			#	Windows @input is already complete
			#

			if @input =~ /^(quit|exit)$/i 
				disconnect
				return
			end

			if @input != "" && !@input.nil?
				if @selected.nil?
					if @input =~ /^\d+$/
						@selected = Communicator.select(self, @input.to_i)
					else
						@selected = Communicator.select(self, @input)
					end
					send_line " system #{@input} selected...", :green
				else
					thecommand = @input.split(/\s|\./, 3)
					@input = ""
					on_fail = lambda {
						send_line(" invalid command", :green)
						send_prompt("> ", :green)
						send_prompt(@input)
					}
					if thecommand[0] =~ /register/i
						@selected.register(self, thecommand[1], thecommand[2], &on_fail)
					elsif thecommand[0] =~ /unregister/i
						@selected.unregister(self, thecommand[1], thecommand[2], &on_fail)
					elsif thecommand[2].nil?
						@selected.send_command(thecommand[0], thecommand[1], &on_fail)
					elsif ['{','['].include?(thecommand[2][0])
						@selected.send_command(thecommand[0], thecommand[1], JSON.parse(thecommand[2], {:symbolize_names => true}), &on_fail)
					else
						@selected.send_command(thecommand[0], thecommand[1], thecommand[2], &on_fail)
					end
					send_line " sent...", :green
				end
			end

			send_prompt("> ", :green)
			@input = ""
		else #if data =~ /^[a-zA-Z0-9\., _-]*$/
			@input << data
		end
	}
end