2009-06-18 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / HttpChannelListener.cs
1 //
2 // HttpChannelListener.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.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.Net;
32 using System.Net.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Text;
37
38 namespace System.ServiceModel.Channels
39 {
40         internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
41                 where TChannel : class, IChannel
42         {
43                 HttpListenerManager<TChannel> httpChannelManager;
44
45                 public HttpSimpleChannelListener (HttpTransportBindingElement source,
46                         BindingContext context)
47                         : base (source, context)
48                 {
49                 }
50
51                 public HttpListener Http {
52                         get {  return httpChannelManager.HttpListener; }
53                 }
54
55                 object creator_lock = new object ();
56
57                 protected override TChannel CreateChannel (TimeSpan timeout)
58                 {
59                         lock (creator_lock) {
60                                 return CreateChannelCore (timeout);
61                         }
62                 }
63
64                 TChannel CreateChannelCore (TimeSpan timeout)
65                 {
66                         if (typeof (TChannel) == typeof (IReplyChannel))
67                                 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
68                         // FIXME: session channel support
69                         if (typeof (TChannel) == typeof (IReplySessionChannel))
70                                 throw new NotImplementedException ();
71
72                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
73                 }
74
75                 protected override void OnOpen (TimeSpan timeout)
76                 {
77                         base.OnOpen (timeout);
78                         StartListening (timeout);
79                 }
80
81                 protected override void OnAbort ()
82                 {
83                         httpChannelManager.Stop (true);
84                 }
85
86                 protected override void OnClose (TimeSpan timeout)
87                 {
88                         if (State == CommunicationState.Closed)
89                                 return;
90                         base.OnClose (timeout);
91                         // FIXME: it is said that channels are not closed
92                         // when the channel listener is closed.
93                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
94                         httpChannelManager.Stop (false);
95                 }
96
97                 void StartListening (TimeSpan timeout)
98                 {
99                         httpChannelManager = new HttpListenerManager<TChannel> (this);
100                         httpChannelManager.Open (timeout);
101                 }
102         }
103
104         internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
105                 where TChannel : class, IChannel
106         {
107                 public AspNetChannelListener (HttpTransportBindingElement source,
108                         BindingContext context)
109                         : base (source, context)
110                 {
111                 }
112
113                 protected override TChannel CreateChannel (TimeSpan timeout)
114                 {
115                         if (typeof (TChannel) == typeof (IReplyChannel))
116                                 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
117                         // FIXME: session channel support
118                         if (typeof (TChannel) == typeof (IReplySessionChannel))
119                                 throw new NotImplementedException ();
120
121                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
122                 }
123         }
124
125         internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
126                 where TChannel : class, IChannel
127         {
128                 HttpTransportBindingElement source;
129                 BindingContext context;
130                 Uri listen_uri;
131                 List<TChannel> channels = new List<TChannel> ();
132                 MessageEncoder encoder;
133
134                 public HttpChannelListenerBase (HttpTransportBindingElement source,
135                         BindingContext context)
136                         : base (context.Binding)
137                 {
138
139                         // FIXME: consider ListenUriMode
140                         // FIXME: there should be some way to post-provide Uri in case of null listenerUri in context.
141                         listen_uri = context.ListenUriBaseAddress != null ?
142                                 new Uri (context.ListenUriBaseAddress, context.ListenUriRelativeAddress) : null;
143                         foreach (BindingElement be in context.RemainingBindingElements) {
144                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
145                                 if (mbe != null) {
146                                         encoder = CreateEncoder<TChannel> (mbe);
147                                         break;
148                                 }
149                         }
150                         if (encoder == null)
151                                 encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
152                 }
153
154                 public MessageEncoder MessageEncoder {
155                         get { return encoder; }
156                 }
157
158                 public override Uri Uri {
159                         get { return listen_uri; }
160                 }
161
162                 protected IList<TChannel> Channels {
163                         get { return channels; }
164                 }
165
166                 protected override TChannel OnAcceptChannel (TimeSpan timeout)
167                 {
168                         TChannel ch = CreateChannel (timeout);
169                         Channels.Add (ch);
170                         return ch;
171                 }
172
173                 protected abstract TChannel CreateChannel (TimeSpan timeout);
174
175                 protected override bool OnWaitForChannel (TimeSpan timeout)
176                 {
177                         throw new NotImplementedException ();
178                 }
179
180                 protected override void OnAbort ()
181                 {
182                         OnClose (TimeSpan.Zero);
183                 }
184
185                 protected override void OnOpen (TimeSpan timeout)
186                 {
187                 }
188
189                 protected override void OnClose (TimeSpan timeout)
190                 {
191                         DateTime start = DateTime.Now;
192                         foreach (TChannel ch in Channels)
193                                 ch.Close (timeout - (DateTime.Now - start));
194                         base.OnClose (timeout - (DateTime.Now - start));
195                 }
196         }
197 }