2009-09-29 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.Linq;
32 using System.Net;
33 using System.Net.Security;
34 using System.ServiceModel;
35 using System.ServiceModel.Description;
36 using System.ServiceModel.Security;
37 using System.Text;
38
39 namespace System.ServiceModel.Channels
40 {
41         internal class HttpSimpleChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
42                 where TChannel : class, IChannel
43         {
44                 HttpListenerManager<TChannel> httpChannelManager;
45
46                 public HttpSimpleChannelListener (HttpTransportBindingElement source,
47                         BindingContext context)
48                         : base (source, context)
49                 {
50                 }
51
52                 public HttpListener Http {
53                         get {  return httpChannelManager.HttpListener; }
54                 }
55
56                 object creator_lock = new object ();
57
58                 protected override TChannel CreateChannel (TimeSpan timeout)
59                 {
60                         lock (creator_lock) {
61                                 return CreateChannelCore (timeout);
62                         }
63                 }
64
65                 TChannel CreateChannelCore (TimeSpan timeout)
66                 {
67                         if (typeof (TChannel) == typeof (IReplyChannel))
68                                 return (TChannel) (object) new HttpSimpleReplyChannel ((HttpSimpleChannelListener<IReplyChannel>) (object) this);
69                         // FIXME: session channel support
70                         if (typeof (TChannel) == typeof (IReplySessionChannel))
71                                 throw new NotImplementedException ();
72
73                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
74                 }
75
76                 protected override void OnOpen (TimeSpan timeout)
77                 {
78                         base.OnOpen (timeout);
79                         StartListening (timeout);
80                 }
81
82                 protected override void OnAbort ()
83                 {
84                         httpChannelManager.Stop (true);
85                 }
86
87                 protected override void OnClose (TimeSpan timeout)
88                 {
89                         if (State == CommunicationState.Closed)
90                                 return;
91                         base.OnClose (timeout);
92                         // FIXME: it is said that channels are not closed
93                         // when the channel listener is closed.
94                         // http://blogs.msdn.com/drnick/archive/2006/03/22/557642.aspx
95                         httpChannelManager.Stop (false);
96                 }
97
98                 void StartListening (TimeSpan timeout)
99                 {
100                         httpChannelManager = new HttpListenerManager<TChannel> (this);
101                         httpChannelManager.Open (timeout);
102                 }
103         }
104
105         internal class AspNetChannelListener<TChannel> : HttpChannelListenerBase<TChannel>
106                 where TChannel : class, IChannel
107         {
108                 public AspNetChannelListener (HttpTransportBindingElement source,
109                         BindingContext context)
110                         : base (source, context)
111                 {
112                 }
113
114                 SvcHttpHandler http_handler;
115                 internal SvcHttpHandler HttpHandler {
116                         get {
117                                 if (http_handler == null)
118                                         http_handler = SvcHttpHandlerFactory.GetHandlerForListener (this);
119                                 return http_handler;
120                         }
121                 }
122
123                 protected override TChannel CreateChannel (TimeSpan timeout)
124                 {
125                         if (typeof (TChannel) == typeof (IReplyChannel))
126                                 return (TChannel) (object) new AspNetReplyChannel ((AspNetChannelListener<IReplyChannel>) (object) this);
127                         // FIXME: session channel support
128                         if (typeof (TChannel) == typeof (IReplySessionChannel))
129                                 throw new NotImplementedException ();
130
131                         throw new NotSupportedException (String.Format ("Channel type {0} is not supported", typeof (TChannel)));
132                 }
133         }
134
135         internal abstract class HttpChannelListenerBase<TChannel> : InternalChannelListenerBase<TChannel>
136                 where TChannel : class, IChannel
137         {
138                 HttpTransportBindingElement source;
139                 BindingContext context;
140                 List<TChannel> channels = new List<TChannel> ();
141                 MessageEncoder encoder;
142
143                 public HttpChannelListenerBase (HttpTransportBindingElement source,
144                         BindingContext context)
145                         : base (context)
146                 {
147                         foreach (BindingElement be in context.RemainingBindingElements) {
148                                 MessageEncodingBindingElement mbe = be as MessageEncodingBindingElement;
149                                 if (mbe != null) {
150                                         encoder = CreateEncoder<TChannel> (mbe);
151                                         break;
152                                 }
153                         }
154                         if (encoder == null)
155                                 encoder = new TextMessageEncoder (MessageVersion.Default, Encoding.UTF8);
156                 }
157
158                 public MessageEncoder MessageEncoder {
159                         get { return encoder; }
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 new List<TChannel> (Channels)) // they will be removed during iteration, so create another one
193                                 ch.Close (timeout - (DateTime.Now - start));
194                         base.OnClose (timeout - (DateTime.Now - start));
195                 }
196         }
197 }