2006-11-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / ClientContextTerminatorSink.cs
1 //
2 // System.Runtime.Remoting.Messaging.ClientContextTerminatorSink.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2003, Lluis Sanchez Gual
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Runtime.Remoting;
34 using System.Runtime.Remoting.Activation;
35 using System.Runtime.Remoting.Contexts;
36
37 namespace System.Runtime.Remoting.Messaging
38 {
39         internal class ClientContextTerminatorSink: IMessageSink
40         {
41                 Context _context;
42
43                 public ClientContextTerminatorSink(Context ctx)
44                 {
45                         _context = ctx;
46                 }
47
48                 public IMessage SyncProcessMessage (IMessage msg)
49                 {
50                         IMessage res;
51
52                         Context.NotifyGlobalDynamicSinks (true, msg, true, false);
53                         _context.NotifyDynamicSinks (true, msg, true, false);
54
55                         if (msg is IConstructionCallMessage)
56                         {
57                                 res = ActivationServices.RemoteActivate ((IConstructionCallMessage)msg);
58                         }
59                         else
60                         {
61                                 Identity identity = RemotingServices.GetMessageTargetIdentity (msg);
62                                 res = identity.ChannelSink.SyncProcessMessage (msg);
63                         }
64
65                         Context.NotifyGlobalDynamicSinks (false, msg, true, false);
66                         _context.NotifyDynamicSinks (false, msg, true, false);
67
68                         return res;
69                 }
70
71                 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
72                 {
73                         if (_context.HasDynamicSinks || Context.HasGlobalDynamicSinks)
74                         {
75                                 Context.NotifyGlobalDynamicSinks (true, msg, true, true);
76                                 _context.NotifyDynamicSinks (true, msg, true, true);
77
78                                 // replySink is null when calling a one way method
79                                 if (replySink != null) replySink = new ClientContextReplySink (_context, replySink);
80                         }
81                         Identity identity = RemotingServices.GetMessageTargetIdentity (msg);
82                         IMessageCtrl res = identity.ChannelSink.AsyncProcessMessage (msg, replySink);
83
84                         if (replySink == null && (_context.HasDynamicSinks || Context.HasGlobalDynamicSinks))
85                         {
86                                 Context.NotifyGlobalDynamicSinks (false, msg, true, true);
87                                 _context.NotifyDynamicSinks (false, msg, true, true);
88                         }
89                         return res;
90                 }
91
92                 public IMessageSink NextSink 
93                 { 
94                         get { return null; }
95                 }       
96         }
97
98         class ClientContextReplySink: IMessageSink
99         {
100                 IMessageSink _replySink;
101                 Context _context;
102
103                 public ClientContextReplySink (Context ctx, IMessageSink replySink)
104                 {
105                         _replySink = replySink;
106                         _context = ctx;
107                 }
108
109                 public IMessage SyncProcessMessage (IMessage msg)
110                 {
111                         Context.NotifyGlobalDynamicSinks (false, msg, true, true);
112                         _context.NotifyDynamicSinks (false, msg, true, true);
113                         return _replySink.SyncProcessMessage (msg);
114                 }
115
116                 public IMessageCtrl AsyncProcessMessage (IMessage msg, IMessageSink replySink)
117                 {
118                         throw new NotSupportedException ();
119                 }       
120
121                 public IMessageSink NextSink 
122                 { 
123                         get { return _replySink; }
124                 }       
125         }
126 }