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 abstract class CommunicatorBase : OperatorBase {
|
---|
10 | public CommunicatorBase()
|
---|
11 | : base() {
|
---|
12 | AddVariableInfo(new VariableInfo("PeerName", "", typeof(StringData), VariableKind.In));
|
---|
13 | AddVariableInfo(new VariableInfo("Protocol", "", typeof(Protocol), VariableKind.In));
|
---|
14 | AddVariableInfo(new VariableInfo("CurrentState", "", typeof(ProtocolState), VariableKind.In));
|
---|
15 | AddVariableInfo(new VariableInfo("Message", "The message to be sent and/or received. If it is not present the operator will listen for incomming messages.", typeof(Message), VariableKind.New | VariableKind.In | VariableKind.Out | VariableKind.Deleted));
|
---|
16 | }
|
---|
17 |
|
---|
18 | public override IOperation Apply(IScope scope) {
|
---|
19 | Protocol protocol = GetVariableValue<Protocol>("Protocol", scope, true);
|
---|
20 | ProtocolState currentState = GetVariableValue<ProtocolState>("CurrentState", scope, true);
|
---|
21 |
|
---|
22 | IVariableInfo info = GetVariableInfo("Message");
|
---|
23 | string actualName = info.ActualName;
|
---|
24 | if (!info.Local)
|
---|
25 | actualName = scope.TranslateName(info.FormalName);
|
---|
26 |
|
---|
27 | string peerName = GetVariableValue<StringData>("PeerName", scope, true).Data;
|
---|
28 | Message message = GetVariableValue<Message>("Message", scope, false, false);
|
---|
29 |
|
---|
30 | if (message == null) { // RECEIVE MODE (no message present or not from this source)
|
---|
31 | message = Receive(scope, protocol, currentState);
|
---|
32 | if (!info.Local) scope.AddVariable(new Variable(actualName, message));
|
---|
33 | else AddVariable(new Variable(actualName, message));
|
---|
34 | } else { // SEND MODE (message present)
|
---|
35 | if (message.Source.Equals(peerName)) {
|
---|
36 | // originating here, send it
|
---|
37 | Send(scope, protocol, currentState, message);
|
---|
38 | if (!info.Local) scope.RemoveVariable(actualName);
|
---|
39 | else RemoveVariable(actualName);
|
---|
40 | // after sending the message if there is something to be received, apply this operator again
|
---|
41 | if (currentState.Expect.Count > 0) return new AtomicOperation(this, scope);
|
---|
42 | } else throw new ArgumentException("ERROR in Communicator: Message to be sent does not originate from this peer");
|
---|
43 | }
|
---|
44 | return null;
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected abstract void Send(IScope scope, Protocol protocol, ProtocolState currentState, Message message);
|
---|
48 | protected abstract Message Receive(IScope scope, Protocol protocol, ProtocolState currentState);
|
---|
49 | }
|
---|
50 | }
|
---|