Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationTCPChannel.cs @ 3883

Last change on this file since 3883 was 3883, checked in by abeham, 14 years ago

#866

  • Added tcp channel for communicating messages
  • Removed unnecessary enabled propagation in SolutionMessageBuilderView
File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Net;
24using System.Net.Sockets;
25using Google.ProtocolBuffers;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.ExternalEvaluation {
31  [Item("EvaluationTCPChannel", "A channel that creates a TCP connection over a network.")]
32  [StorableClass]
33  public class EvaluationTCPChannel : EvaluationChannel {
34    [Storable]
35    private string ipAddress;
36    [Storable]
37    private int port;
38    private Socket socket;
39
40    public EvaluationTCPChannel() : this(String.Empty, 0) { }
41    public EvaluationTCPChannel(string ip, int port)
42      : base() {
43      this.ipAddress = ip;
44      this.port = port;
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      EvaluationTCPChannel clone = (EvaluationTCPChannel)base.Clone(cloner);
49      clone.ipAddress = ipAddress;
50      clone.port = port;
51      return clone;
52    }
53
54    #region IExternalEvaluationChannel Members
55
56    public override void Open() {
57      socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
58      socket.Connect(IPAddress.Parse(ipAddress), port);
59      if (socket.Connected)
60        base.Open();
61    }
62
63    public override void Send(IMessage message) {
64      int size = message.SerializedSize;
65      socket.Send(System.BitConverter.GetBytes(size));
66      socket.Send(message.ToByteArray());
67    }
68
69    public override IMessage Receive(IBuilder builder) {
70      byte[] sizeBuffer = new byte[4];
71      socket.Receive(sizeBuffer);
72      int size = System.BitConverter.ToInt32(sizeBuffer, 0);
73      byte[] messageBuffer = new byte[size];
74      int offset = 0;
75      while (offset < size)
76        offset += socket.Receive(messageBuffer, offset, size, SocketFlags.None);
77      return builder.WeakMergeFrom(ByteString.CopyFrom(messageBuffer)).WeakBuild();
78    }
79
80    public override void Close() {
81      socket.Disconnect(false);
82      socket.Close();
83      base.Close();
84    }
85
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.