Example #1
0
        /// <summary>
        /// Creates identity client snapshot.
        /// </summary>
        /// <returns>Identity client snapshot.</returns>
        public IdentitySnapshot CreateSnapshot()
        {
            IdentitySnapshot res = new IdentitySnapshot()
            {
                Challenge             = this.challenge.ToHex(),
                ClientChallenge       = this.clientChallenge.ToHex(),
                ExpandedPrivateKeyHex = this.keys.ExpandedPrivateKeyHex,
                ExtraData             = this.extraData,
                HostingActive         = this.hostingActive,
                IdentityId            = this.identityId.ToHex(),
                ImageFileName         = Path.GetFileName(imageFileName),
                LocationLatitude      = this.location.Latitude,
                LocationLongitude     = this.location.Longitude,
                Name               = this.name,
                PrivateKeyHex      = this.keys.PrivateKeyHex,
                ProfileInitialized = this.profileInitialized,
                ProfileImageHash   = null,
                ProfileServerKey   = this.profileServerKey.ToHex(),
                ProfileServerName  = this.profileServer.Name,
                PublicKeyHex       = this.keys.PublicKeyHex,
                ThumbnailImageHash = null,
                Type               = this.type,
                Version            = this.version
            };


            if (this.profileImage != null)
            {
                byte[] profileImageHash    = Crypto.Sha256(profileImage);
                string profileImageHashHex = profileImageHash.ToHex();
                res.ProfileImageHash = profileImageHashHex;
            }

            if (this.thumbnailImage != null)
            {
                byte[] thumbnailImageHash    = Crypto.Sha256(thumbnailImage);
                string thumbnailImageHashHex = thumbnailImageHash.ToHex();
                res.ThumbnailImageHash = thumbnailImageHashHex;
            }

            return(res);
        }
Example #2
0
        /// <summary>
        /// Creates instance of identity client from snapshot.
        /// </summary>
        /// <param name="Snapshot">Identity client snapshot.</param>
        /// <param name="Images">Hexadecimal image data mapping to SHA256 hash.</param>
        /// <param name="ProfileServer">Profile server that hosts identity's profile.</param>
        /// <returns>New identity client instance.</returns>
        public static IdentityClient CreateFromSnapshot(IdentitySnapshot Snapshot, Dictionary <string, string> Images, ProfileServer ProfileServer)
        {
            IdentityClient res = new IdentityClient();

            res.challenge       = Snapshot.Challenge.FromHex();
            res.clientChallenge = Snapshot.ClientChallenge.FromHex();

            res.keys = new KeysEd25519();
            res.keys.ExpandedPrivateKeyHex = Snapshot.ExpandedPrivateKeyHex;
            res.keys.PublicKeyHex          = Snapshot.PublicKeyHex;
            res.keys.PrivateKeyHex         = Snapshot.PrivateKeyHex;
            res.keys.ExpandedPrivateKey    = res.keys.ExpandedPrivateKeyHex.FromHex();
            res.keys.PublicKey             = res.keys.PublicKeyHex.FromHex();
            res.keys.PrivateKey            = res.keys.PrivateKeyHex.FromHex();

            res.extraData     = Snapshot.ExtraData;
            res.hostingActive = Snapshot.HostingActive;
            res.identityId    = Snapshot.IdentityId.FromHex();
            res.imageFileName = Snapshot.ImageFileName != null?Path.Combine(CommandProcessor.ImagesDirectory, Snapshot.ImageFileName) : null;

            res.location           = new GpsLocation(Snapshot.LocationLatitude, Snapshot.LocationLongitude);
            res.name               = Snapshot.Name;
            res.profileInitialized = Snapshot.ProfileInitialized;

            res.profileImage   = Snapshot.ProfileImageHash != null ? Images[Snapshot.ProfileImageHash].FromHex() : null;
            res.thumbnailImage = Snapshot.ThumbnailImageHash != null ? Images[Snapshot.ThumbnailImageHash].FromHex() : null;

            res.profileServerKey = Snapshot.ProfileServerKey.FromHex();
            res.type             = Snapshot.Type;
            res.version          = Snapshot.Version;

            res.profileServer  = ProfileServer;
            res.log            = new Logger("NetworkSimulator.IdentityClient", "[" + res.Name + "] ");
            res.messageBuilder = new PsMessageBuilder(0, new List <SemVer>()
            {
                SemVer.V100
            }, res.keys);
            res.InitializeTcpClient();

            return(res);
        }
        /// <summary>
        /// Stops all running profile servers and takes snapshot of the simulation.
        /// <para>All profile servers are expected to be stopped when this method is called.</para>
        /// </summary>
        /// <param name="RunningServerNames">List of servers that were running before the snapshot was taken.</param>
        /// <param name="ProfileServers">List of simulation profile servers.</param>
        /// <param name="IdentityClients">List of simulation identities.</param>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public bool Take(HashSet <string> RunningServerNames, Dictionary <string, ProfileServer> ProfileServers, Dictionary <string, IdentityClient> IdentityClients)
        {
            log.Trace("()");

            foreach (ProfileServer server in ProfileServers.Values)
            {
                ProfileServerSnapshot serverSnapshot = server.CreateSnapshot();
                serverSnapshot.IsRunning = RunningServerNames.Contains(server.Name);
                this.ProfileServers.Add(serverSnapshot);
            }

            foreach (IdentityClient identity in IdentityClients.Values)
            {
                IdentitySnapshot identitySnapshot = identity.CreateSnapshot();

                if (identitySnapshot.ProfileImageHash != null)
                {
                    if (!this.Images.ContainsKey(identitySnapshot.ProfileImageHash))
                    {
                        string imageDataHex = identity.ProfileImage.ToHex();
                        this.Images.Add(identitySnapshot.ProfileImageHash, imageDataHex);
                    }
                }

                if (identitySnapshot.ThumbnailImageHash != null)
                {
                    if (!this.Images.ContainsKey(identitySnapshot.ThumbnailImageHash))
                    {
                        string imageDataHex = identity.ThumbnailImage.ToHex();
                        this.Images.Add(identitySnapshot.ThumbnailImageHash, imageDataHex);
                    }
                }

                this.Identities.Add(identitySnapshot);
            }


            bool error = false;

            try
            {
                if (Directory.Exists(snapshotDirectory))
                {
                    Directory.Delete(snapshotDirectory, true);
                }

                Directory.CreateDirectory(snapshotDirectory);
            }
            catch (Exception e)
            {
                log.Error("Exception occurred while trying to delete and recreate '{0}': {1}", snapshotDirectory, e.ToString());
                error = true;
            }

            if (!error)
            {
                try
                {
                    string serializedProfileServers = JsonConvert.SerializeObject(this.ProfileServers, Formatting.Indented);
                    string serializedIdentities     = JsonConvert.SerializeObject(this.Identities, Formatting.Indented);
                    string serializedImages         = JsonConvert.SerializeObject(this.Images, Formatting.Indented);

                    File.WriteAllText(profileServersFile, serializedProfileServers);
                    File.WriteAllText(identitiesFile, serializedIdentities);
                    File.WriteAllText(imagesFile, serializedImages);
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred while trying to save serialized simulation information: {0}", e.ToString());
                    error = true;
                }
            }

            if (!error)
            {
                foreach (ProfileServer server in ProfileServers.Values)
                {
                    string serverInstanceDirectory   = server.GetInstanceDirectoryName();
                    string snapshotInstanceDirectory = Path.Combine(new string[] { snapshotDirectory, "bin", server.Name });
                    if (!Helpers.DirectoryCopy(serverInstanceDirectory, snapshotInstanceDirectory, true, new string[] { "logs", "tmp" }))
                    {
                        log.Error("Unable to copy files from directory '{0}' to '{1}'.", serverInstanceDirectory, snapshotInstanceDirectory);
                        error = true;
                        break;
                    }

                    string logsDirectory = Path.Combine(snapshotInstanceDirectory, "Logs");
                    try
                    {
                        if (Directory.Exists(logsDirectory))
                        {
                            Directory.Delete(logsDirectory, true);
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error("Exception occurred while trying to delete directory '{0}': {1}", logsDirectory, e.ToString());
                        error = true;
                        break;
                    }
                }
            }

            bool res = !error;

            log.Trace("(-):{0}", res);
            return(res);
        }