Пример #1
0
        /// <summary>
        /// Send command with optional parameters to server and wait for the response
        /// </summary>
        /// <param name="command">The command name</param>
        /// <param name="data">The command parameters </param>
        /// <returns>The server's response</returns>
        public async Task <OsmpResponse> SendCommand(string command, JObject data = null)
        {
            var msg = new OsmpMessage {
                Type = "cmd", Id = command, Data = data
            };

            return(await Send(msg));
        }
Пример #2
0
        private void ReceivedMultipleEvents(OsmpMessage msg)
        {
            if (EventsReceived == null)
            {
                return;
            }
            var events = msg.Data["events"].Values <JObject>()
                         .Select(o => new OsmpEvent {
                Id = o.Value <string>("event"), Data = o.Value <JObject>("data")
            }).ToArray();

            EventsReceived(events);
        }
Пример #3
0
        private void HandleSignRequest(OsmpMessage msg)
        {
            var token = msg.Data.Value <string>("token");

            if (string.IsNullOrWhiteSpace(token))
            {
                Respond(msg, status: "ERROR", result: "Token is null or empty!");
                return;
            }
            var signature = Convert.ToBase64String(_rsa.SignData(Encoding.UTF8.GetBytes(token), CryptoConfig.MapNameToOID("SHA1")));

            Respond(msg, data: new JObject {
                { "signature", signature }
            });
        }
Пример #4
0
        private void Respond(OsmpMessage server_cmd, JObject data = null, string status = "OK", string result = null)
        {
            var msg = new JObject {
                { "type", "response" }, { "id", server_cmd.Id }, { "nr", Interlocked.Increment(ref _msg_counter) }, { "cmd-nr", server_cmd.Nr }, { "status", status }
            };

            if (!string.IsNullOrWhiteSpace(result))
            {
                msg["result"] = result;
            }
            if (data != null && data.Count > 0)
            {
                msg["data"] = data;
            }
            SendText(JsonConvert.SerializeObject(msg));
        }
Пример #5
0
        public async Task <OsmpResponse> SendCommand(string command, JObject @params = null)
        {
            var msg = new OsmpMessage {
                Type = "cmd", Id = command, Data = @params
            };
            var task_source = new TaskCompletionSource <OsmpResponse>();

            msg.SendTimestamp = DateTime.Now;
            msg.TaskSource    = task_source;
            msg.Nr            = Interlocked.Increment(ref _command_counter);
            var json = JsonConvert.SerializeObject(msg);

            lock (_waitingCommands)
                _waitingCommands[msg.Nr] = msg;
            SendText(json);
            return(await task_source.Task.ConfigureAwait(false));
        }
Пример #6
0
        private void ReceiveStream(OsmpStream stream)
        {
            LastReceived = DateTime.Now;
            OsmpMessage cmd = null;

            lock (_waitingCommands)
            {
                if (_waitingCommands.TryGetValue(stream.CmdNr, out cmd))
                {
                    stream.Command = cmd;
                }
            }
            if (CmdStreamReceived != null)
            {
                CmdStreamReceived(stream);
            }
        }
Пример #7
0
        private void ReceiveServerCmd(OsmpMessage msg)
        {
            switch (msg.Id)
            {
            case "password-request":
                HandlePasswordRequest(msg);
                break;

            case "sign-request":
                HandleSignRequest(msg);
                break;

            default:
                Respond(msg, status: "ERROR", result: "Unknown server command: " + msg.Id);
                break;
            }
        }
Пример #8
0
        private void GotSessionStatus(OsmpMessage msg)
        {
            var now = DateTime.Now;

            lock (_waitingCommands)
            {
                foreach (var cmd_nr in msg.Data["active-cmds"].Values <int>())
                {
                    if (!_waitingCommands.ContainsKey(cmd_nr))
                    {
                        continue;
                    }
                    var cmd = _waitingCommands[cmd_nr];
                    cmd.StreamReceivedTimestamp = now;
                    cmd.ReceiveTimestamp        = now;
                }
            }
        }
Пример #9
0
        private void ReceiveServerCmdResponse(JObject obj)
        {
            LastReceived = DateTime.Now;
            var         response = obj.ToObject <OsmpResponse>();
            OsmpMessage cmd      = null;

            lock (_waitingCommands)
            {
                if (_waitingCommands.TryGetValue(response.CmdNr, out cmd))
                {
                    _waitingCommands.Remove(response.CmdNr);
                }
            }
            response.Command = cmd;
            if (cmd != null)
            {
                cmd.TaskSource.SetResult(response);
            }
            //if (CmdResponseReceived != null)
            //    CmdResponseReceived(response);
        }
Пример #10
0
        private void HandlePasswordRequest(OsmpMessage msg)
        {
            var token    = msg.Data.Value <string>("token");
            var username = msg.Data.Value <string>("username");

            if (string.IsNullOrWhiteSpace(token))
            {
                Respond(msg, status: "ERROR", result: "Token is null or empty!");
                return;
            }
            string encrypted_pw = null;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(ServerPublicKey);
                encrypted_pw = Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes("" + _pw + token), false));
            }
            Respond(msg, data: new JObject {
                { "encrypted-password", encrypted_pw }
            });
        }
Пример #11
0
        /// <summary>
        /// Send the given message to the server and wait for the response
        /// </summary>
        /// <param name="msg">The message can be a command, response, event, stream or cancel message</param>
        /// <returns>The server's response</returns>
        public async Task <OsmpResponse> Send(OsmpMessage msg)
        {
            if (!IsConnected)
            {
                return new OsmpResponse()
                       {
                           Result = "Not connected!", Status = "FAIL", Command = msg
                       }
            }
            ;
            var task_source = new TaskCompletionSource <OsmpResponse>();

            msg.SendTimestamp = DateTime.Now;
            msg.TaskSource    = task_source;
            msg.Nr            = Interlocked.Increment(ref _msg_counter);
            var json = JsonConvert.SerializeObject(msg);

            lock (_waitingCommands)
                _waitingCommands[msg.Nr] = msg;

            if (msg.CancellationToken != CancellationToken.None)
            {
                msg.CancellationToken.Register(() => Cancel(msg));
            }
            _client.SendAsync(json, success =>
            {
                if (!success)
                {
                    task_source.TrySetResult(new OsmpResponse()
                    {
                        Result = "Send failed", Status = "FAIL", Command = msg
                    });
                }
                if (MessageSent != null)
                {
                    MessageSent(json, success);
                }
            });
            return(await task_source.Task.ConfigureAwait(false));
        }
Пример #12
0
        private void ReceiveCmdResponse(OsmpResponse response)
        {
            LastReceived = DateTime.Now;
            OsmpMessage cmd = null;

            lock (_waitingCommands)
            {
                if (_waitingCommands.TryGetValue(response.CmdNr, out cmd))
                {
                    _waitingCommands.Remove(response.CmdNr);
                }
            }
            response.Command = cmd;
            if (cmd != null)
            {
                cmd.TaskSource.TrySetResult(response);
            }
            if (CmdResponseReceived != null)
            {
                CmdResponseReceived(response);
            }
        }
Пример #13
0
        private void ReceiveEvent(OsmpMessage msg)
        {
            if (string.IsNullOrEmpty(msg.Id))
            {
                ReceivedMultipleEvents(msg);
                return;
            }
            switch (msg.Id)
            {
            case "session-initiated":
                var server_pkey = msg.Data.Value <string>("public-key");
                if (ServerPublicKey == null)
                {
                    ServerPublicKey = server_pkey;
                }
                else if (ServerPublicKey != server_pkey)
                {
                    if (Error != null)
                    {
                        Error("Warning: server's actual public key differs from configured key!", null);
                    }
                }
                NotifyConnectionEstablished();
                break;

            case "session-status":
                GotSessionStatus(msg);
                return;
            }
            if (EventsReceived != null)
            {
                EventsReceived(new[] { new OsmpEvent {
                                           Id = msg.Id, Data = msg.Data
                                       } });
            }
        }
Пример #14
0
 public void NotifyQuestionAnswerReceived(OsmpMessage answer)
 {
 }
Пример #15
0
 /// <summary>
 /// Cancel a long running command
 /// </summary>
 /// <param name="msg">The originally sent command to be cancelled</param>
 public void Cancel(OsmpMessage msg)
 {
     Cancel(new[] { msg.Nr });
 }