1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Communication.Data;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|