2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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         [MonoTODO]
36         public sealed class InstanceContext : CommunicationObject,
37                 IExtensibleObject<InstanceContext>
38         {
39                 ServiceHostBase host;
40                 object implementation;
41                 int manual_flow_limit;
42                 InstanceBehavior _behavior;
43                 bool is_user_instance_provider;
44                 bool is_user_context_provider;
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,
59                         object implementation) : this(host, implementation, true)
60                 {}
61
62                 internal InstanceContext(ServiceHostBase host, 
63                         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 {
78                                 return is_user_context_provider;                                
79                         }
80                 }
81
82                 internal InstanceBehavior Behavior {
83                         get {
84                                 return _behavior;
85                         }
86                         set {
87                                 _behavior = value;
88                         }
89                 }
90
91                 protected internal override TimeSpan DefaultCloseTimeout {
92                         get { return host.DefaultCloseTimeout; }
93                 }
94
95                 protected internal override TimeSpan DefaultOpenTimeout {
96                         get { return host.DefaultOpenTimeout; }
97                 }
98
99                 public IExtensionCollection<InstanceContext> Extensions {
100                         get { throw new NotImplementedException (); }
101                 }
102
103                 public ServiceHostBase Host {
104                         get { return host; }
105                 }
106
107                 public ICollection<IChannel> IncomingChannels {
108                         get { throw new NotImplementedException (); }
109                 }
110
111                 public int ManualFlowControlLimit {
112                         get { return manual_flow_limit; }
113                         set { manual_flow_limit = value; }
114                 }
115
116                 public ICollection<IChannel> OutgoingChannels {
117                         get { throw new NotImplementedException (); }
118                 }
119
120                 public object GetServiceInstance ()
121                 {
122                         return GetServiceInstance (null);
123                 }
124
125                 public object GetServiceInstance (Message message)
126                 {
127                         if (implementation == null && Behavior != null) {
128                                 implementation = Behavior.GetServiceInstance (this, message, ref is_user_instance_provider);                            
129                         }
130                         return implementation;                          
131                 }
132
133                 public int IncrementManualFlowControlLimit (int incrementBy)
134                 {
135                         throw new NotImplementedException ();
136                 }
137
138                 internal void CloseIfIdle () {
139                         if (Behavior.InstanceContextProvider != null && !IsUserProvidedContext) {
140                                 if (!Behavior.InstanceContextProvider.IsIdle (this)) {
141                                         Behavior.InstanceContextProvider.NotifyIdle (IdleCallback, this);
142                                 }
143                                 else {
144                                         if (State != CommunicationState.Closed)
145                                                 Close ();
146                                 }
147                         }
148                 }
149
150                 static void NotifyIdle (InstanceContext ctx) {
151                         ctx.CloseIfIdle ();
152                 }               
153
154                 internal InstanceContextIdleCallback IdleCallback {
155                         get {
156                                 return idle_callback;
157                         }
158                 }
159
160                 public void ReleaseServiceInstance ()
161                 {                       
162                         Behavior.ReleaseServiceInstance (this, implementation);
163                         implementation = null;
164                 }
165
166                 protected override void OnAbort ()
167                 {
168                 }
169
170                 [MonoTODO]
171                 protected override void OnFaulted ()
172                 {
173                 }
174
175                 protected override void OnClosed ()
176                 {
177                         base.OnClosed ();
178                 }
179
180                 protected override void OnOpened ()
181                 {
182                 }
183
184                 protected override void OnOpening () {
185                         if (Behavior != null)
186                                 Behavior.Initialize (this);
187                         base.OnOpening ();
188                 }
189
190                 protected override IAsyncResult OnBeginOpen (
191                         TimeSpan timeout, AsyncCallback callback, object state)
192                 {
193                         throw new NotImplementedException ();
194                 }
195
196                 protected override void OnEndOpen (IAsyncResult result)
197                 {
198                 }
199
200                 protected override void OnOpen (TimeSpan timeout)
201                 {
202                 }
203
204                 protected override IAsyncResult OnBeginClose (
205                         TimeSpan timeout, AsyncCallback callback, object state)
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 protected override void OnEndClose (IAsyncResult result)
211                 {
212                 }
213
214                 protected override void OnClose (TimeSpan timeout)
215                 {
216                 }               
217         }
218 }