/// <summary> /// Constructor of Session class for a new User /// </summary> /// <param name="username">Username of the user</param> /// <param name="password">Password of the user</param> /// <param name="localObjectStorage">LocalObjectStorage</param> internal Session(string username, string password, LocalObjectStorage localObjectStorage) { LocalObjectStorage = localObjectStorage; rsaKeyPair = Crypto.DeriveRsaKey(Encoding.UTF8.GetBytes(username), Encoding.UTF8.GetBytes(password)); KademliaId id = Crypto.Hash(rsaKeyPair.Public); Username = username; User = localObjectStorage.GetObject(id) as User; if (User == null) { User = new User(rsaKeyPair.Public) {ObjectId = id}; localObjectStorage.StoreObject(User, true); } onUserUpdated = UpdateUser; localObjectStorage.OnUserUpdated += onUserUpdated; }
internal void Test() { var objectId = KademliaId.RandomId; var chunkId = KademliaId.RandomId; var user = new User(); user.ObjectId = objectId; var userFile = new UserFile(); userFile.ChunkList.Add(chunkId); user.Add(userFile); var ms = new MemoryStream(); Serializer.Serialize(ms, user); var bytes = ms.ToArray(); // store the object var ct1 = new CallbackTimeout<Error>(); nodeA.StoreObject(nodeBInfo, objectId, bytes, ct1.Done); if (!ct1.Block(TestParameters.LocalhostCommunicationTimeout)) { Assert.Fail("No response within timeout"); } Assert.AreEqual(Error.Success, ct1.Result); // now retrieve it var ct2 = new CallbackTimeout<Error, byte[]>(); nodeC.GetObjectFromNode(new List<NodeInformation>{nodeBInfo}, objectId, ct2.Done); if (!ct2.Block(TestParameters.LocalhostCommunicationTimeout)) { Assert.Fail("No response within timeout"); } Assert.AreEqual(Error.Success, ct2.Result1); Assert.AreElementsEqual(bytes, ct2.Result2); }
/// <summary> /// Merges the user object with the current object if they are different /// </summary> /// <param name="user">user object to merge this object with</param> /// <returns>True if merged, false if they where identical</returns> internal bool Merge(User user) { using (MemoryStream msThis = new MemoryStream(), msOther = new MemoryStream()) { Serializer.Serialize(msThis, this); Serializer.Serialize(msOther, user); if (Crypto.Hash(msThis.ToArray()).SequenceEqual(Crypto.Hash(msOther.ToArray()))) return false; } foreach (var file in user.files) { if (files.All(f => f.FileId != file.FileId) && deletedFiles.All(f => f.Item2 != file.FileId)) Add(file); } foreach (var file in user.deletedFiles) { if (deletedFiles.All(f => f.Item2 != file.Item2)) AddDeletedFile(file.Item1, file.Item2); } return true; }
private void UpdateUser(User updatedUser) { User.Merge(updatedUser); if (OnUserMerged != null) OnUserMerged(); }
private void StoreTestObject() { // create the test object objectId = KademliaId.RandomId; var chunkId = KademliaId.RandomId; var user = new User(); user.ObjectId = objectId; var userFile = new UserFile(); userFile.ChunkList.Add(chunkId); user.Add(userFile); var ms = new MemoryStream(); Serializer.Serialize(ms, user); bytes = ms.ToArray(); // store it var ct = new CallbackTimeout<Error>(); var ni = new NodeInformation(new IPEndPoint(IPAddress.Loopback, nodes[1].Port), nodes[1].Id); nodes[0].StoreObject(ni, objectId, bytes, ct.Done); if (!ct.Block(TestParameters.LocalhostCommunicationTimeout)) { Assert.Fail("No response within timeout"); } Assert.AreEqual(Error.Success, ct.Result); }