Ejemplo n.º 1
0
        public string Translate(string sourceText, string sourceTextLang, string targetTextLanguage)
        {
            if (_initialized == false)
            {
                throw new Exception("No connection to server");
            }

            _client = new UdpClient();

            try
            {
                var serverEndPoint = new IPEndPoint(RemoteHostIpAddress, Port);
                _client.Connect(serverEndPoint);

                // From "English" to "en", from "Romanian" to "ro", etc.
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref sourceTextLang);
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref targetTextLanguage);

                string textToBeSent = string.Format(Template.TranslateTemplate,
                                                    sourceText, sourceTextLang, targetTextLanguage);

                byte[] buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                _client.Send(buffer, buffer.Length);
                buffer = _client.Receive(ref serverEndPoint);

                string response = string.Empty;

                if (buffer.Length > 0)
                {
                    response = buffer.ToFlowProtocolAsciiDecodedString();
                }

                var responseComponents = _parser.ParseResponse(response);

                if (responseComponents.TryGetValue(Cmd, out string cmd))
                {
                    if (cmd == Commands.Translate)
                    {
                        if (responseComponents.TryGetValue(ResultValue, out string resultValue))
                        {
                            return(resultValue);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                _client.Close();
            }
            return(string.Empty);
        }
Ejemplo n.º 2
0
        public GetMessageResult GetMessage(string translationMode)
        {
            if (_initialized == false)
            {
                throw new Exception("No connection to server");
            }

            if (_sessionToken == Guid.Empty)
            {
                throw new Exception("Not authorized. You have to sign in first.");
            }

            _client = new UdpClient();

            try
            {
                var serverEndPoint = new IPEndPoint(RemoteHostIpAddress, Port);
                _client.Connect(serverEndPoint);

                byte[] buffer;

                if (translationMode == Template.Convention.ClientSaysDoNotTranslate)
                {
                    string textToBeSent = string.Format(Template.GetMessageUnmodifiedTemplate,
                                                        _sessionToken);

                    buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                    _client.Send(buffer, buffer.Length);
                }
                else
                {
                    // From "English" to "en", from "Romanian" to "ro", etc.
                    FlowUtility.ConvertToFlowProtocolLanguageNotations(ref translationMode);

                    string textToBeSent = string.Format(Template.GetMessageTranslatedTemplate,
                                                        _sessionToken, translationMode);

                    buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                    _client.Send(buffer, buffer.Length);
                }

                buffer = _client.Receive(ref serverEndPoint);

                string response = string.Empty;

                if (buffer.Length > 0)
                {
                    response = buffer.ToFlowProtocolAsciiDecodedString();
                }

                var responseComponents = _parser.ParseResponse(response);

                if (responseComponents.TryGetValue(Cmd, out string cmd))
                {
                    if (cmd == Commands.GetMessage)
                    {
                        if (responseComponents.TryGetValue(StatusDescription, out string statusDesc))
                        {
                            if (statusDesc == Error)
                            {
                                responseComponents.TryGetValue(ResultValue, out string resultValue);

                                return(new GetMessageResult
                                {
                                    Success = false,
                                    ErrorExplained = resultValue
                                });
                            }
                            if (statusDesc == Ok)
                            {
                                responseComponents.TryGetValue(SenderId, out string senderId);
                                responseComponents.TryGetValue(SenderName, out string senderName);
                                responseComponents.TryGetValue(Message, out string message);

                                return(new GetMessageResult
                                {
                                    Success = true,
                                    SenderId = senderId,
                                    SenderName = senderName,
                                    MessageBody = message
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                _client.Close();
            }
            return(new GetMessageResult
            {
                Success = false
            });
        }
Ejemplo n.º 3
0
        public SendMessageResult SendMessage(string recipient, string messageText, string messageTextLang)
        {
            if (_initialized == false)
            {
                throw new Exception("No connection to server");
            }

            if (_sessionToken == Guid.Empty)
            {
                throw new Exception("Not authorized. You have to sign in first.");
            }

            _client = new UdpClient();

            try
            {
                var serverEndPoint = new IPEndPoint(RemoteHostIpAddress, Port);
                _client.Connect(serverEndPoint);

                // From "English" to "en", from "Romanian" to "ro", etc.
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref messageTextLang);

                string textToBeSent = string.Format(Template.SendMessageTemplate,
                                                    recipient, messageText, messageTextLang, _sessionToken);

                byte[] buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                _client.Send(buffer, buffer.Length);
                buffer = _client.Receive(ref serverEndPoint);

                string response = string.Empty;

                if (buffer.Length > 0)
                {
                    response = buffer.ToFlowProtocolAsciiDecodedString();
                }

                var responseComponents = _parser.ParseResponse(response);

                if (responseComponents.TryGetValue(Cmd, out string cmd))
                {
                    if (cmd == Commands.SendMessage)
                    {
                        if (responseComponents.TryGetValue(StatusDescription, out string statusDesc))
                        {
                            if (statusDesc == Error)
                            {
                                return(new SendMessageResult
                                {
                                    Success = false
                                });
                            }
                            if (statusDesc == Ok)
                            {
                                if (responseComponents.TryGetValue(ResultValue, out string resultValue))
                                {
                                    return(new SendMessageResult
                                    {
                                        Success = true,
                                        ResponseMessage = resultValue
                                    });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                _client.Close();
            }
            return(new SendMessageResult
            {
                Success = false
            });
        }
Ejemplo n.º 4
0
        public SendMessageResult SendMessage(string recipient, string messageText, string messageTextLang)
        {
            if (_initialized == false)
            {
                throw new Exception("No connection to server");
            }

            if (_sessionToken == Guid.Empty)
            {
                throw new Exception("Not authorized. You have to sign in first.");
            }

            _client = new TcpClient();

            try
            {
                _client.Connect(RemoteHostIpAddress, Port);

                NetworkStream networkStream = _client.GetStream();

                // From "English" to "en", from "Romanian" to "ro", etc.
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref messageTextLang);

                string textToBeSent = string.Format(Template.SendMessageTemplate,
                                                    recipient, messageText, messageTextLang, _sessionToken);

                byte[] buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                if (networkStream.CanWrite)
                {
                    networkStream.Write(buffer, FromBeginning, buffer.Length);
                    networkStream.Flush();
                }

                string response = string.Empty;

                if (networkStream.CanRead)
                {
                    buffer = new byte[EthernetTcpUdpPacketSize];
                    int bytesRead = networkStream.Read(buffer, FromBeginning, EthernetTcpUdpPacketSize);
                    response = buffer.Take(bytesRead).ToArray().ToFlowProtocolAsciiDecodedString();
                }

                var responseComponents = _parser.ParseResponse(response);

                if (responseComponents.TryGetValue(Cmd, out string cmd))
                {
                    if (cmd == Commands.SendMessage)
                    {
                        if (responseComponents.TryGetValue(StatusDescription, out string statusDesc))
                        {
                            if (statusDesc == Error)
                            {
                                return(new SendMessageResult
                                {
                                    Success = false
                                });
                            }
                            if (statusDesc == Ok)
                            {
                                if (responseComponents.TryGetValue(ResultValue, out string resultValue))
                                {
                                    return(new SendMessageResult
                                    {
                                        Success = true,
                                        ResponseMessage = resultValue
                                    });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                _client.Close();
            }
            return(new SendMessageResult
            {
                Success = false
            });
        }
Ejemplo n.º 5
0
        public string Translate(string sourceText, string sourceTextLang, string targetTextLanguage)
        {
            if (_initialized == false)
            {
                throw new Exception("No connection to server");
            }

            _client = new TcpClient();

            try
            {
                _client.Connect(RemoteHostIpAddress, Port);

                NetworkStream networkStream = _client.GetStream();

                // From "English" to "en", from "Romanian" to "ro", etc.
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref sourceTextLang);
                FlowUtility.ConvertToFlowProtocolLanguageNotations(ref targetTextLanguage);

                string textToBeSent = string.Format(Template.TranslateTemplate,
                                                    sourceText, sourceTextLang, targetTextLanguage);

                byte[] buffer = textToBeSent.ToFlowProtocolAsciiEncodedBytesArray();

                if (networkStream.CanWrite)
                {
                    networkStream.Write(buffer, FromBeginning, buffer.Length);
                    networkStream.Flush();
                }

                string response = string.Empty;

                if (networkStream.CanRead)
                {
                    buffer = new byte[EthernetTcpUdpPacketSize];
                    int bytesRead = networkStream.Read(buffer, FromBeginning, EthernetTcpUdpPacketSize);
                    response = buffer.Take(bytesRead).ToArray().ToFlowProtocolAsciiDecodedString();
                }

                var responseComponents = _parser.ParseResponse(response);

                if (responseComponents.TryGetValue(Cmd, out string cmd))
                {
                    if (cmd == Commands.Translate)
                    {
                        if (responseComponents.TryGetValue(ResultValue, out string resultValue))
                        {
                            return(resultValue);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
            finally
            {
                _client.Close();
            }
            return(string.Empty);
        }