~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Sync/.svn/text-base/SyncNode.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
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.
9
 
 |
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.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 | Author: Russ Young
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.IO;
26
 
using Simias.Storage;
27
 
using Simias.Client;
28
 
 
29
 
namespace Simias.Sync
30
 
{
31
 
        #region SyncNodeType
32
 
 
33
 
        /// <summary>
34
 
        /// Special Node types that sync deals with.
35
 
        /// </summary>
36
 
        public enum SyncNodeType : byte
37
 
        {
38
 
                /// <summary>
39
 
                /// This is a generic node.
40
 
                /// </summary>
41
 
                Generic,
42
 
                /// <summary>
43
 
                /// This node represents a file in the FS.
44
 
                /// </summary>
45
 
                File,
46
 
                /// <summary>
47
 
                /// This node represents a directory in the FS.
48
 
                /// </summary>
49
 
                Directory,
50
 
                /// <summary>
51
 
                /// This node represents a deleted node.
52
 
                /// </summary>
53
 
                Tombstone
54
 
        }
55
 
 
56
 
        #endregion
57
 
 
58
 
        #region SyncNodeInfo
59
 
 
60
 
        /// <summary>
61
 
        /// class to represent the minimal information that the sync code needs
62
 
        /// to know about a node to determine if it needs to be synced.
63
 
        /// </summary>
64
 
        public class SyncNodeInfo: IComparable
65
 
        {
66
 
                #region Fields
67
 
 
68
 
                /// <summary>
69
 
                /// The Node ID.
70
 
                /// </summary>
71
 
                public string ID;
72
 
                /// <summary>
73
 
                /// The local incarnation for the node.
74
 
                /// </summary>
75
 
                public ulong LocalIncarnation;
76
 
                /// <summary>
77
 
                /// The Master incarnation for this node.
78
 
                /// </summary>
79
 
                public ulong MasterIncarnation;
80
 
                /// <summary>
81
 
                ///     The base type of this node. 
82
 
                /// </summary>
83
 
                public SyncNodeType NodeType;
84
 
                /// <summary>
85
 
                /// The SyncOperation to perform on this node.
86
 
                /// </summary>
87
 
                public SyncOperation Operation;
88
 
                /// <summary>
89
 
                /// The size of this instance serialized.
90
 
                /// If fields are added this needs to be updated.
91
 
                /// </summary>
92
 
                public static int InstanceSize = 34;
93
 
 
94
 
                #endregion
95
 
 
96
 
                #region Constructor
97
 
 
98
 
                /// <summary>
99
 
                /// 
100
 
                /// </summary>
101
 
                public SyncNodeInfo()
102
 
                {
103
 
                }
104
 
 
105
 
                /// <summary>
106
 
                /// Constructs a SyncNodeInfo from a ShallowNode.
107
 
                /// </summary>
108
 
                /// <param name="collection"></param>
109
 
                /// <param name="sn"></param>
110
 
                public SyncNodeInfo(Collection collection, ShallowNode sn) :
111
 
                        this (new Node(collection, sn))
112
 
                {
113
 
                }
114
 
 
115
 
                /// <summary>
116
 
                /// Construct a SyncNodeInfo from a stream.
117
 
                /// </summary>
118
 
                /// <param name="reader"></param>
119
 
                internal SyncNodeInfo(BinaryReader reader)
120
 
                {
121
 
                        this.ID = new Guid(reader.ReadBytes(16)).ToString();
122
 
                        this.LocalIncarnation = reader.ReadUInt64();
123
 
                        this.MasterIncarnation = reader.ReadUInt64();
124
 
                        this.NodeType = (SyncNodeType)reader.ReadByte();
125
 
                        this.Operation = (SyncOperation)reader.ReadByte();
126
 
                }
127
 
 
128
 
                /// <summary>
129
 
                /// Construct a SyncNodeStamp from a Node.
130
 
                /// </summary>
131
 
                /// <param name="node">the node to use.</param>
132
 
                internal SyncNodeInfo(Node node)
133
 
                {
134
 
                        this.ID = node.ID;
135
 
                        this.LocalIncarnation = node.LocalIncarnation;
136
 
                        this.MasterIncarnation = node.MasterIncarnation;
137
 
                        this.NodeType = GetSyncNodeType(node.Type);
138
 
                        this.Operation = node.Type == NodeTypes.TombstoneType ? SyncOperation.Delete : SyncOperation.Unknown;
139
 
                }
140
 
 
141
 
                /// <summary>
142
 
                /// Consturct a SyncNodeStamp from a ChangeLogRecord.
143
 
                /// </summary>
144
 
                /// <param name="record">The record to use.</param>
145
 
                internal SyncNodeInfo(ChangeLogRecord record)
146
 
                {
147
 
                        this.ID = record.EventID;
148
 
                        this.LocalIncarnation = record.SlaveRev;
149
 
                        this.MasterIncarnation = record.MasterRev;
150
 
                        this.NodeType = GetSyncNodeType(record.Type.ToString());
151
 
                        switch (record.Operation)
152
 
                        {
153
 
                                case ChangeLogRecord.ChangeLogOp.Changed:
154
 
                                        this.Operation = SyncOperation.Change;
155
 
                                        break;
156
 
                                case ChangeLogRecord.ChangeLogOp.Created:
157
 
                                        this.Operation = SyncOperation.Create;
158
 
                                        break;
159
 
                                case ChangeLogRecord.ChangeLogOp.Deleted:
160
 
                                        this.Operation = SyncOperation.Delete;
161
 
                                        break;
162
 
                                case ChangeLogRecord.ChangeLogOp.Renamed:
163
 
                                        this.Operation = SyncOperation.Rename;
164
 
                                        break;
165
 
                                default:
166
 
                                        this.Operation = SyncOperation.Unknown;
167
 
                                        break;
168
 
                        }
169
 
                }
170
 
 
171
 
                #endregion
172
 
                
173
 
                #region publics
174
 
 
175
 
                /// <summary>
176
 
                /// Serializes this instance into a stream.
177
 
                /// </summary>
178
 
                /// <param name="writer">The stream to serialize to.</param>
179
 
                internal void Serialize(BinaryWriter writer)
180
 
                {
181
 
                        writer.Write(new Guid(ID).ToByteArray());
182
 
                        writer.Write(LocalIncarnation);
183
 
                        writer.Write(MasterIncarnation);
184
 
                        writer.Write((byte)NodeType);
185
 
                        writer.Write((byte)Operation);
186
 
                }
187
 
 
188
 
 
189
 
                /// <summary> implement some convenient operator overloads </summary>
190
 
                public int CompareTo(object obj)
191
 
                {
192
 
                        return ID.CompareTo(((SyncNodeInfo)obj).ID);
193
 
                }
194
 
 
195
 
                #endregion
196
 
 
197
 
                #region privates
198
 
 
199
 
                /// <summary>
200
 
                /// Converts the base type string into a SyncNodeType
201
 
                /// </summary>
202
 
                /// <param name="baseType">The base type.</param>
203
 
                /// <returns>the SyncNodeType.</returns>
204
 
                private SyncNodeType GetSyncNodeType(string baseType)
205
 
                {
206
 
                        if (baseType == NodeTypes.DirNodeType)
207
 
                        {
208
 
                                return SyncNodeType.Directory;
209
 
                        }
210
 
                        else if (baseType == NodeTypes.FileNodeType || baseType == NodeTypes.StoreFileNodeType)
211
 
                        {
212
 
                                return SyncNodeType.File;
213
 
                        }
214
 
                        else
215
 
                        {
216
 
                                return SyncNodeType.Generic;
217
 
                        }
218
 
                }
219
 
 
220
 
                #endregion
221
 
        }
222
 
 
223
 
        #endregion
224
 
 
225
 
        #region SyncNode
226
 
 
227
 
        /// <summary>
228
 
        /// This is the object that is used to sync a node.
229
 
        /// </summary>
230
 
        public class SyncNode : SyncNodeInfo
231
 
        {
232
 
                #region fields
233
 
 
234
 
                /// <summary>
235
 
                /// The node as an XML string.
236
 
                /// </summary>
237
 
                public string node;
238
 
 
239
 
                /// <summary>
240
 
                /// The size of this instance serialized.
241
 
                /// </summary>
242
 
                public new int InstanceSize
243
 
                {
244
 
                        get { return SyncNodeInfo.InstanceSize + node.Length; }
245
 
                }
246
 
                
247
 
                #endregion
248
 
 
249
 
                #region Constructor
250
 
 
251
 
                /// <summary>
252
 
                /// 
253
 
                /// </summary>
254
 
                internal SyncNode()
255
 
                {
256
 
                }
257
 
 
258
 
                /// <summary>
259
 
                /// Create a SyncNode from a Node.
260
 
                /// </summary>
261
 
                /// <param name="node">The node used to create the sync node.</param>
262
 
                internal SyncNode(Node node) :
263
 
                        base(node)
264
 
                {
265
 
                        this.node = node.Properties.ToString(true);
266
 
                }
267
 
 
268
 
                /// <summary>
269
 
                /// Create a SyncNode from a stream.
270
 
                /// </summary>
271
 
                /// <param name="reader">The stream containing the SyncNode.</param>
272
 
                internal SyncNode(BinaryReader reader) :
273
 
                        base(reader)
274
 
                {
275
 
                        this.MasterIncarnation = reader.ReadUInt64();
276
 
                        node = reader.ReadString();
277
 
                }
278
 
 
279
 
                #endregion
280
 
 
281
 
                #region publics
282
 
 
283
 
                /// <summary>
284
 
                /// Serialize the SyncNode to the stream.
285
 
                /// </summary>
286
 
                /// <param name="writer"></param>
287
 
                internal new void Serialize(BinaryWriter writer)
288
 
                {
289
 
                        base.Serialize(writer);
290
 
                        writer.Write(MasterIncarnation);
291
 
                        writer.Write(node);
292
 
                }
293
 
 
294
 
                #endregion
295
 
        }
296
 
 
297
 
        #endregion
298
 
 
299
 
 
300
 
        /// <summary>
301
 
        /// Used to report the status of a sync.
302
 
        /// </summary>
303
 
        public class SyncNodeStatus
304
 
        {
305
 
                #region Fields
306
 
 
307
 
                /// <summary>
308
 
                /// The ID of the node.
309
 
                /// </summary>
310
 
                public string           nodeID;
311
 
                /// <summary>
312
 
                /// The status of the sync.
313
 
                /// </summary>
314
 
                public SyncStatus       status;
315
 
 
316
 
                /// <summary>
317
 
                /// The size of this instance serialized.
318
 
                /// </summary>
319
 
                public int InstanceSize = 17;
320
 
 
321
 
                #endregion
322
 
        
323
 
                /// <summary>
324
 
                /// Constructs a SyncNodeStatus object.
325
 
                /// </summary>
326
 
                internal SyncNodeStatus()
327
 
                {
328
 
                        status = SyncStatus.Error;
329
 
                }
330
 
 
331
 
                /// <summary>
332
 
                /// Create a SyncNodeStatus from a stream.
333
 
                /// </summary>
334
 
                /// <param name="reader">The stream containing the SyncNode.</param>
335
 
                internal SyncNodeStatus(BinaryReader reader)
336
 
                {
337
 
                        this.nodeID = new Guid(reader.ReadBytes(16)).ToString();
338
 
                        this.status = (SyncStatus)reader.ReadByte();
339
 
                }
340
 
 
341
 
                /// <summary>
342
 
                /// Serialize the SyncNodeStatus to the stream.
343
 
                /// </summary>
344
 
                /// <param name="writer"></param>
345
 
                internal void Serialize(BinaryWriter writer)
346
 
                {
347
 
                        writer.Write(new Guid(nodeID).ToByteArray());
348
 
                        writer.Write((byte)status);
349
 
                }
350
 
        }
351
 
}