2010-05-14 Andreia Gaita <avidigal@novell.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / MsmqChannelListener.cs
1 //
2 // MsmqChannelListener.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 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.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Messaging;
32 using System.ServiceModel;
33 using System.ServiceModel.Description;
34 using System.ServiceModel.Security;
35 using System.Text;
36
37 namespace System.ServiceModel.Channels
38 {
39         internal class MsmqChannelListener<TChannel> : ChannelListenerBase<TChannel>
40                 where TChannel : class, IChannel
41         {
42                 MsmqTransportBindingElement source;
43                 BindingContext context;
44                 Uri listen_uri;
45                 MessageQueue queue;
46                 List<IChannel> channels = new List<IChannel> ();
47                 MessageEncoder encoder;
48
49                 public MsmqChannelListener (MsmqTransportBindingElement source,
50                         BindingContext context)
51                         : base (context.Binding)
52                 {
53                         if (context.ListenUriMode == ListenUriMode.Explicit)
54                                 listen_uri = new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress);
55                         else
56                                 // FIXME: consider ListenUriMode.Unique
57                                 throw new NotImplementedException ();
58
59                         foreach (BindingElement be in context.Binding.Elements) {
60                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
61                                 if (mbe != null) {
62                                         encoder = CreateEncoder<TChannel> (mbe);
63                                         break;
64                                 }
65                         }
66                         if (encoder == null)
67                                 encoder = new BinaryMessageEncoder ();
68                 }
69
70                 public MessageQueue Queue {
71                         get { return queue; }
72                 }
73
74                 public MessageEncoder MessageEncoder {
75                         get { return encoder; }
76                 }
77
78                 public override Uri Uri {
79                         get { return listen_uri; }
80                 }
81
82                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
83                 {
84                         TChannel ch = PopulateChannel (timeout);
85                         channels.Add (ch);
86                         return ch;
87                 }
88
89                 TChannel PopulateChannel (TimeSpan timeout)
90                 {
91                         if (typeof (TChannel) == typeof (IInputChannel)) {
92                                 return (TChannel) (object) new MsmqInputChannel (
93                                         (MsmqChannelListener<IInputChannel>) (object) this, timeout);
94                         }
95
96                         // FIXME: implement more
97
98                         throw new NotImplementedException ();
99                 }
100
101                 protected override IAsyncResult OnBeginAcceptChannel (
102                         TimeSpan timeout, AsyncCallback callback,
103                         object asyncState)
104                 {
105                         throw new NotImplementedException ();
106                 }
107
108                 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
109                 {
110                         throw new NotImplementedException ();
111                 }
112
113                 protected override IAsyncResult OnBeginWaitForChannel (
114                         TimeSpan timeout, AsyncCallback callback, object state)
115                 {
116                         throw new NotImplementedException ();
117                 }
118
119                 protected override bool OnEndWaitForChannel (IAsyncResult result)
120                 {
121                         throw new NotImplementedException ();
122                 }
123
124                 protected override bool OnWaitForChannel (TimeSpan timeout)
125                 {
126                         throw new NotImplementedException ();
127                 }
128                 
129                 void StartListening (TimeSpan timeout)
130                 {
131                         if (queue != null)
132                                 throw new InvalidOperationException ("This listener is already waiting for connection.");
133
134                         queue = new MessageQueue (listen_uri.GetLeftPart (UriPartial.Scheme));
135                 }
136
137                 protected override void OnOpen (TimeSpan timeout)
138                 {
139                         StartListening (timeout);
140                 }
141
142                 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
143                         AsyncCallback callback, object state)
144                 {
145                         throw new NotImplementedException ();
146                 }
147
148                 protected override void OnEndOpen (IAsyncResult result)
149                 {
150                         throw new NotImplementedException ();
151                 }
152
153                 protected override void OnClose (TimeSpan timeout)
154                 {
155                         // FIXME: somewhere to use timeout?
156                         if (queue == null)
157                                 return;
158                         queue.Dispose ();
159                 }
160
161                 [MonoTODO]
162                 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
163                         AsyncCallback callback, object state)
164                 {
165                         throw new NotImplementedException ();
166                 }
167
168                 [MonoTODO]
169                 protected override void OnEndClose (IAsyncResult result)
170                 {
171                         throw new NotImplementedException ();
172                 }
173
174                 [MonoTODO ("find out what to do here.")]
175                 protected override void OnAbort ()
176                 {
177                 }
178         }
179 }