Exemple #1
0
        /// <summary>
        /// Creates instance of profile server from snapshot.
        /// </summary>
        /// <param name="Snapshot">Profile server snapshot.</param>
        /// <returns>New profile server instance.</returns>
        public static ProfileServer CreateFromSnapshot(ProfileServerSnapshot Snapshot)
        {
            ProfileServer res = new ProfileServer(Snapshot.Name, new GpsLocation(Snapshot.LocationLatitude, Snapshot.LocationLongitude), Snapshot.BasePort);

            res.availableIdentitySlots         = Snapshot.AvailableIdentitySlots;
            res.clientAppServiceInterfacePort  = Snapshot.ClientAppServiceInterfacePort;
            res.clientCustomerInterfacePort    = Snapshot.ClientCustomerInterfacePort;
            res.clientNonCustomerInterfacePort = Snapshot.ClientNonCustomerInterfacePort;
            res.ipAddress                   = IPAddress.Parse(Snapshot.IpAddress);
            res.locPort                     = Snapshot.LocPort;
            res.networkId                   = Crypto.FromHex(Snapshot.NetworkId);
            res.primaryInterfacePort        = Snapshot.PrimaryInterfacePort;
            res.serverNeighborInterfacePort = Snapshot.ServerNeighborInterfacePort;
            res.instanceDirectory           = res.GetInstanceDirectoryName();
            res.locServer                   = new LocServer(res);

            byte[] ipBytes = res.ipAddress.GetAddressBytes();
            Iop.Locnet.NodeContact contact = new Iop.Locnet.NodeContact()
            {
                IpAddress  = ProtocolHelper.ByteArrayToByteString(ipBytes),
                ClientPort = (uint)res.locPort,
                NodePort   = (uint)res.locPort
            };

            return(res);
        }
Exemple #2
0
        /// <summary>
        /// Creates profile server's snapshot.
        /// </summary>
        /// <returns>Profile server's snapshot.</returns>
        public ProfileServerSnapshot CreateSnapshot()
        {
            ProfileServerSnapshot res = new ProfileServerSnapshot()
            {
                AvailableIdentitySlots = this.availableIdentitySlots,
                BasePort = this.basePort,
                ClientAppServiceInterfacePort  = this.clientAppServiceInterfacePort,
                ClientCustomerInterfacePort    = this.clientCustomerInterfacePort,
                ClientNonCustomerInterfacePort = this.clientNonCustomerInterfacePort,
                HostedIdentities  = this.hostedIdentities.Select(i => i.Name).ToList(),
                IpAddress         = this.ipAddress.ToString(),
                IsRunning         = false,
                LocPort           = this.locPort,
                LocServer         = this.locServer.CreateSnapshot(),
                LocationLatitude  = this.location.Latitude,
                LocationLongitude = this.location.Longitude,
                Name                        = this.name,
                NetworkId                   = Crypto.ToHex(this.networkId),
                PrimaryInterfacePort        = this.primaryInterfacePort,
                ServerNeighborInterfacePort = this.serverNeighborInterfacePort
            };

            return(res);
        }
Exemple #3
0
        /// <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 = Crypto.ToHex(identity.ProfileImage);
                        this.Images.Add(identitySnapshot.ProfileImageHash, imageDataHex);
                    }
                }

                if (identitySnapshot.ThumbnailImageHash != null)
                {
                    if (!this.Images.ContainsKey(identitySnapshot.ThumbnailImageHash))
                    {
                        string imageDataHex = Crypto.ToHex(identity.ThumbnailImage);
                        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);
        }