Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Operators/3.2/DataStreamCommunicator.cs @ 1529

Last change on this file since 1529 was 1529, checked in by gkronber, 15 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 3.7 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>()).OuterXml;
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      string s = connection.Read();
60      if (s.Trim().Equals("ACK")) {
61        connection.Write("STATE_ID " + currentState.Name);
62        s = connection.Read();
63        if (s.Trim().Equals("ACK")) {
64          connection.Write(Encode(message));
65        } else throw new InvalidOperationException("Received unexpected response [2]: " + s);
66      } else throw new InvalidOperationException("Received unexpected response [1]: " + s);
67    }
68
69    protected override Message Receive(IScope scope, Protocol protocol, ProtocolState currentState) {
70      IDataStream connection = scope.GetVariableValue<IDataStream>("DataStream", true);
71      Message message = new Message();
72      string rcvd = connection.Read();
73      if (rcvd.StartsWith("PROTOCOL_ID ")) {
74        if (rcvd.Substring(12).Trim().Equals(protocol.Name)) {
75          connection.Write("ACK");
76          rcvd = connection.Read();
77          if (rcvd.StartsWith("STATE_ID ")) {
78            if (rcvd.Substring(9).Trim().Equals(currentState.Name)) {
79              connection.Write("ACK");
80              message = Decode(connection.Read());
81              return message;
82            } else {
83              connection.Write("SYNCERROR STATE_ID");
84              return null;
85            }
86          } else {
87            connection.Write("ERROR");
88            return null;
89          }
90        } else {
91          connection.Write("SYNCERROR PROTOCOL_ID");
92          return null;
93        }
94      } else {
95        connection.Write("ERROR");
96        return null;
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.