New test.
[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         public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
43         {
44                 string _methodName;
45                 string _uri;
46                 string _typeName;
47                 MethodBase _methodBase;
48
49                 object _returnValue;
50                 Exception _exception;
51                 Type [] _methodSignature;
52                 ArgInfo _inArgInfo;
53
54                 object []  _args;
55                 object []  _outArgs;
56                 IMethodCallMessage _callMsg;
57                 LogicalCallContext _callContext;
58                 Identity _targetIdentity;
59
60                 protected IDictionary ExternalProperties;
61                 protected IDictionary InternalProperties;
62
63                 public MethodResponse (Header[] headers, IMethodCallMessage mcm)
64                 {
65                         if (mcm != null)
66                         {
67                                 _methodName = mcm.MethodName;
68                                 _uri = mcm.Uri;
69                                 _typeName = mcm.TypeName;
70                                 _methodBase = mcm.MethodBase;
71                                 _methodSignature = (Type[]) mcm.MethodSignature;
72                                 _args = mcm.Args;
73                         }
74
75                         if (headers != null)
76                         {
77                                 foreach (Header header in headers)
78                                         InitMethodProperty (header.Name, header.Value);
79                         }
80                 }
81
82                 internal MethodResponse (Exception e, IMethodCallMessage msg) {
83                         _callMsg = msg;
84
85                         if (null != msg)
86                                 _uri = msg.Uri;
87                         else
88                                 _uri = String.Empty;
89                         
90                         _exception = e;
91                         _returnValue = null;
92                         _outArgs = new object[0];       // .NET does this
93                 }
94
95                 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
96                         _callMsg = msg;
97
98                         _uri = msg.Uri;
99                         
100                         _exception = null;
101                         _returnValue = returnValue;
102                         _args = outArgs;
103                 }
104
105                 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
106                         _callMsg = msg;
107
108                         _methodBase = msg.MethodBase;
109                         //_typeName = msg.TypeName;
110                         _uri = msg.Uri;
111                         _methodName = msg.MethodName;
112                         
113                         // Get unmarshalled arguments
114                         ArrayList args = retmsg.GetArguments ();
115
116                         _exception = retmsg.GetException (args);
117                         _returnValue = retmsg.GetReturnValue (args);
118                         _args = retmsg.GetArgs (args);
119
120                         _callContext = retmsg.GetLogicalCallContext (args);
121                         if (_callContext == null) _callContext = new LogicalCallContext ();
122                         
123                         if (retmsg.PropertiesCount > 0)
124                                 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
125                 }
126
127                 internal MethodResponse (SerializationInfo info, StreamingContext context) 
128                 {
129                         foreach (SerializationEntry entry in info)
130                                 InitMethodProperty (entry.Name, entry.Value);
131                 }
132
133                 internal void InitMethodProperty (string key, object value) 
134                 {
135                         switch (key) 
136                         {
137                                 case "__TypeName": _typeName = (string) value; break;
138                                 case "__MethodName": _methodName = (string) value; break;
139                                 case "__MethodSignature": _methodSignature = (Type[]) value; break;
140                                 case "__Uri": _uri = (string) value; break;
141                                 case "__Return": _returnValue = value; break;
142                                 case "__OutArgs": _args = (object[]) value; break;
143                                 case "__fault": _exception = (Exception) value; break;
144                                 case "__CallContext": _callContext = (LogicalCallContext) value; break;
145                                 default: Properties [key] = value; break;
146                         }
147                 }
148
149                 public int ArgCount {
150                         get { 
151                                 if (null == _args)
152                                         return 0;
153
154                                 return _args.Length;
155                         }
156                 }
157
158                 public object[] Args {
159                         get { 
160                                 return _args; 
161                         }
162                 }
163
164                 public Exception Exception {
165                         get { 
166                                 return _exception; 
167                         }
168                 }
169                 
170                 public bool HasVarArgs {
171                         get { 
172                                 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
173                         }
174                 }
175                 
176                 public LogicalCallContext LogicalCallContext {
177                         get {
178                                 if (_callContext == null)
179                                         _callContext = new LogicalCallContext ();
180                                 return _callContext;
181                         }
182                 }
183                 
184                 public MethodBase MethodBase {
185                         get { 
186                                 if (null == _methodBase) {
187                                         if (_callMsg != null)
188                                                 _methodBase = _callMsg.MethodBase;
189                                         else if (MethodName != null && TypeName != null)
190                                                 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
191                                 }
192                                 return _methodBase;
193                         }
194                 }
195
196                 public string MethodName {
197                         get { 
198                                 if (null == _methodName && null != _callMsg)
199                                         _methodName = _callMsg.MethodName;
200
201                                 return _methodName;
202                         }
203                 }
204
205                 public object MethodSignature {
206                         get { 
207                                 if (null == _methodSignature && null != _callMsg)
208                                         _methodSignature = (Type []) _callMsg.MethodSignature;
209
210                                 return _methodSignature;
211                         }
212                 }
213
214                 public int OutArgCount {
215                         get { 
216                                 if (_args == null || _args.Length == 0) return 0;
217                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
218                                 return _inArgInfo.GetInOutArgCount ();
219                         }
220                 }
221
222                 public object[] OutArgs {
223                         get { 
224                                 if (_outArgs == null && _args != null) {
225                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
226                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
227                                 }                                       
228                                 return _outArgs;
229                         }
230                 }
231
232                 public virtual IDictionary Properties {
233                         get { 
234                                 if (null == ExternalProperties) {
235                                         MethodReturnDictionary properties = new MethodReturnDictionary (this);
236                                         ExternalProperties = properties;
237                                         InternalProperties = properties.GetInternalProperties();
238                                 }
239                                 
240                                 return ExternalProperties;
241                         }
242                 }
243
244                 public object ReturnValue {
245                         get { 
246                                 return _returnValue;
247                         }
248                 }
249
250                 public string TypeName {
251                         get { 
252                                 if (null == _typeName && null != _callMsg)
253                                         _typeName = _callMsg.TypeName;
254
255                                 return _typeName;
256                         }
257                 }
258
259                 public string Uri {
260                         get { 
261                                 if (null == _uri && null != _callMsg)
262                                         _uri = _callMsg.Uri;
263                                 
264                                 return _uri;
265                         }
266
267                         set { 
268                                 _uri = value;
269                         }
270                 }
271
272                 public object GetArg (int argNum)
273                 {
274                         if (null == _args)
275                                 return null;
276
277                         return _args [argNum];
278                 }
279
280                 public string GetArgName (int index)
281                 {
282                         return MethodBase.GetParameters()[index].Name;
283                 }
284
285                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
286                 {
287                         if (_exception == null)
288                         {
289                                 info.AddValue ("__TypeName", _typeName);
290                                 info.AddValue ("__MethodName", _methodName);
291                                 info.AddValue ("__MethodSignature", _methodSignature);
292                                 info.AddValue ("__Uri", _uri);
293                                 info.AddValue ("__Return", _returnValue);
294                                 info.AddValue ("__OutArgs", _args);
295                         }
296                         else
297                                 info.AddValue ("__fault", _exception);
298
299                         info.AddValue ("__CallContext", _callContext);
300
301                         if (InternalProperties != null) {
302                                 foreach (DictionaryEntry entry in InternalProperties)
303                                         info.AddValue ((string) entry.Key, entry.Value);
304                         }
305                 } 
306
307                 public object GetOutArg (int argNum)
308                 {
309                         if (_args == null) return null;
310                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
311                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
312                 }
313
314                 public string GetOutArgName (int index)
315                 {
316                         if (null == _methodBase)
317                                 return "__method_" + index;
318
319                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
320                         return _inArgInfo.GetInOutArgName(index);
321                 }
322
323                 [MonoTODO]
324                 public virtual object HeaderHandler (Header[] h)
325                 {
326                         throw new NotImplementedException ();
327                 }
328
329                 [MonoTODO]
330                 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
331                 {
332                         throw new NotImplementedException ();
333                 } 
334
335                 Identity IInternalMessage.TargetIdentity
336                 {
337                         get { return _targetIdentity; }
338                         set { _targetIdentity = value; }
339                 }
340
341                 public override string ToString ()
342                 {
343                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
344                         if (_exception != null)
345                         {
346                                 s += "Exception\n)" + _exception;
347                         }
348                         else
349                         {
350                                 if (_args != null)
351                                 {
352                                         for (int n=0; n<_args.Length; n++)
353                                         {
354                                                 if (n>0) s+= ", ";
355                                                 if (_args[n] != null) s += _args[n].GetType().Name + " ";
356                                                 s += GetOutArgName (n);
357                                                 if (_args[n] != null) s += " = {" + _args[n] + "}";
358                                                 else s+=" = {null}";
359                                         }
360                                 }
361                                 s += ")";
362                         }
363                         return s;
364                 }
365
366         }
367 }