Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Communication.Operators/DataStreamCommunicator.cs @ 583

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

Adding communication framework to branch 3.1 (ticket #278)

File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Communication.Data;
7
8namespace HeuristicLab.Communication.Operators {
9  public class DataStreamCommunicator : OperatorBase {
10    public override string Description {
11      get {
12        return @"TODO";
13      }
14    }
15
16    public DataStreamCommunicator() {
17      AddVariableInfo(new VariableInfo("CurrentState", "", typeof(ProtocolState), VariableKind.In));
18      AddVariableInfo(new VariableInfo("DataStream", "", typeof(IDataStream), VariableKind.In));
19      AddVariableInfo(new VariableInfo("Message", "", typeof(StringData), VariableKind.New | VariableKind.Deleted));
20    }
21
22    public override IOperation Apply(IScope scope) {
23      ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
24      IDataStream connection = GetVariableValue<IDataStream>("DataStream", scope, true);
25      IVariableInfo info = GetVariableInfo("Message");
26      string actualName = "";
27      if (!info.Local)
28        actualName = scope.TranslateName(info.FormalName);
29
30      if (currentState.SendingData.Count > 0) {
31        string toSend = GetVariableValue<StringData>(info.FormalName, scope, false).Data;
32        connection.Write(toSend);
33        if (info.Local) RemoveVariable(info.ActualName);
34        else scope.RemoveVariable(actualName);
35      }
36
37      if (currentState.ReceivingData.Count > 0) {
38        string received = connection.Read();
39        if (info.Local) AddVariable(new Variable(info.ActualName, new StringData(received)));
40        else scope.AddVariable(new Variable(actualName, new StringData(received)));
41      }
42      return null;
43    }
44  }
45}
Note: See TracBrowser for help on using the repository browser.