2007-09-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodResponse.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodResponse.cs
3 //
4 // Author:      Duncan Mak (duncan@ximian.com)
5 //              Patrik Torstensson
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Remoting;
37 using System.Runtime.Serialization;
38
39 namespace System.Runtime.Remoting.Messaging {
40
41         [Serializable] [CLSCompliant (false)]
42 #if NET_2_0
43         [System.Runtime.InteropServices.ComVisible (true)]
44 #endif
45         public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
46         {
47                 string _methodName;
48                 string _uri;
49                 string _typeName;
50                 MethodBase _methodBase;
51
52                 object _returnValue;
53                 Exception _exception;
54                 Type [] _methodSignature;
55                 ArgInfo _inArgInfo;
56
57                 object []  _args;
58                 object []  _outArgs;
59                 IMethodCallMessage _callMsg;
60                 LogicalCallContext _callContext;
61                 Identity _targetIdentity;
62
63                 protected IDictionary ExternalProperties;
64                 protected IDictionary InternalProperties;
65
66                 public MethodResponse (Header[] headers, IMethodCallMessage mcm)
67                 {
68                         if (mcm != null)
69                         {
70                                 _methodName = mcm.MethodName;
71                                 _uri = mcm.Uri;
72                                 _typeName = mcm.TypeName;
73                                 _methodBase = mcm.MethodBase;
74                                 _methodSignature = (Type[]) mcm.MethodSignature;
75                                 _args = mcm.Args;
76                         }
77
78                         if (headers != null)
79                         {
80                                 foreach (Header header in headers)
81                                         InitMethodProperty (header.Name, header.Value);
82                         }
83                 }
84
85                 internal MethodResponse (Exception e, IMethodCallMessage msg) {
86                         _callMsg = msg;
87
88                         if (null != msg)
89                                 _uri = msg.Uri;
90                         else
91                                 _uri = String.Empty;
92                         
93                         _exception = e;
94                         _returnValue = null;
95                         _outArgs = new object[0];       // .NET does this
96                 }
97
98                 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
99                         _callMsg = msg;
100
101                         _uri = msg.Uri;
102                         
103                         _exception = null;
104                         _returnValue = returnValue;
105                         _args = outArgs;
106                 }
107
108                 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
109                         _callMsg = msg;
110
111                         _methodBase = msg.MethodBase;
112                         //_typeName = msg.TypeName;
113                         _uri = msg.Uri;
114                         _methodName = msg.MethodName;
115                         
116                         // Get unmarshalled arguments
117                         ArrayList args = retmsg.GetArguments ();
118
119                         _exception = retmsg.GetException (args);
120                         _returnValue = retmsg.GetReturnValue (args);
121                         _args = retmsg.GetArgs (args);
122
123                         _callContext = retmsg.GetLogicalCallContext (args);
124                         if (_callContext == null) _callContext = new LogicalCallContext ();
125                         
126                         if (retmsg.PropertiesCount > 0)
127                                 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
128                 }
129
130                 internal MethodResponse (SerializationInfo info, StreamingContext context) 
131                 {
132                         foreach (SerializationEntry entry in info)
133                                 InitMethodProperty (entry.Name, entry.Value);
134                 }
135
136                 internal void InitMethodProperty (string key, object value) 
137                 {
138                         switch (key) 
139                         {
140                                 case "__TypeName": _typeName = (string) value; break;
141                                 case "__MethodName": _methodName = (string) value; break;
142                                 case "__MethodSignature": _methodSignature = (Type[]) value; break;
143                                 case "__Uri": _uri = (string) value; break;
144                                 case "__Return": _returnValue = value; break;
145                                 case "__OutArgs": _args = (object[]) value; break;
146                                 case "__fault": _exception = (Exception) value; break;
147                                 case "__CallContext": _callContext = (LogicalCallContext) value; break;
148                                 default: Properties [key] = value; break;
149                         }
150                 }
151
152                 public int ArgCount {
153                         get { 
154                                 if (null == _args)
155                                         return 0;
156
157                                 return _args.Length;
158                         }
159                 }
160
161                 public object[] Args {
162                         get { 
163                                 return _args; 
164                         }
165                 }
166
167                 public Exception Exception {
168                         get { 
169                                 return _exception; 
170                         }
171                 }
172                 
173                 public bool HasVarArgs {
174                         get { 
175                                 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
176                         }
177                 }
178                 
179                 public LogicalCallContext LogicalCallContext {
180                         get {
181                                 if (_callContext == null)
182                                         _callContext = new LogicalCallContext ();
183                                 return _callContext;
184                         }
185                 }
186                 
187                 public MethodBase MethodBase {
188                         get { 
189                                 if (null == _methodBase) {
190                                         if (_callMsg != null)
191                                                 _methodBase = _callMsg.MethodBase;
192                                         else if (MethodName != null && TypeName != null)
193                                                 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
194                                 }
195                                 return _methodBase;
196                         }
197                 }
198
199                 public string MethodName {
200                         get { 
201                                 if (null == _methodName && null != _callMsg)
202                                         _methodName = _callMsg.MethodName;
203
204                                 return _methodName;
205                         }
206                 }
207
208                 public object MethodSignature {
209                         get { 
210                                 if (null == _methodSignature && null != _callMsg)
211                                         _methodSignature = (Type []) _callMsg.MethodSignature;
212
213                                 return _methodSignature;
214                         }
215                 }
216
217                 public int OutArgCount {
218                         get { 
219                                 if (_args == null || _args.Length == 0) return 0;
220                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
221                                 return _inArgInfo.GetInOutArgCount ();
222                         }
223                 }
224
225                 public object[] OutArgs {
226                         get { 
227                                 if (_outArgs == null && _args != null) {
228                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
229                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
230                                 }                                       
231                                 return _outArgs;
232                         }
233                 }
234
235                 public virtual IDictionary Properties {
236                         get { 
237                                 if (null == ExternalProperties) {
238                                         MethodReturnDictionary properties = new MethodReturnDictionary (this);
239                                         ExternalProperties = properties;
240                                         InternalProperties = properties.GetInternalProperties();
241                                 }
242                                 
243                                 return ExternalProperties;
244                         }
245                 }
246
247                 public object ReturnValue {
248                         get { 
249                                 return _returnValue;
250                         }
251                 }
252
253                 public string TypeName {
254                         get { 
255                                 if (null == _typeName && null != _callMsg)
256                                         _typeName = _callMsg.TypeName;
257
258                                 return _typeName;
259                         }
260                 }
261
262                 public string Uri {
263                         get { 
264                                 if (null == _uri && null != _callMsg)
265                                         _uri = _callMsg.Uri;
266                                 
267                                 return _uri;
268                         }
269
270                         set { 
271                                 _uri = value;
272                         }
273                 }
274
275                 string IInternalMessage.Uri {
276                         get { return Uri; }
277                         set { Uri = value; }
278                 }
279
280                 public object GetArg (int argNum)
281                 {
282                         if (null == _args)
283                                 return null;
284
285                         return _args [argNum];
286                 }
287
288                 public string GetArgName (int index)
289                 {
290                         return MethodBase.GetParameters()[index].Name;
291                 }
292
293                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
294                 {
295                         if (_exception == null)
296                         {
297                                 info.AddValue ("__TypeName", _typeName);
298                                 info.AddValue ("__MethodName", _methodName);
299                                 info.AddValue ("__MethodSignature", _methodSignature);
300                                 info.AddValue ("__Uri", _uri);
301                                 info.AddValue ("__Return", _returnValue);
302                                 info.AddValue ("__OutArgs", _args);
303                         }
304                         else
305                                 info.AddValue ("__fault", _exception);
306
307                         info.AddValue ("__CallContext", _callContext);
308
309                         if (InternalProperties != null) {
310                                 foreach (DictionaryEntry entry in InternalProperties)
311                                         info.AddValue ((string) entry.Key, entry.Value);
312                         }
313                 } 
314
315                 public object GetOutArg (int argNum)
316                 {
317                         if (_args == null) return null;
318                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
319                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
320                 }
321
322                 public string GetOutArgName (int index)
323                 {
324                         if (null == _methodBase)
325                                 return "__method_" + index;
326
327                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
328                         return _inArgInfo.GetInOutArgName(index);
329                 }
330
331                 [MonoTODO]
332                 public virtual object HeaderHandler (Header[] h)
333                 {
334                         throw new NotImplementedException ();
335                 }
336
337                 [MonoTODO]
338                 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
339                 {
340                         throw new NotImplementedException ();
341                 } 
342
343                 Identity IInternalMessage.TargetIdentity
344                 {
345                         get { return _targetIdentity; }
346                         set { _targetIdentity = value; }
347                 }
348
349 #if !NET_2_0
350                 public override string ToString ()
351                 {
352                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
353                         if (_exception != null)
354                         {
355                                 s += "Exception\n)" + _exception;
356                         }
357                         else
358                         {
359                                 if (_args != null)
360                                 {
361                                         for (int n=0; n<_args.Length; n++)
362                                         {
363                                                 if (n>0) s+= ", ";
364                                                 if (_args[n] != null) s += _args[n].GetType().Name + " ";
365                                                 s += GetOutArgName (n);
366                                                 if (_args[n] != null) s += " = {" + _args[n] + "}";
367                                                 else s+=" = {null}";
368                                         }
369                                 }
370                                 s += ")";
371                         }
372                         return s;
373                 }
374 #endif
375         }
376 }