Example #1
0
 private void PrintStatusMessage(EPICommand cmd)
 {
     if (statusMessageWriter != null)
     {
         statusMessageWriter.WriteLine("Status: {0}", cmd[CommandFields.Responses.S.MessageText]);
     }
 }
Example #2
0
        public string DecodeCommand(EPICommand cmd)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException("cmd");
            }

            if (String.IsNullOrEmpty(cmd.CMDWord) || cmd.CMDWord.Length > 3)
            {
                throw new EPIException("epi command word is invalid", cmd);
            }

            StringBuilder line = new StringBuilder();

            line.AppendFormat("{0}{1}{2}", cmd.CMDWord, fldSep, cmd.ActionId);

            foreach (String field in cmd)
            {
                line.Append(fldSep);
                line.Append(field);
            }

            if (cmd.Completed)
            {
                line.Append(fldSep);
            }

            return(line.ToString());
        }
Example #3
0
        private void Enqueue(EPICommand cmd)
        {
            if (!channels.ContainsKey(cmd.ActionId))
            {
                throw new EPIException($"The response contains an unknown action id (${cmd.ActionId}) and can't assigned to a channel", cmd);
            }

            channels[cmd.ActionId].Enqueue(cmd);
        }
Example #4
0
        public string GetOptionValue(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            EPICommand sho = new CommandBuilder()
                             .SetCMDWord(CommandWords.Session.ShowOptions)
                             .SetActionId(ActionId)
                             .AddField(name)
                             .AddField("0") // mit Metadaten?
                             .Build();

            stream.Write(sho);
            if (stream.Read() != EPIResponseType.Data)
            {
                throw new EPIException("edp option could'nt be received", stream.ResultMessage);
            }

            bool   valueReaded = false;
            string value       = String.Empty;

            while (stream[ActionId].Count > 0)
            {
                EPICommand cmd = stream[ActionId].Dequeue();
                if (CommandWords.Responses.Data == cmd.CMDWord)
                {
                    if (valueReaded)
                    {
                        throw new EPIException("server responses with an unexpected data command", cmd);
                    }

                    if (cmd.Fields.Length < 1)
                    {
                        throw new EPIException("server responses with an empty data command", cmd);
                    }

                    value       = cmd[1];
                    valueReaded = true;
                }

                if (CommandWords.Responses.EndOfData == cmd.CMDWord)
                {
                    if (!valueReaded)
                    {
                        throw new EPIException("server response contains no data about the requested option value");
                    }

                    return(value);
                }
            }

            throw new EPIException("server response contains no data about the requested option value");
        }
Example #5
0
        private void HandleEPICommand(EPICommand cmd)
        {
            switch (cmd.CMDWord)
            {
            case CommandWords.Responses.StatusMessage:
                PrintStatusMessage(cmd);
                break;

            case CommandWords.Responses.Acknowledge:
                resultMessage = cmd;
                break;

            case CommandWords.Responses.NegativeAcknowledge:
                resultMessage = cmd;
                break;

            case CommandWords.Responses.BeginOfData:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.ChangeNotification:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.Data:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.DataContinuation:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.EndOfData:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.End:
                resultMessage = cmd;
                break;

            case CommandWords.Responses.MetaData:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.ProgressMessage:
                Enqueue(cmd);
                break;

            case CommandWords.Responses.Error:
                throw new EPIException("server responses an error: " + cmd[CommandFields.Responses.E.MessageText], cmd);
            }
        }
Example #6
0
        public static EPIResponseType GetTypeOf(EPICommand cmd)
        {
            if (cmd == null)
            {
                return(EPIResponseType.Undefined);
            }

            // Antworten wie Status oder Daten werden nie alleine gesendet, sondern es folgt
            // immer eine abschließende Antwort. Daher ist bei diesen Kommandos der Typ noch undefiniert.
            switch (cmd.CMDWord)
            {
            case CommandWords.Responses.StatusMessage:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.Acknowledge:
                return(EPIResponseType.Acknowledge);

            case CommandWords.Responses.NegativeAcknowledge:
                return(EPIResponseType.NegativeAcknowledge);

            case CommandWords.Responses.BeginOfData:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.ChangeNotification:
                return(EPIResponseType.ChangeNotification);

            case CommandWords.Responses.Data:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.DataContinuation:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.EndOfData:
                return(EPIResponseType.Data);

            case CommandWords.Responses.End:
                return(EPIResponseType.End);

            case CommandWords.Responses.MetaData:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.ProgressMessage:
                return(EPIResponseType.Undefined);

            case CommandWords.Responses.Error:
                return(EPIResponseType.Undefined);

            default:
                return(EPIResponseType.Undefined);
            }
        }
Example #7
0
        /// <summary>
        /// Sendet einen EPI-Kommando an den Server. Der Aufruf löscht die aktuelle Warteschlange der zuletzt empfangenen Nachrichten.
        /// </summary>
        /// <param name="cmd">EPI-Kommando</param>
        public void Write(EPICommand cmd)
        {
            if (!channels.ContainsKey(cmd.ActionId))
            {
                AddChannel(cmd.ActionId);
            }

            channels[cmd.ActionId].Clear();
            string request = DecodeCommand(cmd);

#if DEBUG
            Console.WriteLine("request: " + request);
#endif

            byte[] data = encoding.GetBytes(request);

            stream.Write(data, 0, data.Length);

            // linefeed senden zum Ausführen des Kommandos
            stream.Write(new byte[] { Convert.ToByte(endCommandSign) }, 0, 1);
        }
Example #8
0
        /// <summary>
        /// Öffnet die Verbindung über das Netzwerk und meldet sich am ERP-Server an.
        /// </summary>
        public void Open()
        {
            if (connected)
            {
                Close();
            }

            ResetActionId();

            try {
                client = new TcpClient(host, port);
            } catch (SocketException e) {
                throw new EPIException("connection has been refused", e);
            }

            stream = new EPIStream(client.GetStream())
            {
                Encoding       = System.Text.Encoding.GetEncoding(DefaultEDPEncoding),
                FieldSeparator = DefaultFieldSeparator,
                MaxBufferSize  = DefaultMaxBufferSize
            };

            // Server sendet als erstes eine Status-Message, welche abgeholt werden kann, wird sie nicht
            // abgeholt, wird sie mit dem nächsten Befehl empfangen
            EPICommand status = stream.ReadNextCommand();

            stream.Write(CreateChangeMandantCommand());
            if (stream.Read() != EPIResponseType.Acknowledge)
            {
                throw new EPIException("change mandant failed", stream.ResultMessage);
            }

            stream.Write(CreateLogOnCommand());
            if (stream.Read() != EPIResponseType.Acknowledge)
            {
                throw new EPIException("logon failed", stream.ResultMessage);
            }

            connected = true;
        }
Example #9
0
        /// <summary>
        /// Wartet bzw. liest solange EPI-Kommandos vom Server ein, bis eine abschließenden Antwort,
        /// z.B. ein Acknowledge, eintrifft und liefert das Ergebnis der Abfrage zurück.
        /// </summary>
        /// <returns>Abschließende Antwortyp des Servers</returns>
        public EPIResponseType Read()
        {
            EPIResponseType type = EPIResponseType.Undefined;

            resultMessage = CommandBuilder.CreateEmptyCommand();

            do
            {
                string response = ReadLine();

                // im Response können 1 bis n Nachrichten enthalten sein
                int offset = 0;
                int index  = -1;

                do
                {
                    index = response.IndexOf(endCommandSign, offset);

                    if (index >= 0)
                    {
                        EPICommand cmd = ReadResponse(response, offset, index);
                        HandleEPICommand(cmd);

                        if (type == EPIResponseType.Undefined)
                        {
                            type = EPIResponseTypeHelper.GetTypeOf(cmd);
                        }
                    }

                    offset = index + 1;
                } while (offset > 0 && offset < response.Length);

                // weiterlesen, wenn noch keine abschließende Antowort vom Server gesendet wurde
            } while (type == EPIResponseType.Undefined);

            return(type);
        }
Example #10
0
 /// <summary>
 /// Instanziert einen neuen EPI-Stream mit Standardeinstellungen.
 /// </summary>
 /// <param name="stream">Netzwerkstream, dieser muss vorher geöffnet werden. Geschlossen wird der Stream automatisch oder über Aufruf der Close-Methode.</param>
 public EPIStream(NetworkStream stream)
 {
     this.stream   = stream ?? throw new ArgumentNullException("stream");
     resultMessage = CommandBuilder.CreateEmptyCommand();
     channels      = new Dictionary <uint, Channel>();
 }
Example #11
0
 public EPIException(String message, EPICommand cmd, Exception inner) : base(message, inner)
 {
     this.Cmd = cmd;
 }
Example #12
0
 public EPIException(String message, EPICommand cmd) : base(message)
 {
     this.Cmd = cmd;
 }