Merge pull request #704 from jgagnon/master
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / HandlerTransportBindingElement.cs
1 //
2 // HandlerTransportBindingElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.IO;
30 using System.Net;
31 using System.Net.Security;
32 using System.Threading;
33 using System.Xml;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
37
38 namespace MonoTests.System.ServiceModel.Channels
39 {
40         public delegate Message RequestSender (Message input);
41         public delegate Message RequestReceiver ();
42         public delegate void ReplyHandler (Message input);
43
44         public class HandlerTransportBindingElement : TransportBindingElement
45         {
46                 RequestSender sender;
47
48                 ReplyHandler reply_handler;
49                 RequestReceiver receiver;
50
51                 public HandlerTransportBindingElement (RequestSender sender)
52                 {
53                         this.sender = sender;
54                 }
55
56                 public HandlerTransportBindingElement (ReplyHandler handler, RequestReceiver receiver)
57                 {
58                         this.reply_handler = handler;
59                         this.receiver = receiver;
60                 }
61
62                 public RequestSender RequestSender {
63                         get { return sender; }
64                 }
65
66                 public ReplyHandler ReplyHandler {
67                         get { return reply_handler; }
68                 }
69
70                 public RequestReceiver RequestReceiver {
71                         get { return receiver; }
72                 }
73
74                 public override string Scheme {
75                         get { return "stream"; }
76                 }
77
78                 public override BindingElement Clone ()
79                 {
80                         if (sender != null)
81                                 return new HandlerTransportBindingElement (sender);
82                         else
83                                 return new HandlerTransportBindingElement (reply_handler, receiver);
84                 }
85
86                 public override bool CanBuildChannelFactory<TChannel> (BindingContext context)
87                 {
88                         return typeof (TChannel) == typeof (IRequestChannel) ||
89                                 typeof (TChannel) == typeof (IRequestChannel);
90                 }
91
92                 public override bool CanBuildChannelListener<TChannel> (BindingContext context)
93                 {
94                         return typeof (TChannel) == typeof (IReplyChannel) ||
95                                 typeof (TChannel) == typeof (IInputChannel);
96                 }
97
98                 public override IChannelFactory<TChannel> BuildChannelFactory<TChannel> (BindingContext context)
99                 {
100                         return new HandlerTransportChannelFactory<TChannel> (this);
101                 }
102
103                 public override IChannelListener<TChannel> BuildChannelListener<TChannel> (BindingContext context)
104                 {
105                         // FIXME: pass uri
106                         return new HandlerTransportChannelListener<TChannel> (this, new Uri ("stream:dummy"));
107                 }
108         }
109
110         public class HandlerTransportChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
111         {
112                 HandlerTransportBindingElement source;
113
114                 public HandlerTransportChannelFactory (HandlerTransportBindingElement source)
115                 {
116                         this.source = source;
117                 }
118
119                 public HandlerTransportBindingElement Source {
120                         get { return source; }
121                 }
122
123                 protected override TChannel OnCreateChannel (EndpointAddress address, Uri via)
124                 {
125                         if (typeof (TChannel) == typeof (IRequestChannel))
126                                 return (TChannel) (object) new HandlerTransportRequestChannel ((HandlerTransportChannelFactory<IRequestChannel>) (object) this, address, via);
127                         if (typeof (TChannel) == typeof (IOutputChannel))
128                                 return (TChannel) (object) new HandlerTransportOutputChannel ((HandlerTransportChannelFactory<IOutputChannel>) (object) this, address, via);
129
130                         throw new NotSupportedException (String.Format ("Channel '{0}' is not supported.", typeof (TChannel)));
131                 }
132
133                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
134                 {
135                         throw new NotSupportedException ();
136                 }
137
138                 protected override void OnEndOpen (IAsyncResult result)
139                 {
140                         throw new NotSupportedException ();
141                 }
142
143                 protected override void OnOpen (TimeSpan timeout)
144                 {
145                         // do nothing
146                 }
147         }
148
149         public class HandlerTransportOutputChannel : OutputChannelBase
150         {
151                 HandlerTransportChannelFactory<IOutputChannel> source;
152                 EndpointAddress address;
153                 Uri via;
154
155                 public HandlerTransportOutputChannel (HandlerTransportChannelFactory<IOutputChannel> source, EndpointAddress address, Uri via)
156                         : base (source)
157                 {
158                         this.source = source;
159                         this.address = address;
160                         this.via = via;
161                 }
162
163                 public override EndpointAddress RemoteAddress {
164                         get { return address; }
165                 }
166
167                 public override Uri Via {
168                         get { return via; }
169                 }
170
171                 public override void Send (Message input, TimeSpan timeout)
172                 {
173                         source.Source.RequestSender (input);
174                 }
175
176                 class OutputAsyncResult : IAsyncResult
177                 {
178                         Message message;
179                         object state;
180                         bool completed = true;
181
182                         public OutputAsyncResult (Message message, object state)
183                         {
184                                 this.message = message;
185                                 this.state = state;
186                         }
187
188                         public Message Message {
189                                 get { return message; }
190                         }
191
192                         public object AsyncState {
193                                 get { return state; }
194                         }
195
196                         public WaitHandle AsyncWaitHandle {
197                                 get { return null; }
198                         }
199
200                         public bool CompletedSynchronously {
201                                 get { return true; }
202                         }
203
204                         public bool IsCompleted {
205                                 get { return completed; }
206                                 internal set { completed = value; }
207                         }
208                 }
209
210                 public override IAsyncResult BeginSend (Message input, TimeSpan timeout, AsyncCallback callback, object state)
211                 {
212                         // FIXME: timeout is not considered here.
213                         return new OutputAsyncResult (input, state);
214                 }
215
216                 public override void EndSend (IAsyncResult result)
217                 {
218                         source.Source.RequestSender (((OutputAsyncResult) result).Message);
219                 }
220
221
222                 protected override void OnAbort ()
223                 {
224                 }
225
226                 protected override void OnOpen (TimeSpan timeout)
227                 {
228                         // throw new NotImplementedException ("OnOpen");
229                 }
230
231                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
232                 {
233                         throw new NotImplementedException ("OnBeginOpen");
234                 }
235
236                 protected override void OnEndOpen (IAsyncResult result)
237                 {
238                         throw new NotImplementedException ("OnEndOpen");
239                 }
240
241                 protected override void OnClose (TimeSpan timeout)
242                 {
243                         // throw new NotImplementedException ("OnClose");
244                 }
245
246                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
247                 {
248                         throw new NotImplementedException ("OnBeginClose");
249                 }
250
251                 protected override void OnEndClose (IAsyncResult result)
252                 {
253                         throw new NotImplementedException ("OnEndClose");
254                 }
255         }
256
257         public class HandlerTransportRequestChannel : RequestChannelBase
258         {
259                 HandlerTransportChannelFactory<IRequestChannel> source;
260                 EndpointAddress address;
261                 Uri via;
262
263                 public HandlerTransportRequestChannel (HandlerTransportChannelFactory<IRequestChannel> source, EndpointAddress address, Uri via)
264                         : base (source)
265                 {
266                         this.source = source;
267                         this.address = address;
268                         this.via = via;
269                 }
270
271                 public override EndpointAddress RemoteAddress {
272                         get { return address; }
273                 }
274
275                 public override Uri Via {
276                         get { return via; }
277                 }
278
279                 public override Message Request (Message input, TimeSpan timeout)
280                 {
281                         return source.Source.RequestSender (input);
282                 }
283
284                 class RequestAsyncResult : IAsyncResult
285                 {
286                         Message message;
287                         object state;
288                         bool completed = true;
289
290                         public RequestAsyncResult (Message message, object state)
291                         {
292                                 this.message = message;
293                                 this.state = state;
294                         }
295
296                         public Message Message {
297                                 get { return message; }
298                         }
299
300                         public object AsyncState {
301                                 get { return state; }
302                         }
303
304                         public WaitHandle AsyncWaitHandle {
305                                 get { return null; }
306                         }
307
308                         public bool CompletedSynchronously {
309                                 get { return true; }
310                         }
311
312                         public bool IsCompleted {
313                                 get { return completed; }
314                                 internal set { completed = value; }
315                         }
316                 }
317
318                 public override IAsyncResult BeginRequest (Message input, TimeSpan timeout, AsyncCallback callback, object state)
319                 {
320                         // FIXME: timeout is not considered here.
321                         return new RequestAsyncResult (input, state);
322                 }
323
324                 public override Message EndRequest (IAsyncResult result)
325                 {
326                         return source.Source.RequestSender (((RequestAsyncResult) result).Message);
327                 }
328
329
330                 protected override void OnAbort ()
331                 {
332                 }
333
334                 protected override void OnOpen (TimeSpan timeout)
335                 {
336                         // throw new NotImplementedException ("OnOpen");
337                 }
338
339                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
340                 {
341                         throw new NotImplementedException ("OnBeginOpen");
342                 }
343
344                 protected override void OnEndOpen (IAsyncResult result)
345                 {
346                         throw new NotImplementedException ("OnEndOpen");
347                 }
348
349                 protected override void OnClose (TimeSpan timeout)
350                 {
351                         // throw new NotImplementedException ("OnClose");
352                 }
353
354                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
355                 {
356                         throw new NotImplementedException ("OnBeginClose");
357                 }
358
359                 protected override void OnEndClose (IAsyncResult result)
360                 {
361                         throw new NotImplementedException ("OnEndClose");
362                 }
363         }
364
365         public class HandlerTransportChannelListener<TChannel>
366                 : ChannelListenerBase<TChannel>
367                 where TChannel : class, IChannel
368         {
369                 HandlerTransportBindingElement source;
370                 Uri uri;
371
372                 public HandlerTransportChannelListener (HandlerTransportBindingElement source, Uri uri)
373                 {
374                         this.source = source;
375                         this.uri = uri;
376                 }
377
378                 public HandlerTransportBindingElement Source {
379                         get { return source; }
380                 }
381
382
383                 public override Uri Uri {
384                         get { return uri; }
385                 }
386
387                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
388                 {
389                         if (typeof (TChannel) == typeof (IReplyChannel))
390                                 return (TChannel) (object) new HandlerTransportReplyChannel ((HandlerTransportChannelListener<IReplyChannel>) (object) this);
391
392                         throw new NotSupportedException ();
393                 }
394
395                 protected override IAsyncResult OnBeginAcceptChannel (TimeSpan timeout, AsyncCallback callback, object state)
396                 {
397                         throw new NotImplementedException ("OnBeginAcceptChannel");
398                 }
399
400                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
401                 {
402                         throw new NotImplementedException ("EndAcceptChannel");
403                 }
404
405                 protected override bool OnWaitForChannel (TimeSpan timeout)
406                 {
407                         return true;
408                 }
409
410                 protected override IAsyncResult OnBeginWaitForChannel (TimeSpan timeout, AsyncCallback callback, object state)
411                 {
412                         throw new NotImplementedException ("OnBeginWaitForChannel");
413                 }
414
415                 protected override bool OnEndWaitForChannel (IAsyncResult result)
416                 {
417                         throw new NotImplementedException ("EndWaitForChannel");
418                 }
419
420
421                 protected override void OnAbort ()
422                 {
423                 }
424
425                 protected override void OnOpen (TimeSpan timeout)
426                 {
427                         // throw new NotImplementedException ("OnOpen");
428                 }
429
430                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
431                 {
432                         throw new NotImplementedException ("OnBeginOpen");
433                 }
434
435                 protected override void OnEndOpen (IAsyncResult result)
436                 {
437                         throw new NotImplementedException ("EndOpen");
438                 }
439
440                 protected override void OnClose (TimeSpan timeout)
441                 {
442                         //throw new NotImplementedException ("Close");
443                 }
444
445                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
446                 {
447                         throw new NotImplementedException ("OnBeginClose");
448                 }
449
450                 protected override void OnEndClose (IAsyncResult result)
451                 {
452                         throw new NotImplementedException ("OnEndClose");
453                 }
454         }
455
456         public class HandlerTransportReplyChannel : ReplyChannelBase
457         {
458                 EndpointAddress address;
459                 HandlerTransportChannelListener<IReplyChannel> source;
460
461                 public HandlerTransportReplyChannel (HandlerTransportChannelListener<IReplyChannel> source)
462                         : base (source)
463                 {
464                         this.source = source;
465                         address = new EndpointAddress (source.Uri);
466                 }
467
468                 public HandlerTransportChannelListener<IReplyChannel> Source {
469                         get { return source; }
470                 }
471
472                 public override EndpointAddress LocalAddress {
473                         get { return address; }
474                 }
475
476                 class ReceiveRequestAsyncResult : IAsyncResult
477                 {
478                         object state;
479                         bool completed = true;
480
481                         public ReceiveRequestAsyncResult (object state)
482                         {
483                                 this.state = state;
484                         }
485
486                         public object AsyncState {
487                                 get { return state; }
488                         }
489
490                         public WaitHandle AsyncWaitHandle {
491                                 get { return null; }
492                         }
493
494                         public bool CompletedSynchronously {
495                                 get { return true; }
496                         }
497
498                         public bool IsCompleted {
499                                 get { return completed; }
500                                 internal set { completed = value; }
501                         }
502                 }
503
504                 public override RequestContext ReceiveRequest (TimeSpan timeout)
505                 {
506                         RequestContext ret;
507                         if (!TryReceiveRequest (timeout, out ret))
508                                 throw new Exception ();
509                         return ret;
510                 }
511
512                 public override IAsyncResult BeginReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
513                 {
514                         return new ReceiveRequestAsyncResult (state);
515                 }
516
517                 public override RequestContext EndReceiveRequest (IAsyncResult result)
518                 {
519                         //ReceiveRequestAsyncResult r =
520                         //      (ReceiveRequestAsyncResult) result;
521                         return new HandlerRequestContext (this);
522                 }
523
524                 public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext ret)
525                 {
526                         ret = new HandlerRequestContext (this);
527                         return true;
528                 }
529
530                 public override IAsyncResult BeginTryReceiveRequest (TimeSpan timeout, AsyncCallback callback, object state)
531                 {
532                         // hack, hack
533                         return new ReceiveRequestAsyncResult (state);
534                 }
535
536                 public override bool EndTryReceiveRequest (IAsyncResult result, out RequestContext ret)
537                 {
538                         // hack, hack
539                         //ReceiveRequestAsyncResult r =
540                         //      (ReceiveRequestAsyncResult) result;
541                         return TryReceiveRequest (TimeSpan.FromSeconds (5), out ret);
542                 }
543
544                 public override bool WaitForRequest (TimeSpan timeout)
545                 {
546                         throw new NotImplementedException ();
547                 }
548
549                 public override IAsyncResult BeginWaitForRequest (TimeSpan timeout, AsyncCallback callback, object state)
550                 {
551                         throw new NotImplementedException ("BeginWaitForRequest");
552                 }
553
554                 public override bool EndWaitForRequest (IAsyncResult result)
555                 {
556                         throw new NotImplementedException ("EndWaitForRequest");
557                 }
558
559
560                 protected override void OnAbort ()
561                 {
562                 }
563
564                 protected override void OnOpen (TimeSpan timeout)
565                 {
566                         // throw new NotImplementedException ("OnOpen");
567                 }
568
569                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
570                 {
571                         throw new NotImplementedException ("OnBeginOpen");
572                 }
573
574                 protected override void OnEndOpen (IAsyncResult result)
575                 {
576                         throw new NotImplementedException ("EndOpen");
577                 }
578
579                 protected override void OnClose (TimeSpan timeout)
580                 {
581                         //throw new NotImplementedException ("Close");
582                 }
583
584                 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
585                 {
586                         throw new NotImplementedException ("OnBeginClose");
587                 }
588
589                 protected override void OnEndClose (IAsyncResult result)
590                 {
591                         throw new NotImplementedException ("OnEndClose");
592                 }
593         }
594
595         public class HandlerRequestContext : RequestContext
596         {
597                 HandlerTransportReplyChannel source;
598                 Message request_message;
599
600                 public HandlerRequestContext (HandlerTransportReplyChannel source)
601                 {
602                         this.source = source;
603                         if (source.Source.Source.RequestReceiver != null)
604                                 request_message = source.Source.Source.RequestReceiver ();
605                 }
606
607
608                 public override void Abort ()
609                 {
610                 }
611
612                 public override void Close ()
613                 {
614                 }
615
616                 public override void Close (TimeSpan timeout)
617                 {
618                 }
619
620                 public override Message RequestMessage {
621                         get { return request_message; }
622                 }
623
624                 public override void Reply (Message msg)
625                 {
626                         source.Source.Source.ReplyHandler (msg);
627                 }
628
629                 public override void Reply (Message msg, TimeSpan timeout)
630                 {
631                         source.Source.Source.ReplyHandler (msg);
632                 }
633
634                 public override IAsyncResult BeginReply (Message msg, AsyncCallback callback, object state)
635                 {
636                         throw new NotImplementedException ("BeginReply");
637                 }
638
639                 public override IAsyncResult BeginReply (Message msg, TimeSpan timeout, AsyncCallback callback, object state)
640                 {
641                         throw new NotImplementedException ("BeginReply");
642                 }
643
644                 public override void EndReply (IAsyncResult result)
645                 {
646                         throw new NotImplementedException ("EndReply");
647                 }
648         }
649 }