Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.Communication.Operators/DataStreamCommunicator.cs @ 978

Last change on this file since 978 was 704, checked in by abeham, 16 years ago

[TICKET #297] communication restructuring

File size: 3.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Communication.Data;
30
31namespace HeuristicLab.Communication.Operators {
32  public class DataStreamCommunicator : CommunicatorBase {
33    public override string Description {
34      get {
35        return @"Sends a message if present and otherwise listens for incoming messages";
36      }
37    }
38
39    public DataStreamCommunicator() {
40      AddVariableInfo(new VariableInfo("DataStream", "", typeof(IDataStream), VariableKind.In));
41    }
42
43    private string Encode(Message message) {
44      XmlDocument document = new XmlDocument();
45      return message.GetXmlNode("Message", document, new Dictionary<Guid, IStorable>()).ToString();
46    }
47
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        }
64      }
65    }
66
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;
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.