public SynchronizationTopology Flatten()
        {
            var topology = new SynchronizationTopology();

            topology.Servers.Add(ServerUrl);

            var queue = new Queue <SynchronizationTopologyNodeBase>();

            queue.Enqueue(this);

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();

                foreach (var dst in node.Destinations)
                {
                    HandleLink(topology, node, dst);
                    queue.Enqueue(dst);
                }

                foreach (var source in node.Sources)
                {
                    HandleLink(topology, node, source);
                    queue.Enqueue(source);
                }
            }

            return(topology);
        }
        private static void HandleLink(SynchronizationTopology topology, SynchronizationTopologyNodeBase source, SynchronizationTopologyNodeBase target)
        {
            topology.Servers.Add(source.ServerUrl);
            topology.Servers.Add(target.ServerUrl);

            SynchronizationTopologyConnection connection = null;

            if (target is SynchronizationTopologyDestinationNode)
            {
                var destinationNode = (SynchronizationTopologyDestinationNode)target;
                // going to destination
                connection             = topology.GetConnection(source.ServerUrl, target.ServerUrl) ?? new SynchronizationTopologyConnection();
                connection.Source      = source.ServerUrl;
                connection.Destination = target.ServerUrl;
                connection.SourceToDestinationState = destinationNode.State;
                connection.SendServerId             = destinationNode.SendServerId;
                connection.Errors = destinationNode.Errors;
            }
            else if (target is SynchronizationTopologySourceNode)
            {
                // going to source
                var sourceNode = (SynchronizationTopologySourceNode)target;
                connection             = topology.GetConnection(target.ServerUrl, source.ServerUrl) ?? new SynchronizationTopologyConnection();
                connection.Source      = target.ServerUrl;
                connection.Destination = source.ServerUrl;
                connection.DestinationToSourceState = sourceNode.State;
                connection.StoredServerId           = sourceNode.StoredServerId;
                connection.LastSourceFileEtag       = sourceNode.LastSourceFileEtag;
            }

            topology.Connections.Add(connection);
        }
        public SynchronizationTopology Flatten()
        {
            var topology = new SynchronizationTopology();

            topology.Servers.Add(ServerUrl);

            var queue = new Queue <SynchronizationTopologyNodeBase>();

            queue.Enqueue(this);

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();

                foreach (var dst in node.Destinations)
                {
                    HandleLink(topology, node, dst);
                    queue.Enqueue(dst);
                }

                foreach (var source in node.Sources)
                {
                    HandleLink(topology, node, source);
                    queue.Enqueue(source);
                }
            }

            foreach (var connection in topology.Connections)
            {
                connection.Source      = GetUrlByResourceId(connection.SourceServerId, connection.Source);
                connection.Destination = GetUrlByResourceId(connection.DestinationServerId, connection.Destination);

                Guid resourceId;
                if (connection.DestinationServerId.HasValue == false &&
                    urlToResourceId.TryGetValue(connection.Destination, out resourceId))
                {
                    connection.DestinationServerId = resourceId;
                }

                topology.Servers.Add(connection.Source);
                topology.Servers.Add(connection.Destination);
            }

            return(topology);
        }
        private void HandleLink(SynchronizationTopology topology, SynchronizationTopologyNodeBase source, SynchronizationTopologyNodeBase target)
        {
            SynchronizationTopologyConnection connection = null;

            if (target is SynchronizationTopologyDestinationNode)
            {
                var destinationNode = (SynchronizationTopologyDestinationNode)target;
                // going to destination

                var sourceServerId = destinationNode.SendServerId;
                var sourceUrl      = AddToCache(sourceServerId, source.ServerUrl);

                var destinationId = GetResourceId(destinationNode.DestinationServerId, destinationNode.ServerUrl);
                var targetUrl     = AddToCache(destinationId, destinationNode.ServerUrl);

                connection = topology.GetConnection(sourceServerId, destinationId, sourceUrl, targetUrl);
                if (connection == null)
                {
                    connection = new SynchronizationTopologyConnection();
                    topology.Connections.Add(connection);
                }

                connection.SendServerId        = destinationNode.SendServerId;
                connection.DestinationServerId = destinationId;
                connection.SourceUrl.Add(source.ServerUrl);
                connection.DestinationUrl.Add(target.ServerUrl);
                connection.SourceToDestinationState = destinationNode.State;
                connection.Errors = destinationNode.Errors;

                //left for backward compatibility with v3.0
                connection.Source      = sourceUrl;
                connection.Destination = targetUrl;
            }
            else if (target is SynchronizationTopologySourceNode)
            {
                // going to source
                var sourceNode     = (SynchronizationTopologySourceNode)target;
                var sourceServerId = sourceNode.StoredServerId;
                var sourceUrl      = AddToCache(sourceServerId, target.ServerUrl);

                var destinationId = GetResourceId(sourceNode.DestinationServerId, source.ServerUrl);
                var targetUrl     = AddToCache(destinationId, source.ServerUrl);

                connection = topology.GetConnection(sourceServerId, destinationId, sourceUrl, targetUrl);
                if (connection == null)
                {
                    connection = new SynchronizationTopologyConnection();
                    topology.Connections.Add(connection);
                }

                connection.StoredServerId      = sourceServerId;
                connection.DestinationServerId = destinationId;
                connection.SourceUrl.Add(target.ServerUrl);
                connection.DestinationUrl.Add(source.ServerUrl);
                connection.DestinationToSourceState = sourceNode.State;
                connection.LastSourceFileEtag       = sourceNode.LastSourceFileEtag;

                //left for backward compatibility with v3.0
                connection.Source      = sourceUrl;
                connection.Destination = targetUrl;
            }

            topology.Connections.Add(connection);
        }