Merge pull request #205 from m3rlinez/master
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / InstanceContext.cs
1 //
2 // InstanceContext.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-2006 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.ServiceModel.Channels;
31 using System.ServiceModel.Dispatcher;
32
33 namespace System.ServiceModel
34 {
35         public sealed class InstanceContext : CommunicationObject,
36                 IExtensibleObject<InstanceContext>
37         {
38                 ServiceHostBase host;
39                 object implementation;
40                 int manual_flow_limit;
41                 InstanceManager instance_manager;
42                 bool is_user_instance_provider;
43                 bool is_user_context_provider;
44                 ExtensionCollection<InstanceContext> _extensions;
45
46                 static InstanceContextIdleCallback idle_callback = new InstanceContextIdleCallback(NotifyIdle);
47
48                 public InstanceContext (object implementation)
49                         : this (null, implementation)
50                 {
51                 }
52
53                 public InstanceContext (ServiceHostBase host)
54                         : this (host, null)
55                 {
56                 }
57
58                 public InstanceContext (ServiceHostBase host, object implementation)
59                         : this (host, implementation, true)
60                 {
61                 }
62
63                 internal InstanceContext (ServiceHostBase host, object implementation, bool userContextProvider)
64                 {
65                         this.host = host;
66                         this.implementation = implementation;
67                         is_user_context_provider = userContextProvider;
68                 }
69
70                 internal bool IsUserProvidedInstance {
71                         get {
72                                 return is_user_instance_provider;
73                         }
74                 }
75
76                 internal bool IsUserProvidedContext {
77                         get { return is_user_context_provider; }
78                 }
79
80                 internal InstanceManager InstanceManager {
81                         get { return instance_manager; }
82                         set { instance_manager = value; }
83                 }
84
85                 protected internal override TimeSpan DefaultCloseTimeout {
86                         get { return host.DefaultCloseTimeout; }
87                 }
88
89                 protected internal override TimeSpan DefaultOpenTimeout {
90                         get { return host.DefaultOpenTimeout; }
91                 }
92
93                 public IExtensionCollection<InstanceContext> Extensions {
94                         get {
95                                 if (_extensions == null)
96                                         _extensions = new ExtensionCollection<InstanceContext> (this);
97                                 return _extensions;
98                         }
99                 }
100
101                 public ServiceHostBase Host {
102                         get { return host; }
103                 }
104
105                 public ICollection<IChannel> IncomingChannels {
106                         get { throw new NotImplementedException (); }
107                 }
108
109                 public int ManualFlowControlLimit {
110                         get { return manual_flow_limit; }
111                         set { manual_flow_limit = value; }
112                 }
113
114                 public ICollection<IChannel> OutgoingChannels {
115                         get { throw new NotImplementedException (); }
116                 }
117
118                 public object GetServiceInstance ()
119                 {
120                         return GetServiceInstance (null);
121                 }
122
123                 public object GetServiceInstance (Message message)
124                 {
125                         if (implementation == null && instance_manager != null) {
126                                 implementation = instance_manager.GetServiceInstance (this, message, ref is_user_instance_provider);                            
127                         }
128                         return implementation;                          
129                 }
130
131                 public int IncrementManualFlowControlLimit (int incrementBy)
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 internal void CloseIfIdle () {
137                         if (instance_manager.InstanceContextProvider != null && !IsUserProvidedContext) {
138                                 if (!instance_manager.InstanceContextProvider.IsIdle (this)) {
139                                         instance_manager.InstanceContextProvider.NotifyIdle (IdleCallback, this);
140                                 }
141                                 else {
142                                         if (State != CommunicationState.Closed)
143                                                 Close ();
144                                 }
145                         }
146                 }
147
148                 static void NotifyIdle (InstanceContext ctx) {
149                         ctx.CloseIfIdle ();
150                 }               
151
152                 internal InstanceContextIdleCallback IdleCallback {
153                         get {
154                                 return idle_callback;
155                         }
156                 }
157
158                 public void ReleaseServiceInstance ()
159                 {
160                         instance_manager.ReleaseServiceInstance (this, implementation);
161                         // This does NOT dispose the instance implementation. See DispatrchRuntimeTest.TestInstanceBehavior2 (which never reports "Dispose").
162                         implementation = null;
163                 }
164
165                 void DisposeInstance ()
166                 {
167                         var disp = implementation as IDisposable;
168                         if (disp != null)
169                                 disp.Dispose ();
170                         implementation = null;
171                 }
172
173                 protected override void OnAbort ()
174                 {
175                         DisposeInstance ();
176                 }
177
178                 protected override void OnFaulted ()
179                 {
180                         DisposeInstance ();
181                         base.OnFaulted ();
182                 }
183
184                 protected override void OnClosed ()
185                 {
186                         DisposeInstance ();
187                         base.OnClosed ();
188                 }
189
190                 [MonoTODO]
191                 protected override void OnOpened ()
192                 {
193                         base.OnOpened ();
194                 }
195
196                 protected override void OnOpening ()
197                 {
198                         base.OnOpening ();
199                         if (instance_manager != null)
200                                 instance_manager.Initialize (this);
201                 }
202
203                 Action<TimeSpan> open_delegate, close_delegate;
204
205                 protected override IAsyncResult OnBeginOpen (
206                         TimeSpan timeout, AsyncCallback callback, object state)
207                 {
208                         if (open_delegate == null)
209                                 open_delegate = new Action<TimeSpan> (OnOpen);
210                         return open_delegate.BeginInvoke (timeout, callback, state);
211                 }
212
213                 protected override void OnEndOpen (IAsyncResult result)
214                 {
215                         open_delegate.EndInvoke (result);
216                 }
217
218                 protected override void OnOpen (TimeSpan timeout)
219                 {
220                 }
221
222                 protected override IAsyncResult OnBeginClose (
223                         TimeSpan timeout, AsyncCallback callback, object state)
224                 {
225                         if (close_delegate == null)
226                                 close_delegate = new Action<TimeSpan> (OnClose);
227                         return close_delegate.BeginInvoke (timeout, callback, state);
228                 }
229
230                 protected override void OnEndClose (IAsyncResult result)
231                 {
232                         close_delegate.EndInvoke (result);
233                 }
234
235                 protected override void OnClose (TimeSpan timeout)
236                 {
237                 }
238         }
239 }