summaryrefslogtreecommitdiff
path: root/dotnet/client-010/management/console/QMFObject.cs
blob: 905422efd9efd2787fc2814bfb032e4b71fff370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

using System;
using System.Collections.Generic ;
using log4net ;
using org.apache.qpid.transport.codec ;

namespace org.apache.qpid.console
{
	
	/**
	 * An object which is returned from an agent by the Session. It can have
	 * methods, properties, and statistics.
	 */
	public class QMFObject
	{
	
		public static ILog log = LogManager.GetLogger(typeof(QMFObject)) ;	

 		public Session Session  {get;set;}
 		protected SchemaClass _Schema ;
 		virtual public SchemaClass Schema {get {return _Schema;} set {_Schema=value;}}
 		bool Managed ;
 		public DateTime CurrentTime {get;set;}
		public DateTime CreateTime {get;set;} 		
		public DateTime DeleteTime {get;set;}		
		public ObjectID ObjectID {get;set;}
		public Dictionary<string, object> Properties = new Dictionary<string, object>() ;
		public Dictionary<string, object> Statistics = new Dictionary<string, object>() ;		
			
 		
 		// This constructor is the "naked" constructor which creates
 		// an object without a session or a schema. It is used by
 		// subclasses which are auto generated
 		public QMFObject() {
 		}
 		
 		public QMFObject(QMFObject source) {
 			this.Session = source.Session ;
 			this.Schema = source.Schema ;
 			this.Managed = source.Managed ;
 			this.CurrentTime = source.CurrentTime ;
 			this.CreateTime = source.CreateTime ;
 			this.DeleteTime = source.DeleteTime ;
 			this.ObjectID = source.ObjectID ;
 			this.Properties = source.Properties ;
 			this.Statistics = source.Statistics ;
 		}
 		
 		// This constructor is used by a session make object call to 
 		// create a blank object from a schema.
 		public QMFObject(Session session, SchemaClass schema, bool hasProperties, bool hasStats , bool isManaged) {
			Session = session ;
			Schema = schema ;
			Managed = isManaged ; 
			
			if (hasProperties) {
				foreach (SchemaProperty prop in Schema.GetAllProperties()) {
					object propValue = null ; 
					if (!prop.Optional) {
						propValue = Util.DefaultValue(prop.Type) ;
					}
					this.SetProperty(prop.Name, propValue) ;			
				}
			}	
			
			if (hasStats) {
				foreach (SchemaStatistic stat in Schema.Statistics)  {			
					SetStatistic(stat.Name, Util.DefaultValue(stat.Type)) ;
				}
			}															
 		}
 		
 		// This constructor is used by the session to create an object based on a data 
 		// stream by the agent.
		public QMFObject(Session session, SchemaClass schema, IDecoder dec, bool hasProperties, bool hasStats , bool isManaged)
		{
			Session = session ;
			Schema = schema ;
			Managed = isManaged ;
			
			if (Managed) {
			    // FIXME DateTime or Uint64??
				CurrentTime = new DateTime(dec.ReadDatetime()) ;
				CreateTime = new DateTime(dec.ReadDatetime()) ;				
				DeleteTime = new DateTime(dec.ReadDatetime()) ;				
				ObjectID = new ObjectID(dec) ;
			}
			
			if (hasProperties) {
				List<string> excluded = ProcessPresenceMasks(dec, Schema) ;
				
				foreach (SchemaProperty prop in Schema.GetAllProperties()) {
					if (excluded.Contains(prop.Name)) {
					    log.Debug(String.Format("Setting Property Default {0}", prop.Name)) ;					    					
						safeAddProperty(prop.Name, null) ;	
					} else {
						//log.Debug(String.Format("Setting Property {0}", prop.Name)) ;
						safeAddProperty(prop.Name, session.DecodeValue(dec, prop.Type)) ;
					}
				}
			}
			
			if (hasStats) {
				foreach (SchemaStatistic stat in Schema.GetAllStatistics())  {
					//log.Debug(String.Format("Setting Statistic {0}", stat.Name)) ;				
					Statistics.Add(stat.Name, session.DecodeValue(dec, stat.Type)) ;
				}
			}
			
		}

		protected List<string> ProcessPresenceMasks(IDecoder dec, SchemaClass schema) {
			List<string> excludes = new List<string> () ;
			short bit = 0 ;
			short mask = 0 ;
			foreach (SchemaProperty prop in Schema.GetAllProperties()) {
				if (prop.Optional) {
					//log.Debug(String.Format("Property named {0} is optional", prop.Name)) ;				
					if (bit == 0) {
						mask=dec.ReadUint8() ;
						bit = 1 ;
					}

					if ((mask & bit) == 0) {
						//log.Debug(String.Format("Property named {0} is not present", prop.Name)) ;
						excludes.Add(prop.Name) ;
					}
					bit *= 2 ;
					if (bit == 256) {
						bit = 0 ;
					}
				}
			}
			return excludes ;			
		}
		
		protected List<SchemaMethod> getMethods() {
			return Schema.GetAllMethods() ;
		}
		
		public object GetProperty(string attributeName) {
			//FIXME any object refs?
			object returnValue = null ;
			Properties.TryGetValue(attributeName, out returnValue) ;
			return returnValue ;
		}
		
		protected void SetStatistic(string attributeName, object newValue) {
			Statistics.Remove(attributeName) ;
			Statistics.Add(attributeName, newValue) ;
		}
		
		public void SetProperty(string attributeName, object newValue) {
			Properties.Remove(attributeName) ;
			Properties.Add(attributeName, newValue) ;
		}		
		
		public bool IsDeleted() {
			return !DeleteTime.Equals(new DateTime(0)) ;
		}
		
		public string RoutingKey() {
			return ObjectID.RoutingCode() ;
		}
		
		public long AgentBank() {
			return ObjectID.AgentBank() ;
		}
		
		public long BrokerBank() {
			return ObjectID.BrokerBank() ;
		}
				
		override public string ToString() {
			string propertyString = "" ;
			foreach (KeyValuePair<string, object> pair in Properties) {
				propertyString = propertyString + String.Format("(Name: '{0}' Value: '{1}')", pair.Key, pair.Value) ;
			}
			
			string statsString = "" ;
			foreach (KeyValuePair<string, object> sPair in Statistics) {
				statsString = statsString + String.Format("(Name: '{0}' Value: '{1}')", sPair.Key, sPair.Value) ;
			}			
			if (Managed) {
				return String.Format("Managed QMFObject {0}:{1}({2}) Properties: [{3}] Statistics: [{4}])", Schema.PackageName, Schema.ClassName, ObjectID, propertyString, statsString) ;
			} else {
				return String.Format("QMFObject {0}:{1} Properties: [{2}] Statistics: [{3}]", Schema.PackageName, Schema.ClassName, propertyString, statsString) ;
			}
		}
		
		public MethodResult InvokeMethod(string name, params object[] args) {
			return this.InternalInvokeMethod(name, new List<object>(args), true, Broker.SYNC_TIME ) ;
		}
		
		public MethodResult InvokeMethod(string name, int timeToLive, params object[] args) {
			return this.InternalInvokeMethod(name, new List<object>(args), true, timeToLive) ;
		}
		
		public MethodResult InvokeMethod(string name, bool synchronous, int timeToLive, params object[] args) {
			return this.InternalInvokeMethod(name, new List<object>(args), synchronous, timeToLive ) ;
		}
				
		public MethodResult InvokeMethod(string name, bool synchronous, params object[] args) {
			return this.InternalInvokeMethod(name, new List<object>(args), synchronous, Broker.SYNC_TIME) ;
		}
		
		protected MethodResult InternalInvokeMethod(string name, List<object> args, bool synchronous, int timeToLive) {
			if (!Managed) {
				throw new Exception ("Object is not Managed") ;
			}
			if (Schema.GetMethod(name) == null) {
				throw new Exception (String.Format("Method named '{0}' does not exist", name))  ;
			}
			return Session.InvokeMethod(this, name, args, synchronous, timeToLive) ;
		}
		
		public void Encode (IEncoder enc) {
			int mask = 0 ;
			int bit = 0 ;
			List<SchemaProperty> propsToEncode = new List<SchemaProperty>() ;
			log.Debug(String.Format("Encoding class {0}:{1}", Schema.PackageName, Schema.ClassName)) ;
					
			enc.WriteUint8(20) ;
			Schema.Key.encode(enc) ;
			
			foreach (SchemaProperty prop in Schema.GetAllProperties()) {
				if (prop.Optional) {
					if (bit == 0) {
						bit =1 ;
					}
					if ((Properties.ContainsKey(prop.Name)) && (Properties[prop.Name] != null)) {
						mask |= bit ;
						propsToEncode.Add(prop) ;
					} else {
					}
					bit = bit << 1 ;
					if (bit == 256) {
						bit = 0 ;
						enc.WriteUint8((short)mask) ;
						mask = 0 ;
					}
				} else {
					propsToEncode.Add(prop) ;
				}
			}
			if (bit != 0) {
				enc.WriteUint8((short)mask) ;
			}		
			
			foreach (SchemaProperty prop in propsToEncode) {
				object obj = Properties[prop.Name] ;
				//log.Debug(String.Format("Encoding property {0}", prop.Name)) ;
				Session.EncodeValue(enc, prop.Type, obj) ;
			}
			foreach (SchemaStatistic stat in Schema.Statistics) {
				object obj = Statistics[stat.Name] ;
				Session.EncodeValue(enc, stat.Type, obj) ;
			}			
			log.Debug("Done") ;
		 
		 }
		 
		
		protected void safeAddProperty(string propName, object value) {
			if (Properties.ContainsKey(propName)) {
				Properties[propName] = value ;
			} else {
				Properties.Add(propName, value) ;
			}
		}
				 
		
	}
}