public ReplicationResult ReplicateNextPage(NodeEndpoint replicationEndpoint,
                                                   ReplicationType type,
                                                   int segment)
        {
            writer.Write(new StorageMessageUnion.Builder
            {
                Type = StorageMessageType.ReplicateNextPageRequest,
                ReplicateNextPageRequest = new ReplicateNextPageRequestMessage.Builder
                {
                    ReplicationEndpoint = replicationEndpoint.GetNodeEndpoint(),
                    Segment             = segment,
                    Type = type == ReplicationType.Backup? Protocol.ReplicationType.Backup : Protocol.ReplicationType.Ownership
                }.Build()
            }.Build());
            writer.Flush();
            stream.Flush();

            var union = ReadReply(StorageMessageType.ReplicateNextPageResponse);

            return(new ReplicationResult
            {
                Done = union.ReplicateNextPageResponse.Done,
                PutRequests = union.ReplicateNextPageResponse.PutRequestsList.Select(
                    x => x.GetPutRequest()
                    ).ToArray(),
                RemoveRequests = union.ReplicateNextPageResponse.RemoveRequestsList.Select(
                    x => x.GetRemoveRequest()
                    ).ToArray()
            });
        }
        public int[] AssignAllEmptySegments(NodeEndpoint replicationEndpoint,
                                            ReplicationType type, int[] segments)
        {
            writer.Write(new StorageMessageUnion.Builder
            {
                Type = StorageMessageType.AssignAllEmptySegmentsRequest,
                AssignAllEmptySegmentsRequest = new AssignAllEmptySegmentsRequestMessage.Builder
                {
                    ReplicationEndpoint = replicationEndpoint.GetNodeEndpoint(),
                    Type         = type == ReplicationType.Backup? Protocol.ReplicationType.Backup : Protocol.ReplicationType.Ownership,
                    SegmentsList = { segments }
                }.Build()
            }.Build());
            writer.Flush();
            stream.Flush();

            var union = ReadReply(StorageMessageType.AssignAllEmptySegmentsResponse);

            return(union.AssignAllEmptySegmentsResponse.AssignedSegmentsList.ToArray());
        }
        public int[] AssignAllEmptySegments(NodeEndpoint replicationEndpoint,
            ReplicationType type,   int[] segments)
        {
            writer.Write(new StorageMessageUnion.Builder
            {
                Type = StorageMessageType.AssignAllEmptySegmentsRequest,
                AssignAllEmptySegmentsRequest = new AssignAllEmptySegmentsRequestMessage.Builder
                {
                    ReplicationEndpoint = replicationEndpoint.GetNodeEndpoint(),
                    Type = type == ReplicationType.Backup? Protocol.ReplicationType.Backup : Protocol.ReplicationType.Ownership,
                    SegmentsList = { segments }
                }.Build()
            }.Build());
            writer.Flush();
            stream.Flush();

            var union = ReadReply(StorageMessageType.AssignAllEmptySegmentsResponse);

            return union.AssignAllEmptySegmentsResponse.AssignedSegmentsList.ToArray();
        }
        public ReplicationResult ReplicateNextPage(NodeEndpoint replicationEndpoint,
            ReplicationType type,
            int segment)
        {
            writer.Write(new StorageMessageUnion.Builder
            {
                Type = StorageMessageType.ReplicateNextPageRequest,
                ReplicateNextPageRequest = new ReplicateNextPageRequestMessage.Builder
                {
                    ReplicationEndpoint = replicationEndpoint.GetNodeEndpoint(),
                    Segment = segment,
                    Type = type == ReplicationType.Backup? Protocol.ReplicationType.Backup : Protocol.ReplicationType.Ownership
                }.Build()
            }.Build());
            writer.Flush();
            stream.Flush();

            var union = ReadReply(StorageMessageType.ReplicateNextPageResponse);

            return new ReplicationResult
            {
                Done = union.ReplicateNextPageResponse.Done,
                PutRequests = union.ReplicateNextPageResponse.PutRequestsList.Select(
                    x => x.GetPutRequest()
                    ).ToArray(),
                RemoveRequests = union.ReplicateNextPageResponse.RemoveRequestsList.Select(
                    x => x.GetRemoveRequest()
                    ).ToArray()
            };
        }