Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.7 KB
RevLine 
[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
22using System;
[584]23using System.Collections.Generic;
[704]24using System.IO;
[584]25using System.Text;
[704]26using System.Xml;
[584]27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Communication.Data;
30
31namespace 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();
[1207]45      return message.GetXmlNode("Message", document, new Dictionary<Guid, IStorable>()).OuterXml;
[704]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);
[1207]59      string s = connection.Read();
60      if (s.Trim().Equals("ACK")) {
[704]61        connection.Write("STATE_ID " + currentState.Name);
[1207]62        s = connection.Read();
63        if (s.Trim().Equals("ACK")) {
[704]64          connection.Write(Encode(message));
[1207]65        } else throw new InvalidOperationException("Received unexpected response [2]: " + s);
66      } else throw new InvalidOperationException("Received unexpected response [1]: " + s);
[704]67    }
[584]68
[704]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 ")) {
[1207]74        if (rcvd.Substring(12).Trim().Equals(protocol.Name)) {
[704]75          connection.Write("ACK");
76          rcvd = connection.Read();
77          if (rcvd.StartsWith("STATE_ID ")) {
[1207]78            if (rcvd.Substring(9).Trim().Equals(currentState.Name)) {
[704]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;
[584]97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.