Class: ProcessWanker::NetConnection

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/net/net_connection.rb

Direct Known Subclasses

NetClient, NetServerClient

Constant Summary

Constants included from Log

Log::DEBUG, Log::ERROR, Log::INFO, Log::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, info, log, set_level, warn

Constructor Details

#initialize(ssl_connection) ⇒ NetConnection

Returns a new instance of NetConnection.



34
35
36
37
38
39
# File 'lib/net/net_connection.rb', line 34

def initialize(ssl_connection)
	@write_mutex=Mutex.new
	@ssl_connection=ssl_connection
	@read_thread = Thread.new { read_proc }
	@user = ssl_connection.peer_cert.subject.to_a.select { |x| x[0]=="CN" }.map { |x| x[1] }[0]
end

Instance Attribute Details

#ssl_connectionObject

Returns the value of attribute ssl_connection.



25
26
27
# File 'lib/net/net_connection.rb', line 25

def ssl_connection
  @ssl_connection
end

#userObject

Returns the value of attribute user.



26
27
28
# File 'lib/net/net_connection.rb', line 26

def user
  @user
end

Instance Method Details

#close_rudelyObject



78
79
80
81
82
83
# File 'lib/net/net_connection.rb', line 78

def close_rudely()
	ProcessWanker::with_logged_rescue("close_rudely",Log::DEBUG) do
		@ssl_connection.io.close()
	end
	disconnect()
end

#disconnectObject



91
92
93
94
95
96
# File 'lib/net/net_connection.rb', line 91

def disconnect()
	on_close()
	if(Thread.current != @read_thread)
		@read_thread.join
	end
end

#on_closeObject



136
137
138
139
140
141
# File 'lib/net/net_connection.rb', line 136

def on_close()
	ProcessWanker::with_logged_rescue("on_close",Log::DEBUG) do
		@ssl_connection.close if(@ssl_connection)
	end
	@ssl_connection=nil
end

#on_msg(msg) ⇒ Object



149
150
151
# File 'lib/net/net_connection.rb', line 149

def on_msg(msg)
#			puts msg.inspect
end

#read_connectionObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/net/net_connection.rb', line 116

def read_connection()
	begin
		length=@ssl_connection.read(4)
		raise "closed" if(length.length != 4)
		length=length.unpack("N")[0]
		data=@ssl_connection.read(length)
		raise "closed" if(data.length != length)
		msg=Marshal.load(data)
		on_msg(msg)
	rescue Exception => e
		on_close()
	end
end

#read_procObject



104
105
106
107
108
# File 'lib/net/net_connection.rb', line 104

def read_proc
	while(@ssl_connection)
		read_connection()
	end
end

#send_msg(msg) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/net/net_connection.rb', line 57

def send_msg(msg)

	@write_mutex.synchronize do			
		debug("sending message #{msg.inspect}")
		begin
			data=Marshal.dump(msg)
			length=[data.length].pack("N")
			@ssl_connection.write(length + data)
		rescue Exception => e
			on_close()
		end
	end
	
end

#waitObject



47
48
49
# File 'lib/net/net_connection.rb', line 47

def wait
	@read_thread.join
end