1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
22
|***************************************************************************/
25
using System.Collections;
29
namespace Simias.Client.Event
32
/// Describes the name-value pair contained in an IProcEventData object.
34
public class IProcEventNameValue
38
/// Name of the value.
68
/// Initializes an instance of the object.
70
/// <param name="name">Name of the value.</param>
71
/// <param name="value">String that represents the value.</param>
72
public IProcEventNameValue( string name, string value )
81
/// Implements the parsing of event data to and from is serialized
82
/// representation to an object.
84
public class IProcEventData
88
/// Xml tags that define the event data.
90
private static string EventTag = "Event";
91
private static string EventTypeTag = "type";
94
/// Xml tags used to describe a NodeEventArgs object.
96
private const string NEA_ActionTag = "Action";
97
private const string NEA_TimeTag = "Time";
98
private const string NEA_SourceTag = "Source";
99
private const string NEA_CollectionTag = "Collection";
100
private const string NEA_ModifierTag = "Modifier";
101
private const string NEA_TypeTag = "Type";
102
private const string NEA_EventIDTag = "EventID";
103
private const string NEA_NodeTag = "Node";
104
private const string NEA_FlagsTag = "Flags";
105
private const string NEA_MasterRevTag = "MasterRev";
106
private const string NEA_SlaveRevTag = "SlaveRev";
107
private const string NEA_FileSizeTag = "FileSize";
110
/// Xml tags used to describe a CollectionSyncEventArgs object.
112
private const string CEA_NameTag = "Name";
113
private const string CEA_IDTag = "ID";
114
private const string CEA_ActionTag = "Action";
115
private const string CEA_ConnectedTag = "Connected";
116
private const string CEA_YieldedTag = "Yielded";
119
/// Xml tags used to describe a FileSyncEventArgs object.
121
private const string FEA_CollectionIDTag = "CollectionID";
122
private const string FEA_ObjectTypeTag = "ObjectType";
123
private const string FEA_DeleteTag = "Delete";
124
private const string FEA_NameTag = "Name";
125
private const string FEA_SizeTag = "Size";
126
private const string FEA_SizeToSyncTag = "SizeToSync";
127
private const string FEA_SizeRemainingTag = "SizeRemaining";
128
private const string FEA_DirectionTag = "Direction";
129
private const string FEA_StatusTag = "Status";
132
/// Xml tags used to describe an NotifyEventArgs object.
134
private const string NMA_MessageTag = "Message";
135
private const string NMA_TimeTag = "Time";
136
private const string NMA_TypeTag = "Type";
139
/// Xml document used to hold event data.
141
private XmlDocument document;
146
/// Returns the type of event this object represents.
150
get { return document.DocumentElement.GetAttribute( EventTypeTag ); }
156
/// Initializes an instance of the object.
158
/// <param name="args">Information regarding the collection sync event.</param>
159
public IProcEventData( CollectionSyncEventArgs args )
161
document = new XmlDocument();
162
XmlElement element = document.CreateElement( EventTag );
163
element.SetAttribute( EventTypeTag, typeof( CollectionSyncEventArgs ).Name );
164
document.AppendChild( element );
165
FromCollectionSyncEventArgs( args );
169
/// Initializes an instance of the object.
171
/// <param name="args">Information regarding the file sync event.</param>
172
public IProcEventData( FileSyncEventArgs args )
174
document = new XmlDocument();
175
XmlElement element = document.CreateElement( EventTag );
176
element.SetAttribute( EventTypeTag, typeof( FileSyncEventArgs ).Name );
177
document.AppendChild( element );
178
FromFileSyncEventArgs( args );
182
/// Initializes an instance of the object.
184
/// <param name="args">Information regarding the node event.</param>
185
public IProcEventData( NodeEventArgs args )
187
document = new XmlDocument();
188
XmlElement element = document.CreateElement( EventTag );
189
element.SetAttribute( EventTypeTag, typeof( NodeEventArgs ).Name );
190
document.AppendChild( element );
191
FromNodeEventArgs( args );
195
/// Initalizes an instance of the object.
197
/// <param name="args">Information regarding the notify event.</param>
198
public IProcEventData( NotifyEventArgs args )
200
document = new XmlDocument();
201
XmlElement element = document.CreateElement( EventTag );
202
element.SetAttribute( EventTypeTag, typeof( NotifyEventArgs ).Name );
203
document.AppendChild( element );
204
FromNotifyEventArgs( args );
208
/// Initializes an instance of the object.
210
/// <param name="document">Xml document that contains an IProcEventData message.</param>
211
public IProcEventData( XmlDocument document )
213
this.document = document;
217
#region Private Methods
219
/// Translates the information in the NodeEventArgs object into the IProcEventData object.
221
/// <param name="args">NodeEventArgs containing Node event information.</param>
222
private void FromNodeEventArgs( NodeEventArgs args )
224
AddData( new IProcEventNameValue( NEA_ActionTag, args.EventData ) );
225
AddData( new IProcEventNameValue( NEA_TimeTag, args.TimeStamp.Ticks.ToString() ) );
226
AddData( new IProcEventNameValue( NEA_SourceTag, args.Source ) );
227
AddData( new IProcEventNameValue( NEA_CollectionTag, args.Collection ) );
228
AddData( new IProcEventNameValue( NEA_TypeTag, args.Type ) );
229
AddData( new IProcEventNameValue( NEA_EventIDTag, args.EventId.ToString() ) );
230
AddData( new IProcEventNameValue( NEA_NodeTag, args.Node ) );
231
AddData( new IProcEventNameValue( NEA_FlagsTag, args.Flags.ToString() ) );
232
AddData( new IProcEventNameValue( NEA_MasterRevTag, args.MasterRev.ToString() ) );
233
AddData( new IProcEventNameValue( NEA_SlaveRevTag, args.SlaveRev.ToString() ) );
234
AddData( new IProcEventNameValue( NEA_FileSizeTag, args.FileSize.ToString() ) );
235
AddData( new IProcEventNameValue( NEA_ModifierTag, args.Modifier ) );
239
/// Translates the information in the CollectionSyncEventArgs object into the IProcEventData object.
241
/// <param name="args">CollectionSyncEventArgs containing Sync event information.</param>
242
private void FromCollectionSyncEventArgs( CollectionSyncEventArgs args )
244
AddData( new IProcEventNameValue( CEA_NameTag, args.Name ) );
245
AddData( new IProcEventNameValue( CEA_IDTag, args.ID ) );
246
AddData( new IProcEventNameValue( CEA_ActionTag, args.Action.ToString() ) );
247
AddData( new IProcEventNameValue( CEA_ConnectedTag, args.Connected.ToString() ) );
248
AddData( new IProcEventNameValue( CEA_YieldedTag, args.Yielded.ToString() ) );
252
/// Translates the information in the FileSyncEventArgs object into the IProcEventData object.
254
/// <param name="args">FileSyncEventArgs containing Sync event information.</param>
255
private void FromFileSyncEventArgs( FileSyncEventArgs args )
257
AddData( new IProcEventNameValue( FEA_CollectionIDTag, args.CollectionID ) );
258
AddData( new IProcEventNameValue( FEA_ObjectTypeTag, args.ObjectType.ToString() ) );
259
AddData( new IProcEventNameValue( FEA_DeleteTag, args.Delete.ToString() ) );
260
AddData( new IProcEventNameValue( FEA_NameTag, args.Name ) );
261
AddData( new IProcEventNameValue( FEA_SizeTag, args.Size.ToString() ) );
262
AddData( new IProcEventNameValue( FEA_SizeToSyncTag, args.SizeToSync.ToString() ) );
263
AddData( new IProcEventNameValue( FEA_SizeRemainingTag, args.SizeRemaining.ToString() ) );
264
AddData( new IProcEventNameValue( FEA_DirectionTag, args.Direction.ToString() ) );
265
AddData( new IProcEventNameValue( FEA_StatusTag, args.Status.ToString() ) );
269
/// Translates the information in the NotifyEventArgs object into the IProcEventData object.
271
/// <param name="args">NotifyEventArgs containing the notify event information.</param>
272
private void FromNotifyEventArgs( NotifyEventArgs args )
274
AddData( new IProcEventNameValue( NMA_TypeTag, args.EventData ) );
275
AddData( new IProcEventNameValue( NMA_TimeTag, args.TimeStamp.Ticks.ToString() ) );
276
AddData( new IProcEventNameValue( NMA_MessageTag, args.Message ) );
280
#region Public Methods
282
/// Adds a name value pair to the event data.
284
/// <param name="data">IProcEventNameValue object that contains the name value pair.</param>
285
public void AddData( IProcEventNameValue data )
287
XmlElement element = document.CreateElement( data.Name );
288
element.InnerText = data.Value;
289
document.DocumentElement.AppendChild( element );
293
/// Method used by clients to enumerate the data in the IProcEventData object.
295
/// <returns>An IEnumerator object that can be used to get a list of IProcEventNameValue objects.</returns>
296
public IEnumerator GetEventEnumerator()
298
return new DataEnumerator( document ).GetEnumerator();
302
/// Converts an IProcEventRegistration object to a buffer representation.
304
/// <returns>A byte array that represents the IProcEventRegistration object.</returns>
305
public byte[] ToBuffer()
307
string msgString = ToString();
308
UTF8Encoding utf8 = new UTF8Encoding();
309
int msgLength = utf8.GetByteCount( msgString );
310
byte[] msgHeader = BitConverter.GetBytes( msgLength );
311
byte[] buffer = new byte[ msgHeader.Length + msgLength ];
313
// Copy the message length and the message into the buffer.
314
msgHeader.CopyTo( buffer, 0 );
315
utf8.GetBytes( msgString, 0, msgString.Length, buffer, 4 );
320
/// Converts the IProcEventData object into a CollectionSyncEventArgs object.
322
/// <returns>A CollectionSyncEventArgs object.</returns>
323
public CollectionSyncEventArgs ToCollectionSyncEventArgs()
325
// Preinitialize all of the node event arguments.
326
string name = string.Empty;
327
string ID = string.Empty;
328
Action action = Action.StartSync;
329
bool successful = true;
330
bool yielded = false;
332
// Walk through each named/value pair and convert the xml data back into CollectionSyncEventArgs data.
333
foreach ( XmlNode xn in document.DocumentElement )
351
action = ( Action )Enum.Parse( typeof( Action ), xn.InnerText, false );
355
case CEA_ConnectedTag:
357
successful = Boolean.Parse( xn.InnerText );
363
yielded = Boolean.Parse( xn.InnerText );
369
// Create the object and set the flags.
370
return new CollectionSyncEventArgs( name, ID, action, successful, yielded );
374
/// Converts the IProcEventData object into a FileSyncEventArgs object.
376
/// <returns>A FileSyncEventArgs object.</returns>
377
public FileSyncEventArgs ToFileSyncEventArgs()
379
// Preinitialize all of the node event arguments.
380
string collectionID = string.Empty;
381
ObjectType objectType = ObjectType.File;
383
string name = string.Empty;
386
long sizeRemaining = 0;
387
Direction direction = Direction.Downloading;
388
SyncStatus status = SyncStatus.Success;
390
// Walk through each named/value pair and convert the xml data back into FileSyncEventArgs data.
391
foreach ( XmlNode xn in document.DocumentElement )
395
case FEA_CollectionIDTag:
397
collectionID = xn.InnerText;
400
case FEA_ObjectTypeTag:
402
objectType = ( ObjectType )Enum.Parse( typeof( ObjectType ), xn.InnerText, false );
407
delete = Boolean.Parse( xn.InnerText );
418
size = Convert.ToInt64( xn.InnerText );
422
case FEA_SizeToSyncTag:
424
sizeToSync = Convert.ToInt64( xn.InnerText );
428
case FEA_SizeRemainingTag:
430
sizeRemaining = Convert.ToInt64( xn.InnerText );
434
case FEA_DirectionTag:
436
direction = ( Direction )Enum.Parse( typeof( Direction ), xn.InnerText, false );
442
status = (SyncStatus)Enum.Parse( typeof(SyncStatus), xn.InnerText, false);
448
// Create the object and set the flags.
449
return new FileSyncEventArgs( collectionID, objectType, delete, name, size, sizeToSync, sizeRemaining, direction, status );
453
/// Converts the IProcEventData object into a NodeEventArgs object.
455
/// <returns>A NodeEventArgs object.</returns>
456
public NodeEventArgs ToNodeEventArgs()
458
// Preinitialize all of the node event arguments.
459
EventType changeType = EventType.NodeChanged;
460
DateTime time = DateTime.MinValue;
461
string source = string.Empty;
462
string node = string.Empty;
463
string collection = string.Empty;
464
string modifier = string.Empty;
465
string type = string.Empty;
472
// Walk through each named/value pair and convert the xml data back into NodeEventArgs data.
473
foreach ( XmlNode xn in document.DocumentElement )
479
changeType = ( EventType )Enum.Parse( typeof( EventType ), xn.InnerText );
485
time = new DateTime( Convert.ToInt64( xn.InnerText ) );
491
source = xn.InnerText;
495
case NEA_CollectionTag:
497
collection = xn.InnerText;
501
case NEA_ModifierTag:
503
modifier = xn.InnerText;
515
eventID = Convert.ToInt32( xn.InnerText );
527
flags = Convert.ToUInt16( xn.InnerText );
531
case NEA_MasterRevTag:
533
masterRev = Convert.ToUInt64( xn.InnerText );
537
case NEA_SlaveRevTag:
539
slaveRev = Convert.ToUInt64( xn.InnerText );
543
case NEA_FileSizeTag:
545
fileSize = Convert.ToInt64( xn.InnerText );
551
// Create the object and set the flags.
552
NodeEventArgs args = new NodeEventArgs( source, node, collection, modifier, type, changeType, eventID, time, masterRev, slaveRev, fileSize );
558
/// Converts the IProcEventData object into a NotifyEventArgs object.
560
/// <returns>A NotifyEventArgs object.</returns>
561
public NotifyEventArgs ToNotifyEventArgs()
563
// Preinitialize all of the node event arguments.
564
string type = string.Empty;
565
string message = string.Empty;
566
DateTime time = DateTime.MinValue;
568
// Walk through each named/value pair and convert the xml data back into NotifyEventArgs data.
569
foreach ( XmlNode xn in document.DocumentElement )
581
message = xn.InnerText;
587
time = new DateTime( Convert.ToInt64( xn.InnerText ) );
593
// Create the object and set the flags.
594
return new NotifyEventArgs( type, message, time );
598
/// Converts an IProcEventData object to a string representation.
600
/// <returns>A string that represents the IProcEventData object.</returns>
601
public override string ToString()
603
return document.OuterXml;
607
#region IEnumerator Members
608
private class DataEnumerator : IEnumerable, IEnumerator
610
#region Class Members
612
/// Contains the enumeration for all data elements.
614
private IEnumerator dataEnumerator;
619
/// Initializes an instance of the object.
621
/// <param name="document">Xml document that represents an IProcEventData object.</param>
622
public DataEnumerator( XmlDocument document )
624
dataEnumerator = document.DocumentElement.GetEnumerator();
628
#region IEnumerable Members
630
/// Method used by clients to enumerate the data in the IProcEventData object.
632
/// <returns>An IEnumerator.</returns>
633
public IEnumerator GetEnumerator()
639
#region IEnumerator Members
641
/// Sets the enumerator to its initial position, which is before
642
/// the first element in the collection.
646
dataEnumerator.Reset();
650
/// Gets the current element in the collection.
652
public object Current
656
XmlElement element = dataEnumerator.Current as XmlElement;
657
return new IProcEventNameValue( element.Name, element.InnerText );
662
/// Advances the enumerator to the next element of the collection.
665
/// true if the enumerator was successfully advanced to the next element;
666
/// false if the enumerator has passed the end of the collection.
668
public bool MoveNext()
670
return dataEnumerator.MoveNext();