zas-client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/zas/client.rb +31 -17
- metadata +1 -1
data/lib/zas/client.rb
CHANGED
@@ -18,22 +18,15 @@ module Zas
|
|
18
18
|
# Public: Initialize the client with the given configuration.
|
19
19
|
#
|
20
20
|
# config - Configuration spec for the client.
|
21
|
+
#
|
22
|
+
# Important note: when you construct the Zas::Client it is your responsibility to shut
|
23
|
+
# down cleanly. To do this call client.shutdown when your consumer exits.
|
21
24
|
def initialize(config=ClientConfiguration.new)
|
22
25
|
self.host = config.host
|
23
26
|
self.port = config.port
|
24
27
|
self.timeout = config.timeout
|
25
28
|
self.logger = config.logger
|
26
29
|
self.context = ZMQ::Context.new
|
27
|
-
|
28
|
-
Signal.trap(:INT) {
|
29
|
-
begin
|
30
|
-
puts "Trapped INT in Zas client"
|
31
|
-
disconnect
|
32
|
-
close
|
33
|
-
rescue => e
|
34
|
-
puts "Error while trying to shut down zas client: #{e.message}"
|
35
|
-
end
|
36
|
-
}
|
37
30
|
end
|
38
31
|
|
39
32
|
# Public: Authenticate the given credentials.
|
@@ -67,20 +60,40 @@ module Zas
|
|
67
60
|
|
68
61
|
# Public: Disconnect the socket.
|
69
62
|
def disconnect
|
70
|
-
if socket
|
71
|
-
|
72
|
-
socket.
|
63
|
+
if @socket
|
64
|
+
@socket.close
|
65
|
+
logger.debug "Closed socket #{@socket.inspect}"
|
73
66
|
end
|
74
67
|
@socket = nil
|
68
|
+
rescue Exception => e
|
69
|
+
puts "Exception in disconnect(): #{e.message}"
|
70
|
+
raise
|
75
71
|
end
|
76
72
|
|
77
73
|
# Public: Close the context.
|
78
74
|
def close
|
79
|
-
if context
|
80
|
-
|
81
|
-
context.close
|
82
|
-
context
|
75
|
+
if @context
|
76
|
+
puts "Closing context #{@context.inspect}"
|
77
|
+
@context.close
|
78
|
+
puts "Closed context #{@context.inspect}"
|
83
79
|
end
|
80
|
+
@context = nil
|
81
|
+
rescue Exception => e
|
82
|
+
puts "Exception is close(): #{e.message}"
|
83
|
+
raise
|
84
|
+
end
|
85
|
+
|
86
|
+
# Public: shut down the client (disconnect any open sockets
|
87
|
+
# and close the context.
|
88
|
+
def shutdown
|
89
|
+
puts "Disconnecting"
|
90
|
+
disconnect
|
91
|
+
puts "Closing"
|
92
|
+
close
|
93
|
+
puts "Done"
|
94
|
+
rescue Exception => e
|
95
|
+
puts "Exception in shutdown(): #{e.message}"
|
96
|
+
raise
|
84
97
|
end
|
85
98
|
|
86
99
|
private
|
@@ -102,6 +115,7 @@ module Zas
|
|
102
115
|
socket
|
103
116
|
else
|
104
117
|
logger.info "Cannot build a new socket as there is no context"
|
118
|
+
nil
|
105
119
|
end
|
106
120
|
end
|
107
121
|
end
|