15
15
*/
16
16
17
17
using System ;
18
- using System . Collections ;
19
- using System . Collections . Generic ;
20
- using System . Collections . ObjectModel ;
21
18
using System . Transactions ;
22
19
using Amqp ;
23
20
using Amqp . Framing ;
24
21
using Amqp . Types ;
22
+ using System . Threading . Tasks ;
23
+ using Amqp . Listener ;
25
24
26
25
namespace ClientLib
27
26
{
@@ -38,40 +37,55 @@ public class ReceiverClient : CoreClient
38
37
/// <returns>build receiver link</returns>
39
38
private ReceiverLink PeprareReceiverLink ( ReceiverOptions options )
40
39
{
41
- Source recvSource = new Source ( ) ;
42
- recvSource . Address = options . Address ;
43
-
40
+ Source recvSource = new Source ( )
41
+ {
42
+ Address = options . Address
43
+ } ;
44
44
if ( options . RecvBrowse )
45
45
recvSource . DistributionMode = new Symbol ( "copy" ) ;
46
46
47
47
//source
48
48
if ( ! string . IsNullOrEmpty ( options . MsgSelector ) )
49
49
{
50
- Map filters = new Map ( ) ;
51
- filters . Add ( new Symbol ( "filter" ) ,
52
- new DescribedValue (
50
+ Map filters = new Map
51
+ {
52
+ {
53
+ new Symbol ( "filter" ) ,
54
+ new DescribedValue (
53
55
new Symbol ( "apache.org:selector-filter:float" ) ,
54
- options . MsgSelector ) ) ;
55
- filters . Add ( new Symbol ( "filter1" ) ,
56
- new DescribedValue (
56
+ options . MsgSelector )
57
+ } ,
58
+ {
59
+ new Symbol ( "filter1" ) ,
60
+ new DescribedValue (
57
61
new Symbol ( "apache.org:selector-filter:string" ) ,
58
- options . MsgSelector ) ) ;
59
- filters . Add ( new Symbol ( "filter2" ) ,
60
- new DescribedValue (
62
+ options . MsgSelector )
63
+ } ,
64
+ {
65
+ new Symbol ( "filter2" ) ,
66
+ new DescribedValue (
61
67
new Symbol ( "apache.org:selector-filter:int" ) ,
62
- options . MsgSelector ) ) ;
63
- filters . Add ( new Symbol ( "filter3" ) ,
64
- new DescribedValue (
68
+ options . MsgSelector )
69
+ } ,
70
+ {
71
+ new Symbol ( "filter3" ) ,
72
+ new DescribedValue (
65
73
new Symbol ( "apache.org:selector-filter:boolean" ) ,
66
- options . MsgSelector ) ) ;
67
- filters . Add ( new Symbol ( "filter4" ) ,
68
- new DescribedValue (
74
+ options . MsgSelector )
75
+ } ,
76
+ {
77
+ new Symbol ( "filter4" ) ,
78
+ new DescribedValue (
69
79
new Symbol ( "apache.org:selector-filter:list" ) ,
70
- options . MsgSelector ) ) ;
71
- filters . Add ( new Symbol ( "filter5" ) ,
72
- new DescribedValue (
80
+ options . MsgSelector )
81
+ } ,
82
+ {
83
+ new Symbol ( "filter5" ) ,
84
+ new DescribedValue (
73
85
new Symbol ( "apache.org:selector-filter:map" ) ,
74
- options . MsgSelector ) ) ;
86
+ options . MsgSelector )
87
+ }
88
+ } ;
75
89
recvSource . FilterSet = filters ;
76
90
}
77
91
Attach attach = new Attach ( )
@@ -86,6 +100,83 @@ private ReceiverLink PeprareReceiverLink(ReceiverOptions options)
86
100
}
87
101
#endregion
88
102
103
+ #region Listener methods
104
+ /// <summary>
105
+ /// Method for init container listener
106
+ /// </summary>
107
+ /// <param name="options">receiver options</param>
108
+ private void InitListener ( ReceiverOptions options )
109
+ {
110
+ this . CreateContainerHost ( options ) ;
111
+ this . containerHost . Open ( ) ;
112
+ this . containerHost . RegisterMessageProcessor ( options . Address , new MessageProcessor ( options , this . containerHost ) ) ;
113
+ System . Threading . Thread . Sleep ( options . Timeout ) ;
114
+ }
115
+
116
+ /// <summary>
117
+ /// Private class for handling requests on listener
118
+ /// </summary>
119
+ class MessageProcessor : IMessageProcessor
120
+ {
121
+ int received ;
122
+ int count ;
123
+ ReceiverOptions options ;
124
+ ContainerHost host ;
125
+
126
+ /// <summary>
127
+ /// Constructor of MessageProcessor
128
+ /// </summary>
129
+ /// <param name="options">receiver options</param>
130
+ /// <param name="host">container host listener</param>
131
+ public MessageProcessor ( ReceiverOptions options , ContainerHost host )
132
+ {
133
+ this . received = 0 ;
134
+ this . options = options ;
135
+ this . count = options . MsgCount ;
136
+ this . host = host ;
137
+ }
138
+
139
+ public int Credit { get { return options . Count ; } }
140
+
141
+ /// <summary>
142
+ /// init of message processor
143
+ /// </summary>
144
+ /// <param name="messageContext">context of messsage</param>
145
+ public void Process ( MessageContext messageContext )
146
+ {
147
+ var task = this . ReplyAsync ( messageContext ) ;
148
+ }
149
+
150
+ /// <summary>
151
+ /// Async tassk for handling requst
152
+ /// </summary>
153
+ /// <param name="messageContext">context of message</param>
154
+ /// <returns>async task</returns>
155
+ async Task ReplyAsync ( MessageContext messageContext )
156
+ {
157
+ while ( this . received < count )
158
+ {
159
+ try
160
+ {
161
+ Message message = messageContext . Message ;
162
+ Formatter . LogMessage ( message , options ) ;
163
+ this . received ++ ;
164
+ messageContext . Complete ( ) ;
165
+ }
166
+ catch ( Exception exception )
167
+ {
168
+ Console . Error . WriteLine ( "ERROR: {{'cause': '{0}'}}" , exception . Message ) ;
169
+ break ;
170
+ }
171
+
172
+ await Task . Delay ( 500 ) ;
173
+ }
174
+ host . Close ( ) ;
175
+ }
176
+ }
177
+
178
+ #endregion
179
+
89
180
#region Receive methods
90
181
/// <summary>
91
182
/// Method for browse or selector receive
@@ -208,61 +299,68 @@ public void Run(string[] args)
208
299
{
209
300
this . ParseArguments ( args , options ) ;
210
301
211
- //init timestamping
212
- this . ptsdata = Utils . TsInit ( options . LogStats ) ;
302
+ if ( options . RecvListener )
303
+ {
304
+ this . InitListener ( options ) ;
305
+ }
306
+ else
307
+ {
308
+ //init timestamping
309
+ this . ptsdata = Utils . TsInit ( options . LogStats ) ;
213
310
214
- Utils . TsSnapStore ( this . ptsdata , 'B' , options . LogStats ) ;
311
+ Utils . TsSnapStore ( this . ptsdata , 'B' , options . LogStats ) ;
215
312
216
- this . SetAddress ( options . Url ) ;
217
- this . CreateConnection ( options ) ;
313
+ this . SetAddress ( options . Url ) ;
314
+ this . CreateConnection ( options ) ;
218
315
219
- Utils . TsSnapStore ( this . ptsdata , 'C' , options . LogStats ) ;
316
+ Utils . TsSnapStore ( this . ptsdata , 'C' , options . LogStats ) ;
220
317
221
- this . CreateSession ( ) ;
318
+ this . CreateSession ( ) ;
222
319
223
- Utils . TsSnapStore ( this . ptsdata , 'D' , options . LogStats ) ;
320
+ Utils . TsSnapStore ( this . ptsdata , 'D' , options . LogStats ) ;
224
321
225
- ReceiverLink receiver = this . PeprareReceiverLink ( options ) ;
322
+ ReceiverLink receiver = this . PeprareReceiverLink ( options ) ;
226
323
227
- Message message = new Message ( ) ;
324
+ Message message = new Message ( ) ;
228
325
229
- this . ts = Utils . GetTime ( ) ;
326
+ this . ts = Utils . GetTime ( ) ;
230
327
231
- Utils . TsSnapStore ( this . ptsdata , 'E' , options . LogStats ) ;
232
- int nReceived = 0 ;
328
+ Utils . TsSnapStore ( this . ptsdata , 'E' , options . LogStats ) ;
329
+ int nReceived = 0 ;
233
330
234
- if ( options . Capacity > - 1 )
235
- receiver . SetCredit ( options . Capacity ) ;
331
+ if ( options . Capacity > - 1 )
332
+ receiver . SetCredit ( options . Capacity ) ;
236
333
237
- bool tx_batch_flag = String . IsNullOrEmpty ( options . TxLoopendAction ) ? ( options . TxSize > 0 ) : true ;
334
+ bool tx_batch_flag = String . IsNullOrEmpty ( options . TxLoopendAction ) ? ( options . TxSize > 0 ) : true ;
238
335
239
- //receiving of messages
240
- if ( options . RecvBrowse || ! String . IsNullOrEmpty ( options . MsgSelector ) )
241
- this . ReceiveAll ( receiver , options ) ;
242
- else
243
- {
244
- if ( tx_batch_flag )
245
- this . TransactionReceive ( receiver , options ) ;
336
+ //receiving of messages
337
+ if ( options . RecvBrowse || ! String . IsNullOrEmpty ( options . MsgSelector ) )
338
+ this . ReceiveAll ( receiver , options ) ;
246
339
else
247
- this . Receive ( receiver , options ) ;
248
- }
340
+ {
341
+ if ( tx_batch_flag )
342
+ this . TransactionReceive ( receiver , options ) ;
343
+ else
344
+ this . Receive ( receiver , options ) ;
345
+ }
249
346
250
- if ( options . CloseSleep > 0 )
251
- {
252
- System . Threading . Thread . Sleep ( options . CloseSleep ) ;
253
- }
347
+ if ( options . CloseSleep > 0 )
348
+ {
349
+ System . Threading . Thread . Sleep ( options . CloseSleep ) ;
350
+ }
254
351
255
- //close connection and link
256
- this . CloseLink ( receiver ) ;
257
- this . CloseConnection ( ) ;
352
+ //close connection and link
353
+ this . CloseLink ( receiver ) ;
354
+ this . CloseConnection ( ) ;
258
355
259
- Utils . TsSnapStore ( this . ptsdata , 'G' , options . LogStats ) ;
356
+ Utils . TsSnapStore ( this . ptsdata , 'G' , options . LogStats ) ;
260
357
261
- //report timestamping
262
- if ( this . ptsdata . Count > 0 )
263
- {
264
- Console . WriteLine ( "STATS " + Utils . TsReport ( this . ptsdata ,
265
- nReceived , message . Body . ToString ( ) . Length * sizeof ( Char ) , 0 ) ) ;
358
+ //report timestamping
359
+ if ( this . ptsdata . Count > 0 )
360
+ {
361
+ Console . WriteLine ( "STATS " + Utils . TsReport ( this . ptsdata ,
362
+ nReceived , message . Body . ToString ( ) . Length * sizeof ( Char ) , 0 ) ) ;
363
+ }
266
364
}
267
365
this . exitCode = ReturnCode . ERROR_SUCCESS ;
268
366
}
0 commit comments