[3883] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3883] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Net;
|
---|
| 24 | using System.Net.Sockets;
|
---|
| 25 | using Google.ProtocolBuffers;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
| 31 | [Item("EvaluationTCPChannel", "A channel that creates a TCP connection over a network.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class EvaluationTCPChannel : EvaluationChannel {
|
---|
[6470] | 34 |
|
---|
[3890] | 35 | public const int MAX_VARINT32_SIZE = 5;
|
---|
| 36 |
|
---|
[6470] | 37 | #region Fields & Properties
|
---|
[3883] | 38 | [Storable]
|
---|
| 39 | private string ipAddress;
|
---|
[3890] | 40 | public string IpAddress {
|
---|
| 41 | get { return ipAddress; }
|
---|
| 42 | set {
|
---|
[6470] | 43 | if (value == ipAddress) return;
|
---|
[3890] | 44 | ipAddress = value;
|
---|
[6470] | 45 | UpdateName();
|
---|
| 46 | OnIpAddressChanged();
|
---|
[3890] | 47 | }
|
---|
| 48 | }
|
---|
[3883] | 49 | [Storable]
|
---|
| 50 | private int port;
|
---|
[3890] | 51 | public int Port {
|
---|
| 52 | get { return port; }
|
---|
| 53 | set {
|
---|
[6470] | 54 | if (value == port) return;
|
---|
[3890] | 55 | port = value;
|
---|
[6470] | 56 | UpdateName();
|
---|
| 57 | OnPortChanged();
|
---|
[3890] | 58 | }
|
---|
| 59 | }
|
---|
[3883] | 60 | private Socket socket;
|
---|
[6470] | 61 | #endregion
|
---|
[3883] | 62 |
|
---|
[6470] | 63 | #region Construction & Cloning
|
---|
[4722] | 64 | [StorableConstructor]
|
---|
| 65 | protected EvaluationTCPChannel(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected EvaluationTCPChannel(EvaluationTCPChannel original, Cloner cloner)
|
---|
| 67 | : base(original, cloner) {
|
---|
| 68 | ipAddress = original.ipAddress;
|
---|
| 69 | port = original.port;
|
---|
[6470] | 70 | UpdateName();
|
---|
[4722] | 71 | }
|
---|
[6470] | 72 |
|
---|
[4722] | 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new EvaluationTCPChannel(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3883] | 77 | public EvaluationTCPChannel() : this(String.Empty, 0) { }
|
---|
| 78 | public EvaluationTCPChannel(string ip, int port)
|
---|
| 79 | : base() {
|
---|
| 80 | this.ipAddress = ip;
|
---|
| 81 | this.port = port;
|
---|
[6470] | 82 | UpdateName();
|
---|
[3883] | 83 | }
|
---|
[6470] | 84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 85 | private void AfterDeserialization() {
|
---|
| 86 | UpdateName();
|
---|
| 87 | }
|
---|
| 88 | #endregion
|
---|
[3883] | 89 |
|
---|
[6470] | 90 |
|
---|
[8298] | 91 |
|
---|
[3883] | 92 | #region IExternalEvaluationChannel Members
|
---|
| 93 |
|
---|
| 94 | public override void Open() {
|
---|
| 95 | socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
---|
| 96 | socket.Connect(IPAddress.Parse(ipAddress), port);
|
---|
[3890] | 97 | if (socket.Connected) {
|
---|
[3883] | 98 | base.Open();
|
---|
[3890] | 99 | OnConnected();
|
---|
| 100 | }
|
---|
[3883] | 101 | }
|
---|
| 102 |
|
---|
| 103 | public override void Send(IMessage message) {
|
---|
[3890] | 104 | try {
|
---|
| 105 | byte[] buffer = EncodeDelimited(message);
|
---|
| 106 | socket.Send(buffer);
|
---|
[8298] | 107 | } catch (SocketException) {
|
---|
[3890] | 108 | Close();
|
---|
[3895] | 109 | throw;
|
---|
[8298] | 110 | } catch (ObjectDisposedException) {
|
---|
[3890] | 111 | socket = null;
|
---|
| 112 | Close();
|
---|
[3895] | 113 | throw;
|
---|
[3890] | 114 | }
|
---|
[3883] | 115 | }
|
---|
| 116 |
|
---|
[3890] | 117 | private byte[] EncodeDelimited(IMessage message) {
|
---|
| 118 | int messageSize = message.SerializedSize;
|
---|
| 119 | int headerSize = GetVarint32EncodedSize(messageSize);
|
---|
| 120 | byte[] buffer = new byte[headerSize + messageSize];
|
---|
| 121 | CodedOutputStream tmp = CodedOutputStream.CreateInstance(buffer);
|
---|
| 122 | tmp.WriteRawVarint32((uint)messageSize);
|
---|
| 123 | message.WriteTo(tmp);
|
---|
| 124 | return buffer;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private int GetVarint32EncodedSize(int size) {
|
---|
| 128 | // Varints in Protocol Buffers are encoded using the 7 lower order bits (the MSB indicates continuation (=1) or termination (=0))
|
---|
| 129 | int sizeByteCount = 1;
|
---|
| 130 | int limit = 128;
|
---|
[3893] | 131 | while (size >= limit) {
|
---|
[3890] | 132 | sizeByteCount++;
|
---|
| 133 | limit *= 128;
|
---|
| 134 | }
|
---|
| 135 | return sizeByteCount;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[8298] | 138 | public override IMessage Receive(IBuilder builder, ExtensionRegistry extensions) {
|
---|
[3890] | 139 | try {
|
---|
| 140 | byte[] buffer = GetMessageBuffer();
|
---|
[8298] | 141 | return builder.WeakMergeFrom(ByteString.CopyFrom(buffer), extensions).WeakBuild();
|
---|
| 142 | } catch (SocketException) {
|
---|
[3890] | 143 | Close();
|
---|
[3895] | 144 | throw;
|
---|
[8298] | 145 | } catch (ObjectDisposedException) {
|
---|
[3890] | 146 | socket = null;
|
---|
| 147 | Close();
|
---|
[3895] | 148 | throw;
|
---|
[3890] | 149 | }
|
---|
[3883] | 150 | }
|
---|
| 151 |
|
---|
[3890] | 152 | private byte[] GetMessageBuffer() {
|
---|
| 153 | byte[] buffer;
|
---|
| 154 | int messageSize = GetReceivedMessageSize(out buffer);
|
---|
| 155 | int headerSize = GetVarint32EncodedSize(messageSize);
|
---|
| 156 | int messageBytesInHeader = Math.Min(messageSize, buffer.Length - headerSize);
|
---|
| 157 | byte[] messageBuffer = new byte[messageSize];
|
---|
| 158 | Array.Copy(buffer, headerSize, messageBuffer, 0, messageBytesInHeader);
|
---|
| 159 | ReceiveMessage(messageBuffer, messageBytesInHeader, messageSize - messageBytesInHeader);
|
---|
| 160 | return messageBuffer;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private int GetReceivedMessageSize(out byte[] buffer) {
|
---|
| 164 | buffer = new byte[MAX_VARINT32_SIZE];
|
---|
| 165 | socket.Receive(buffer);
|
---|
| 166 | CodedInputStream tmp = CodedInputStream.CreateInstance(buffer);
|
---|
| 167 | return (int)tmp.ReadRawVarint32();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | private void ReceiveMessage(byte[] buffer, int offset, int size) {
|
---|
| 171 | while (size > 0) {
|
---|
| 172 | int received = socket.Receive(buffer, offset, size, SocketFlags.None);
|
---|
| 173 | offset += received;
|
---|
| 174 | size -= received;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[3883] | 178 | public override void Close() {
|
---|
[3890] | 179 | if (socket != null) {
|
---|
[3895] | 180 | try {
|
---|
| 181 | if (socket.Connected)
|
---|
| 182 | socket.Disconnect(false);
|
---|
| 183 | socket.Close();
|
---|
[8298] | 184 | } catch { }
|
---|
[3890] | 185 | socket = null;
|
---|
| 186 | }
|
---|
| 187 | bool wasInitialized = IsInitialized;
|
---|
[3883] | 188 | base.Close();
|
---|
[3890] | 189 | if (wasInitialized) OnDiconnected();
|
---|
[3883] | 190 | }
|
---|
| 191 |
|
---|
| 192 | #endregion
|
---|
[3890] | 193 |
|
---|
[6470] | 194 | #region Auxiliary Methods
|
---|
| 195 | private void UpdateName() {
|
---|
| 196 | name = string.Format("TCPChannel {0}:{1}", ipAddress, port);
|
---|
| 197 | OnNameChanged();
|
---|
| 198 | }
|
---|
| 199 | #endregion
|
---|
| 200 |
|
---|
[3890] | 201 | #region Events
|
---|
| 202 | public event EventHandler IpAddressChanged;
|
---|
| 203 | protected void OnIpAddressChanged() {
|
---|
| 204 | EventHandler handler = IpAddressChanged;
|
---|
| 205 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 206 | }
|
---|
| 207 | public event EventHandler PortChanged;
|
---|
| 208 | protected void OnPortChanged() {
|
---|
| 209 | EventHandler handler = PortChanged;
|
---|
| 210 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 211 | }
|
---|
| 212 | public event EventHandler Connected;
|
---|
| 213 | protected void OnConnected() {
|
---|
| 214 | EventHandler handler = Connected;
|
---|
| 215 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 216 | }
|
---|
| 217 | public event EventHandler Disconnected;
|
---|
| 218 | protected void OnDiconnected() {
|
---|
| 219 | EventHandler handler = Disconnected;
|
---|
| 220 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 221 | }
|
---|
| 222 | #endregion
|
---|
[3883] | 223 | }
|
---|
| 224 | }
|
---|