Ejemplo n.º 1
0
 private static Stream Serialize(Message message)
 {
     MemoryStream stream = new MemoryStream();
     Serializer.Serialize(message, stream);
     stream.Flush();
     return stream;
 }
Ejemplo n.º 2
0
 internal ClientCommandEventArgs(string localEndPoint, string remoteEndPoint, ServiceType serviceType, Message requestMessage, DateTime requestTime, Message responseMessage, DateTime responseTime)
     : this(localEndPoint, remoteEndPoint, serviceType, requestMessage, requestTime)
 {
     this.responseMessage = responseMessage;
     this.responseTime = responseTime;
     hasResponse = true;
 }
Ejemplo n.º 3
0
 internal ClientCommandEventArgs(string localEndPoint, string remoteEndPoint, ServiceType serviceType, Message requestMessage, DateTime requestTime)
 {
     this.localEndPoint = localEndPoint;
     this.remoteEndPoint = remoteEndPoint;
     this.serviceType = serviceType;
     this.requestTime = requestTime;
     this.requestMessage = requestMessage;
 }
Ejemplo n.º 4
0
        public void Serialize(Message message, Stream output)
        {
            if (output == null)
                throw new ArgumentNullException("output");

            if (!output.CanWrite)
                throw new ArgumentException("The output stream cannot be written.");

            BinaryWriter writer = new BinaryWriter(output, Encoding);
            Serialize(message, writer);
        }
Ejemplo n.º 5
0
        internal Message ProcessCallback(ServiceType serviceType, Message inputStream)
        {
            if (serviceType == ServiceType.Admin)
                return Processor.Process(inputStream);
            if (serviceType == ServiceType.Manager)
                return Manager.Processor.Process(inputStream);
            if (serviceType == ServiceType.Root)
                return Root.Processor.Process(inputStream);
            if (serviceType == ServiceType.Block)
                return Block.Processor.Process(inputStream);

            throw new InvalidOperationException();
        }
Ejemplo n.º 6
0
            private Message DoProcess(Message messageStream, int tryCount)
            {
                try {
                    HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(address.ToUri());
                    lock (request) {
                        // Write the message.
                        request.Headers["Service-Type"] = serviceType.ToString();
                        if (!String.IsNullOrEmpty(connector.userName) &&
                            !String.IsNullOrEmpty(connector.password))
                            request.Credentials = new NetworkCredential(connector.userName, connector.password);
                        request.Method = "POST";
                        Stream output = request.GetRequestStream();
                        connector.MessageSerializer.Serialize(messageStream, output);
                        output.Flush();
                        output.Close();

                        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                        if (response.StatusCode != HttpStatusCode.OK)
                            throw new InvalidOperationException();

                        Stream input = response.GetResponseStream();
                        ResponseMessage baseResponse = (ResponseMessage) connector.MessageSerializer.Deserialize(input, MessageType.Response);
                        return new ResponseMessage((RequestMessage)messageStream, baseResponse);
                    }
                } catch (Exception e) {
                    if (tryCount == 0 && e is WebException)
                        // retry ...
                        return DoProcess(messageStream, tryCount + 1);

                    MessageError error;
                    if (e is WebException) {
                        error = new MessageError(new Exception("Web Error: maybe a timeout in the request.", e));
                    } else {
                        // Report this error as a msg_stream fault,
                        error = new MessageError(new Exception(e.Message, e));
                    }

                    Message responseMessage;
                    if (messageStream is MessageStream) {
                        responseMessage = MessageStream.NewResponse();
                        ResponseMessage errorMessage = new ResponseMessage("error");
                        errorMessage.Arguments.Add(error);
                        ((MessageStream)responseMessage).AddMessage(errorMessage);
                    } else {
                        responseMessage = ((RequestMessage) messageStream).CreateResponse("error");
                        responseMessage.Arguments.Add(error);
                    }

                    return responseMessage;
                }
            }
Ejemplo n.º 7
0
 protected abstract void Serialize(Message message, BinaryWriter writer);
Ejemplo n.º 8
0
        internal void Response(Message response)
        {
            ClientCommandEventArgs args = new ClientCommandEventArgs(LocalEndPoint, remoteEndPoint, currentServiceType,
                                                                     currentRequest, currentRequestTime, response, DateTime.Now);

            try {
                service.OnClientResponse(args);
                //TODO: should we store the entire command?
                commandCount++;
            } finally {
                currentRequest = null;
            }
        }
Ejemplo n.º 9
0
        internal void Request(ServiceType serviceType, Message request)
        {
            currentRequest = request;
            currentRequestTime = DateTime.Now;
            currentServiceType = serviceType;

            ClientCommandEventArgs args = new ClientCommandEventArgs(LocalEndPoint, remoteEndPoint, currentServiceType,
                                                                     currentRequest, currentRequestTime);

            service.OnClientRequest(args);
        }
Ejemplo n.º 10
0
            public Message Process(Message request)
            {
                Message response;
                if (MessageStream.TryProcess(this, request, out response))
                    return response;

                response = ((RequestMessage)request).CreateResponse();

                // For each message in the message input,
                try {
                    string command = request.Name;
                    // Report on the services running,
                    if (command.Equals("report")) {
                        lock (service.serverManagerLock) {
                            // TODO:
                            long tm = 0;		// Total Memory
                            long fm = 0;		// Free Memory
                            long td = 0;		// Total Space
                            long fd = 0;		// Free Space
                            if (service.Block == null) {
                                response.Arguments.Add("block=no");
                            } else {
                                response.Arguments.Add(service.Block.BlockCount.ToString());
                            }
                            response.Arguments.Add("manager=" + (service.Manager == null ? "no" : "yes"));
                            response.Arguments.Add("root=" + (service.Root == null ? "no" : "yes"));
                            response.Arguments.Add(tm - fm);
                            response.Arguments.Add(tm);
                            response.Arguments.Add(td - fd);
                            response.Arguments.Add(td);
                        }
                    } else if (command.Equals("reportStats")) {
                        // Analytics stats; we convert the stats to a long[] array and
                        // send it as a reply.
                        long[] stats = GetStats();
                        response.Arguments.Add(stats);
                    } else {
                        // Starts a service,
                        if (command.Equals("init")) {
                            string service_type = request.Arguments[0].ToString();
                            service.StartService(service_type);
                        }
                            // Stops a service,
                        else if (command.Equals("dispose")) {
                            string service_type = request.Arguments[0].ToString();
                            service.StopService(service_type);
                        } else {
                            throw new Exception("Unknown command: " + command);
                        }

                        // Add reply message,
                        response.Arguments.Add(1L);
                    }

                } catch (OutOfMemoryException e) {
                    service.Logger.Error(service, "Out Of Memory Error.");
                    // This will end the connection);
                    throw;
                } catch (Exception e) {
                    service.Logger.Error("Error while processing.");
                    response.Arguments.Add(new MessageError(e));
                }

                return response;
            }
 private static string Serialize(Message messageStream)
 {
     MemoryStream outputStream = new MemoryStream();
     JsonRpcMessageSerializer messageSerializer = new JsonRpcMessageSerializer(Encoding.UTF8);
     messageSerializer.Serialize(messageStream, outputStream);
     outputStream.Position = 0;
     StreamReader reader = new StreamReader(outputStream);
     string line;
     StringBuilder sb = new StringBuilder();
     while ((line = reader.ReadLine()) != null) {
         sb.Append(line);
     }
     return sb.ToString();
 }
Ejemplo n.º 12
0
            public Message Process(Message request)
            {
                Message response;
                if (MessageStream.TryProcess(this, request, out response))
                    return response;

                response = ((RequestMessage) request).CreateResponse();

                // The messages in the stream,
                try {
                    // Check the service isn't in a stop state,
                    service.CheckErrorState();

                    switch (request.Name) {
                        case "getServerListForBlock": {
                            long blockId = request.Arguments[0].ToInt64();
                            BlockServerInfo[] servers = service.GetServerListForBlock(blockId);
                            IServiceAddress[] addresses = new IServiceAddress[servers.Length];
                            int[] status = new int[servers.Length];
                            response.Arguments.Add(servers.Length);
                            for (int i = 0; i < servers.Length; i++) {
                                addresses[i] = servers[i].Address;
                                status[i] = (int) servers[i].Status;
                            }
                            response.Arguments.Add(addresses);
                            response.Arguments.Add(status);
                            break;
                        }
                        case "allocateNode": {
                                int nodeSize = request.Arguments[0].ToInt32();
                                DataAddress address = service.AllocateNode(nodeSize);
                                response.Arguments.Add(address);
                                break;
                            }
                        case "registerBlockServer": {
                                IServiceAddress address = (IServiceAddress)request.Arguments[0].Value;
                                service.RegisterBlockServer(address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "unregisterBlockServer": {
                                IServiceAddress address = (IServiceAddress)request.Arguments[0].Value;
                                service.UnregisterBlockServer(address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "unregisterAllBlockServers": {
                                service.UnregisterAllBlockServers();
                                response.Arguments.Add(1L);
                                break;
                            }

                        // root servers
                        case "registerRootServer": {
                                IServiceAddress address = (IServiceAddress)request.Arguments[0].Value;
                                service.RegisterRootServer(address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "unregisterRootServer": {
                                IServiceAddress address = (IServiceAddress)request.Arguments[0].Value;
                                service.UnregisterRootServer(address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "unregisterAllRootServers": {
                                service.UnregisterAllRootServers();
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "getRootForPath": {
                                string pathName = (string)request.Arguments[0].Value;
                                IServiceAddress address = service.GetRootForPath(pathName);
                                response.Arguments.Add(address);
                                break;
                            }
                        case "addPathRootMapping": {
                                string pathName = request.Arguments[0].ToString();
                                IServiceAddress address = (IServiceAddress)request.Arguments[1].Value;
                                service.AddPathRootMapping(pathName, address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "removePathRootMapping": {
                                string pathName = request.Arguments[0].ToString();
                                service.RemovePathRootMapping(pathName);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "getPaths": {
                                string[] pathSet = service.GetPaths();
                                response.Arguments.Add(pathSet);
                                break;
                            }
                        case "getServerGUIDList": {
                                long blockId = request.Arguments[0].ToInt64();
                                long[] serverGuids = service.GetServerGuidList(blockId);
                                response.Arguments.Add(serverGuids);
                                break;
                            }
                        case "addBlockServerMapping": {
                                long blockId = request.Arguments[0].ToInt64();
                                long serverGuid = request.Arguments[1].ToInt64();
                                service.AddBlockServerMapping(blockId, serverGuid);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "removeBlockServerMapping": {
                                long blockId = request.Arguments[0].ToInt64();
                                long serverGuid = request.Arguments[1].ToInt64();
                                service.RemoveBlockServerMapping(blockId, serverGuid);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "notifyBlockServerFailure": {
                                IServiceAddress address = (IServiceAddress)request.Arguments[0].Value;
                                service.NotifyBlockServerFailure(address);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "getBlockMappingCount": {
                                long blockMappingCount = service.GetBlockMappingCount();
                                response.Arguments.Add(blockMappingCount);
                                break;
                            }
                        case "getBlockMappingRange": {
                                long start = request.Arguments[0].ToInt64();
                                long end = request.Arguments[1].ToInt64();
                                long[] mappings = service.GetBlockMappingRange(start, end);
                                response.Arguments.Add(mappings);
                                break;
                            }
                        default:
                            throw new ApplicationException("Unknown message name: " + request.Name);
                    }
                } catch (OutOfMemoryException e) {
                    service.Logger.Error("Out of Memory", e);
                    service.SetErrorState(e);
                    throw;
                } catch (Exception e) {
                    service.Logger.Error("Error while processing message", e);
                    response.Arguments.Add(new MessageError(e));
                }

                return response;
            }
Ejemplo n.º 13
0
 public Message Process(Message messageStream)
 {
     return connector.callback(serviceType, messageStream);
 }
Ejemplo n.º 14
0
        protected override void Serialize(Message message, BinaryWriter writer)
        {
            if (message is MessageStream) {
                MessageStream messageStream = (MessageStream)message;

                writer.Write(2);
                int sz = messageStream.MessageCount;
                writer.Write(sz);

                foreach (Message child in messageStream) {
                    Serialize(child, writer);
                }
            } else {
                writer.Write(1);
                string messageName = message.Name;
                if (String.IsNullOrEmpty(messageName)) {
                    if (message is RequestMessage)
                        throw new ArgumentException("A request message must have a name.");

                    Message request = ((ResponseMessage) message).Request;
                    if (request == null)
                        throw new ArgumentException("An unnamed response must belong to a request context.");

                    messageName = request.Name;
                }

                writer.Write(messageName);

                int sz = message.Arguments.Count;
                writer.Write(sz);
                for (int i = 0; i < sz; i++) {
                    MessageArgument argument = message.Arguments[i];
                    WriteArgument(argument, writer);
                }

                writer.Write(8);
            }
        }
Ejemplo n.º 15
0
 public static string GetErrorStackTrace(Message message)
 {
     MessageError error = GetError(message);
     return error == null ? null : error.StackTrace;
 }
Ejemplo n.º 16
0
 public Message Process(Message message)
 {
     return DoProcess(message, 0);
 }
Ejemplo n.º 17
0
 private Message Command(IServiceAddress machine, ServiceType serviceType, Message request)
 {
     IMessageProcessor proc = connector.Connect(machine, serviceType);
     return proc.Process(request);
 }
Ejemplo n.º 18
0
            private Message DoProcess(Message messageStream, int tryCount)
            {
                TcpConnection c = null;

                try {
                    // Check if there's a connection in the pool already,
                    c = connector.GetConnection(address);

                    lock (c) {
                        // Write the message.
                        char code = '\0';
                        if (serviceType == ServiceType.Manager) {
                            code = 'm';
                        } else if (serviceType == ServiceType.Root) {
                            code = 'r';
                        } else if (serviceType == ServiceType.Block) {
                            code = 'b';
                        } else if (serviceType == ServiceType.Admin) {
                            code = 'a';
                        }

                        BinaryWriter writer = new BinaryWriter(c.Stream, Encoding.Unicode);
                        writer.Write(code);

                        IMessageSerializer serializer = new BinaryRpcMessageSerializer();
                        serializer.Serialize(messageStream, c.Stream);
                        writer.Flush();

                        Message response = serializer.Deserialize(c.Stream, MessageType.Response);
                        if (response is MessageStream) {
                            return response;
                        } else {
                            return new ResponseMessage((RequestMessage) messageStream, (ResponseMessage) response);
                        }
                    }
                } catch (Exception e) {
                    // If this is a 'connection reset by peer' error, wipe the connection
                    // from the cache and retry connection,
                    if (tryCount == 0 &&
                        (e is SocketException ||
                         e is EndOfStreamException)) {
                        connector.InvalidateConnection(address);
                        // And retry,
                        return DoProcess(messageStream, tryCount + 1);
                    }

                    MessageError error;
                    if (e is EndOfStreamException) {
                        error = new MessageError(new Exception("EOF (is net password correct?)", e));
                    } else {
                        // Report this error as a msg_stream fault,
                        error = new MessageError(new Exception(e.Message, e));
                    }

                    Message responseMessage;
                    if (messageStream is MessageStream) {
                        responseMessage = new MessageStream(MessageType.Response);
                        ResponseMessage inner = new ResponseMessage();
                        inner.Arguments.Add(error);
                        ((MessageStream)responseMessage).AddMessage(inner);
                    } else {
                        responseMessage = ((RequestMessage) messageStream).CreateResponse();
                        responseMessage.Arguments.Add(error);
                    }

                    return responseMessage;
                } finally {
                    if (c != null)
                        connector.ReleaseConnection(c);
                }
            }
Ejemplo n.º 19
0
 public Message Process(Message messageStream)
 {
     return DoProcess(messageStream, 0);
 }
Ejemplo n.º 20
0
 protected abstract void Serialize(Message message, TextWriter writer);
Ejemplo n.º 21
0
 public static string GetErrorMessage(Message message)
 {
     MessageError error = GetError(message);
     return error == null ? null : error.Message;
 }
Ejemplo n.º 22
0
            public Message Process(Message request)
            {
                Message response;
                if (MessageStream.TryProcess(this, request, out response))
                    return response;

                // The reply message,

                response = ((RequestMessage) request).CreateResponse();

                try {
                    service.CheckErrorState();

                    switch (request.Name) {
                        case "publishPath": {
                            string pathName = request.Arguments[0].ToString();
                            DataAddress rootNode = (DataAddress) request.Arguments[1].Value;
                                service.PublishPath(pathName, rootNode);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "getSnapshot": {
                                string path = request.Arguments[0].ToString();
                                DataAddress address = service.GetSnapshot(path);
                                response.Arguments.Add(address);
                                break;
                            }
                        case "getSnapshots": {
                                string path = request.Arguments[0].ToString();
                                DateTime start = DateTime.FromBinary(request.Arguments[1].ToInt64());
                                DateTime end = DateTime.FromBinary(request.Arguments[2].ToInt64());
                                DataAddress[] addresses = service.GetSnapshots(path, start, end);
                                response.Arguments.Add(addresses);
                                break;
                            }
                        case "getCurrentTime": {
                                response.Arguments.Add(DateTime.Now.ToUniversalTime().ToBinary());
                                break;
                            }
                        case "addPath": {
                                string pathName = request.Arguments[0].ToString();
                                string pathTypeName = request.Arguments[1].ToString();
                                DataAddress rootNode = (DataAddress)request.Arguments[2].Value;
                                service.AddPath(pathName, pathTypeName, rootNode);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "removePath": {
                                string pathName = request.Arguments[0].ToString();
                                service.RemovePath(pathName);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "getPathType": {
                                string pathName = request.Arguments[0].ToString();
                                string pathType = service.GetPathType(pathName);
                                response.Arguments.Add(pathType);
                                break;
                            }
                        case "checkPathType": {
                                string pathType = request.Arguments[0].ToString();
                                service.CheckPathType(pathType);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "initPath": {
                                string pathName = request.Arguments[0].ToString();
                                service.InitPath(pathName);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "commit": {
                                string pathName = request.Arguments[0].ToString();
                                DataAddress proposal = (DataAddress)request.Arguments[1].Value;
                                DataAddress rootNode = service.Commit(pathName, proposal);
                                response.Arguments.Add(rootNode);
                                break;
                            }
                        case "bindWithManager": {
                                IServiceAddress manager = (IServiceAddress)request.Arguments[0].Value;
                                service.BindWithManager(manager);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "unbindWithManager": {
                                IServiceAddress manager = (IServiceAddress)request.Arguments[0].Value;
                                service.UnbindWithManager(manager);
                                response.Arguments.Add(1L);
                                break;
                            }
                        case "pathReport": {
                                string[] pathNames, pathTypes;
                                service.PathReport(out pathNames, out pathTypes);
                            response.Arguments.Add(pathNames);
                            response.Arguments.Add(pathTypes);
                                break;
                            }
                        default:
                            throw new ApplicationException("Unknown message received: " + request.Name);
                    }
                } catch (OutOfMemoryException e) {
                    service.Logger.Error("Out of memory!");
                    service.SetErrorState(e);
                    throw e;
                } catch (Exception e) {
                    service.Logger.Error("Error while processing", e);
                    response.Arguments.Add(new MessageError(e));
                }

                return response;
            }
Ejemplo n.º 23
0
        public static MessageError GetError(Message message)
        {
            if (message is MessageStream) {
                foreach (Message msg in (MessageStream)message) {
                    MessageError error = GetError(msg);
                    if (error != null)
                        return error;
                }

                return null;
            }

            return message.Arguments.Count == 1 && message.Arguments[0].Value is MessageError
                   	? (MessageError) message.Arguments[0].Value
                   	: null;
        }
Ejemplo n.º 24
0
        protected virtual void OnClientRequest(ServiceType serviceType, string remoteEndPoint, Message requestMessage)
        {
            if (connections == null)
                return;

            ClientConnection connection;
            if (!connections.TryGetValue(remoteEndPoint, out connection))
                return;

            connection.Request(serviceType, requestMessage);
        }
Ejemplo n.º 25
0
            public Message Process(Message messageStream)
            {
                try {
                    lock (connector.proxy_lock) {
                        IMessageSerializer serializer = connector.MessageSerializer;

                        char code = '\0';
                        if (serviceType == ServiceType.Admin)
                            code = 'a';
                        else if (serviceType == ServiceType.Block)
                            code = 'b';
                        else if (serviceType == ServiceType.Manager)
                            code = 'm';
                        else if (serviceType == ServiceType.Root)
                            code = 'r';

                        // Write the message.
                        connector.pout.Write(code);
                        TcpServiceAddressHandler handler = new TcpServiceAddressHandler();
                        byte[] addressBytes = handler.ToBytes(address);
                        connector.pout.Write(handler.GetCode(typeof(TcpServiceAddress)));
                        connector.pout.Write(addressBytes.Length);
                        connector.pout.Write(addressBytes);
                        serializer.Serialize(messageStream, connector.pout.BaseStream);
                        connector.pout.Flush();

                        ResponseMessage baseResponse = (ResponseMessage) serializer.Deserialize(connector.pin.BaseStream, MessageType.Response);
                        return new ResponseMessage((RequestMessage) messageStream, baseResponse);
                    }
                } catch (IOException e) {
                    // Probably caused because the proxy closed the connection when a
                    // timeout was reached.
                    throw new Exception("IO Error", e);
                }
            }
Ejemplo n.º 26
0
        protected void OnClientResponse(string remoteEndPoint, Message responseMessage)
        {
            if (connections == null)
                return;

            ClientConnection connection;
            if (!connections.TryGetValue(remoteEndPoint, out connection))
                return;

            connection.Response(responseMessage);
        }