| Class | OpenWFE::SocketParticipant |
| In: |
lib/openwfe/participants/socketparticipants.rb
|
| Parent: | Object |
This participant implementation dispatches workitems over TCP sockets. By default the workitem are dumped as YAML strings, but you can override the encode_workitem(wi) method.
A small example :
require 'openwfe/particpants/socketparticipants'
sp = OpenWFE::SocketParticipant.new("target.host.xx", 7007)
engine.register_participant("Alfred", sp)
| host | [RW] | |
| port | [RW] |
A ‘static’ method for dispatching workitems, you can use it directly, without instantiating the SocketParticipant :
require 'openwfe/participants/socketparticipants'
SocketParticipant.dispatch("127.0.0.1", 7007, workitem)
# File lib/openwfe/participants/socketparticipants.rb, line 109
109: def SocketParticipant.dispatch (host, port, workitem)
110:
111: SocketParticipant.new(host, port).dispatch(workitem)
112: end
The constructor
# File lib/openwfe/participants/socketparticipants.rb, line 70
70: def initialize (host, port)
71:
72: @host = host
73: @port = port
74: end
# File lib/openwfe/participants/socketparticipants.rb, line 85
85: def dispatch (workitem)
86:
87: socket = TCPSocket.new(@host, @port)
88: socket.puts encode_workitem(workitem)
89: socket.puts
90: socket.close_write
91:
92: #print "\n__socket.closed? #{socket.closed?}"
93:
94: reply = fetch_reply(socket)
95:
96: socket.close
97:
98: decode_reply(reply)
99: end
By default, will just return the reply without touching it
# File lib/openwfe/participants/socketparticipants.rb, line 127
127: def decode_reply (r)
128: r
129: end
By default, uses YAML to serialize the workitem (of course you can override this method).
# File lib/openwfe/participants/socketparticipants.rb, line 120
120: def encode_workitem (wi)
121: YAML.dump(wi)
122: end
The code that waits for the reply from the server, nicely wrapped inside a timeout and a rescue block.
# File lib/openwfe/participants/socketparticipants.rb, line 135
135: def fetch_reply (socket)
136:
137: reply = ""
138:
139: begin
140:
141: timeout(7) do
142: while true
143: r = socket.gets
144: break unless r
145: reply << r
146: end
147: end
148:
149: rescue Exception => e
150: puts e
151: raise "timeout while waiting for reply"
152: end
153:
154: reply
155: end