New test.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / Identity.cs
1 //
2 // System.Runtime.Remoting.Identity.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
5 //
6 // (C) 2002, 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.Runtime.Remoting.Messaging;
33 using System.Runtime.Remoting.Proxies;
34 using System.Runtime.Remoting.Contexts;
35 using System.Runtime.Remoting.Lifetime;
36
37 namespace System.Runtime.Remoting
38 {
39         internal abstract class Identity
40         {
41                 // An Identity object holds remoting information about
42                 // an object. It can be used to store client side information
43                 // (information about how to reach the remote server),
44                 // and also to store server side information (information
45                 // about how to dispatch messages to the object in the server).
46
47                 // URI of the object
48                 protected string _objectUri;
49
50                 // Message sink to use to send a message to the remote server
51                 protected IMessageSink _channelSink = null;
52
53                 protected IMessageSink _envoySink = null;
54
55                 DynamicPropertyCollection _clientDynamicProperties;
56                 DynamicPropertyCollection _serverDynamicProperties;
57
58                 // The ObjRef 
59                 protected ObjRef _objRef;
60                 
61                 // This flag is set when the identity is removed from the uri table.
62                 // It is needed because in some scenarios the runtime may try to
63                 // dispose the identity twice.
64                 bool _disposed = false;
65
66                 public Identity(string objectUri)
67                 {
68                         _objectUri = objectUri;
69                 }
70
71                 public abstract ObjRef CreateObjRef (Type requestedType);
72
73                 public bool IsFromThisAppDomain
74                 {
75                         get
76                         {
77                                 return (_channelSink == null);
78                         }
79                 }
80
81                 public IMessageSink ChannelSink
82                 {
83                         get { return _channelSink; }
84                         set { _channelSink = value; }
85                 }
86
87                 public IMessageSink EnvoySink
88                 {
89                         get { return _envoySink; }
90                 }
91
92                 public string ObjectUri
93                 {
94                         get { return _objectUri; }
95                         set { _objectUri = value; }
96                 }
97
98                 public bool IsConnected
99                 {
100                         get { return _objectUri != null; }
101                 }
102                 
103                 public bool Disposed
104                 {
105                         get { return _disposed; }
106                         set { _disposed = true; }
107                 }
108
109                 public DynamicPropertyCollection ClientDynamicProperties
110                 {
111                         get { 
112                                 if (_clientDynamicProperties == null) _clientDynamicProperties = new DynamicPropertyCollection();
113                                 return _clientDynamicProperties; 
114                         }
115                 }
116
117                 public DynamicPropertyCollection ServerDynamicProperties
118                 {
119                         get { 
120                                 if (_serverDynamicProperties == null) _serverDynamicProperties = new DynamicPropertyCollection();
121                                 return _serverDynamicProperties; 
122                         }
123                 }
124
125                 public bool HasClientDynamicSinks
126                 {
127                         get { return (_clientDynamicProperties != null && _clientDynamicProperties.HasProperties); }
128                 }
129
130                 public bool HasServerDynamicSinks
131                 {
132                         get { return (_serverDynamicProperties != null && _serverDynamicProperties.HasProperties); }
133                 }
134
135                 public void NotifyClientDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
136                 {
137                         if (_clientDynamicProperties != null && _clientDynamicProperties.HasProperties) 
138                                 _clientDynamicProperties.NotifyMessage (start, req_msg, client_site, async);
139                 }
140
141                 public void NotifyServerDynamicSinks  (bool start, IMessage req_msg, bool client_site, bool async)
142                 {
143                         if (_serverDynamicProperties != null && _serverDynamicProperties.HasProperties) 
144                                 _serverDynamicProperties.NotifyMessage (start, req_msg, client_site, async);
145                 }
146         }
147
148         internal class ClientIdentity : Identity
149         {
150                 WeakReference _proxyReference;
151
152                 public ClientIdentity (string objectUri, ObjRef objRef): base (objectUri)
153                 {
154                         _objRef = objRef;
155                         _envoySink = (_objRef.EnvoyInfo != null) ? _objRef.EnvoyInfo.EnvoySinks : null;
156                 }
157
158                 public MarshalByRefObject ClientProxy
159                 {
160                         get     { return (MarshalByRefObject) _proxyReference.Target; }
161                         set { _proxyReference = new WeakReference (value); }
162                 }
163
164                 public override ObjRef CreateObjRef (Type requestedType)
165                 {
166                         return _objRef;
167                 }
168
169                 public string TargetUri
170                 {
171                         get { return _objRef.URI; }
172                 }
173         }
174 }