[591] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[584] | 23 | using System.Collections.Generic;
|
---|
[704] | 24 | using System.IO;
|
---|
[584] | 25 | using System.Text;
|
---|
[704] | 26 | using System.Xml;
|
---|
[584] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Communication.Data;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Communication.Operators {
|
---|
[704] | 32 | public class DataStreamCommunicator : CommunicatorBase {
|
---|
[584] | 33 | public override string Description {
|
---|
| 34 | get {
|
---|
[704] | 35 | return @"Sends a message if present and otherwise listens for incoming messages";
|
---|
[584] | 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public DataStreamCommunicator() {
|
---|
| 40 | AddVariableInfo(new VariableInfo("DataStream", "", typeof(IDataStream), VariableKind.In));
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[704] | 43 | private string Encode(Message message) {
|
---|
| 44 | XmlDocument document = new XmlDocument();
|
---|
| 45 | return message.GetXmlNode("Message", document, new Dictionary<Guid, IStorable>()).ToString();
|
---|
| 46 | }
|
---|
[584] | 47 |
|
---|
[704] | 48 | private Message Decode(string m) {
|
---|
| 49 | XmlDocument document = new XmlDocument();
|
---|
| 50 | document.LoadXml(m);
|
---|
| 51 | Message message = new Message();
|
---|
| 52 | message.Populate(document.SelectSingleNode("Message"), new Dictionary<Guid, IStorable>());
|
---|
| 53 | return message;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | protected override void Send(IScope scope, Protocol protocol, ProtocolState currentState, Message message) {
|
---|
| 57 | IDataStream connection = scope.GetVariableValue<IDataStream>("DataStream", true);
|
---|
| 58 | connection.Write("PROTOCOL_ID " + protocol.Name);
|
---|
| 59 | if (connection.Read().Equals("ACK")) {
|
---|
| 60 | connection.Write("STATE_ID " + currentState.Name);
|
---|
| 61 | if (connection.Read().Equals("ACK")) {
|
---|
| 62 | connection.Write(Encode(message));
|
---|
| 63 | }
|
---|
[584] | 64 | }
|
---|
[704] | 65 | }
|
---|
[584] | 66 |
|
---|
[704] | 67 | protected override Message Receive(IScope scope, Protocol protocol, ProtocolState currentState) {
|
---|
| 68 | IDataStream connection = scope.GetVariableValue<IDataStream>("DataStream", true);
|
---|
| 69 | Message message = new Message();
|
---|
| 70 | string rcvd = connection.Read();
|
---|
| 71 | if (rcvd.StartsWith("PROTOCOL_ID ")) {
|
---|
| 72 | if (rcvd.Substring(12).Equals(protocol.Name)) {
|
---|
| 73 | connection.Write("ACK");
|
---|
| 74 | rcvd = connection.Read();
|
---|
| 75 | if (rcvd.StartsWith("STATE_ID ")) {
|
---|
| 76 | if (rcvd.Substring(9).Equals(currentState.Name)) {
|
---|
| 77 | connection.Write("ACK");
|
---|
| 78 | message = Decode(connection.Read());
|
---|
| 79 | return message;
|
---|
| 80 | } else {
|
---|
| 81 | connection.Write("SYNCERROR STATE_ID");
|
---|
| 82 | return null;
|
---|
| 83 | }
|
---|
| 84 | } else {
|
---|
| 85 | connection.Write("ERROR");
|
---|
| 86 | return null;
|
---|
| 87 | }
|
---|
| 88 | } else {
|
---|
| 89 | connection.Write("SYNCERROR PROTOCOL_ID");
|
---|
| 90 | return null;
|
---|
| 91 | }
|
---|
| 92 | } else {
|
---|
| 93 | connection.Write("ERROR");
|
---|
| 94 | return null;
|
---|
[584] | 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|