New test.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodCall.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodCall.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
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.Serialization;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40         [Serializable] [CLSCompliant (false)]
41         public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
42         {
43                 string _uri;
44                 string _typeName;
45                 string _methodName;
46                 object[] _args;
47                 Type[] _methodSignature;
48                 MethodBase _methodBase;
49                 LogicalCallContext _callContext;
50                 ArgInfo _inArgInfo;
51                 Identity _targetIdentity;
52
53                 protected IDictionary ExternalProperties;
54                 protected IDictionary InternalProperties;
55
56                 public MethodCall (Header [] headers)
57                 {
58                         Init();
59
60                         if (headers == null || headers.Length == 0) return;
61
62                         foreach (Header header in headers)
63                                 InitMethodProperty (header.Name, header.Value);
64
65                         ResolveMethod ();
66                 }
67
68                 internal MethodCall (SerializationInfo info, StreamingContext context)
69                 {
70                         Init();
71
72                         foreach (SerializationEntry entry in info)
73                                 InitMethodProperty ((string)entry.Name, entry.Value);
74                 }
75
76                 internal MethodCall (CADMethodCallMessage msg) 
77                 {
78                         _uri = string.Copy (msg.Uri);
79                         
80                         // Get unmarshalled arguments
81                         ArrayList args = msg.GetArguments ();
82
83                         _args = msg.GetArgs (args);
84                         _callContext = msg.GetLogicalCallContext (args);
85                         if (_callContext == null)
86                                 _callContext = new LogicalCallContext ();
87         
88                         _methodBase = msg.GetMethod ();
89                         
90                         Init();
91
92                         if (msg.PropertiesCount > 0)
93                                 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
94                 }
95
96                 public MethodCall (IMessage msg)
97                 {
98                         if (msg is IMethodMessage)
99                                 CopyFrom ((IMethodMessage) msg);
100                         else
101                         {
102                                 foreach (DictionaryEntry entry in msg.Properties)
103                                         InitMethodProperty ((String) entry.Key, entry.Value);
104                                 Init();
105                 }
106                 }
107
108                 internal MethodCall (string uri, string typeName, string methodName, object[] args)
109                 {
110                         _uri = uri;
111                         _typeName = typeName;
112                         _methodName = methodName;
113                         _args = args;
114
115                         Init();
116                         ResolveMethod();
117                 }
118
119                 internal MethodCall ()
120                 {
121                 }
122                 
123                 internal void CopyFrom (IMethodMessage call)
124                 {
125                         _uri = call.Uri;
126                         _typeName = call.TypeName;
127                         _methodName = call.MethodName;
128                         _args = call.Args;
129                         _methodSignature = (Type[]) call.MethodSignature;
130                         _methodBase = call.MethodBase;
131                         _callContext = call.LogicalCallContext;
132
133                         Init();
134                 }
135                 
136                 internal virtual void InitMethodProperty(string key, object value)
137                 {
138                         switch (key)
139                         {
140                                 case "__TypeName" : _typeName = (string) value; return;
141                                 case "__MethodName" : _methodName = (string) value; return;
142                                 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
143                                 case "__Args" : _args = (object[]) value; return;
144                                 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
145                                 case "__Uri" : _uri = (string) value; return;
146                                 default: Properties[key] = value; return;
147                         }
148                 }
149
150                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
151                 {
152                         info.AddValue ("__TypeName", _typeName);
153                         info.AddValue ("__MethodName", _methodName);
154                         info.AddValue ("__MethodSignature", _methodSignature);
155                         info.AddValue ("__Args", _args);
156                         info.AddValue ("__CallContext", _callContext);
157                         info.AddValue ("__Uri", _uri);
158
159                         if (InternalProperties != null) {
160                                 foreach (DictionaryEntry entry in InternalProperties)
161                                         info.AddValue ((string) entry.Key, entry.Value);
162                         }
163                 } 
164
165                 public int ArgCount {
166                         get { return _args.Length; }
167                 }
168
169                 public object[] Args {
170                         get { return _args; }
171                 }
172                 
173                 public bool HasVarArgs {
174                         get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
175                 }
176
177                 public int InArgCount 
178                 {
179                         get 
180                         { 
181                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
182                                 return _inArgInfo.GetInOutArgCount();
183                         }
184                 }
185
186                 public object[] InArgs 
187                 {
188                         get 
189                         { 
190                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
191                                 return _inArgInfo.GetInOutArgs (_args);
192                         }
193                 }
194                 
195                 public LogicalCallContext LogicalCallContext {
196                         get {
197                                 if (_callContext == null)
198                                         _callContext = new LogicalCallContext ();
199                                 return _callContext;
200                         }
201                 }
202                 
203                 public MethodBase MethodBase {
204                         get {
205                                 if (_methodBase == null)
206                                         ResolveMethod ();
207                                         
208                                 return _methodBase;
209                         }
210                 }
211
212                 public string MethodName {
213                         get {
214                                 // lazily fill in _methodName from _methodBase
215                                 if (_methodName == null)
216                                         _methodName = _methodBase.Name;
217                                 return _methodName;
218                         }
219                 }
220
221                 public object MethodSignature {
222                         get { 
223                                 if (_methodSignature == null && _methodBase != null)
224                                 {
225                                         ParameterInfo[] parameters = _methodBase.GetParameters();
226                                         _methodSignature = new Type[parameters.Length];
227                                         for (int n=0; n<parameters.Length; n++)
228                                                 _methodSignature[n] = parameters[n].ParameterType;
229                                 }
230                                 return _methodSignature;
231                         }
232                 }
233
234                 public virtual IDictionary Properties {
235                         get 
236                         { 
237                                 if (ExternalProperties == null) InitDictionary ();
238                                 return ExternalProperties; 
239                         }
240                 }
241
242                 internal virtual void InitDictionary()
243                 {
244                         MethodCallDictionary props = new MethodCallDictionary (this);
245                         ExternalProperties = props;
246                         InternalProperties = props.GetInternalProperties();
247                 }
248
249                 public string TypeName 
250                 {
251                         get {
252                                 // lazily fill in _typeName from _methodBase
253                                 if (_typeName == null)
254                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
255                                 return _typeName;
256                         }
257                 }
258
259                 public string Uri {
260                         get { return _uri; }
261                         set { _uri = value; }
262                 }
263
264                 public object GetArg (int argNum)
265                 {
266                         return _args[argNum];
267                 }
268
269                 public string GetArgName (int index)
270                 {
271                         return _methodBase.GetParameters()[index].Name;
272                 }
273
274                 public object GetInArg (int argNum)
275                 {
276                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
277                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
278                 }
279
280                 public string GetInArgName (int index)
281                 {
282                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
283                         return _inArgInfo.GetInOutArgName(index);
284                 }
285
286                 [MonoTODO]
287                 public virtual object HeaderHandler (Header[] h)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 public virtual void Init ()
293                 {
294                 }
295
296                 public void ResolveMethod ()
297                 {
298                         if (_uri != null)
299                         {
300                                 Type type = RemotingServices.GetServerTypeForUri (_uri);
301                                 if (type == null) {
302                                         string sname = _typeName != null ? " (" + _typeName + ")" : "";
303                                         throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
304                                 }
305
306                                 if (CastTo (_typeName, type) != null) {
307                                         _methodBase = RemotingServices.GetMethodBaseFromName (type, _methodName, _methodSignature);
308                                         if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + type);
309                                         return;
310                                 }
311                                 else
312                                         throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
313                         }
314                         _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
315                         if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
316                 }
317
318                 Type CastTo (string clientType, Type serverType)
319                 {
320                         int i = clientType.IndexOf(',');
321                         if (i != -1) clientType = clientType.Substring (0,i).Trim();
322
323                         if (clientType == serverType.FullName) return serverType;
324
325                         // base class hierarchy
326
327                         Type baseType = serverType.BaseType;
328                         while (baseType != null) {
329                         if (clientType == baseType.FullName) return baseType;
330                         baseType = baseType.BaseType;
331                 }
332
333                         // Implemented interfaces
334
335                         Type[] interfaces = serverType.GetInterfaces();
336                         foreach (Type itype in interfaces)
337                                 if (clientType == itype.FullName) return itype;
338      
339                 return null;
340                 }
341                 
342                 [MonoTODO]
343                 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
344                 {
345                         throw new NotImplementedException ();
346                 }
347
348                 Identity IInternalMessage.TargetIdentity
349                 {
350                         get { return _targetIdentity; }
351                         set { _targetIdentity = value; }
352                 }
353
354                 public override string ToString ()
355                 {
356                         string s = _typeName.Split(',')[0] + "." + _methodName + " (";
357                         if (_args != null)
358                         {
359                                 for (int n=0; n<_args.Length; n++)
360                                 {
361                                         if (n>0) s+= ", ";
362                                         if (_args[n] != null) s += _args[n].GetType().Name + " ";
363                                         s += GetArgName (n);
364                                         if (_args[n] != null) s += " = {" + _args[n] + "}";
365                                         else s+=" = {null}";
366                                 }
367                         }
368                         s += ")";
369                         return s;
370                 }
371         }
372 }