[3883] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7268] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 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);
|
---|
[4722] | 107 | }
|
---|
| 108 | catch (SocketException) {
|
---|
[3890] | 109 | Close();
|
---|
[3895] | 110 | throw;
|
---|
[4722] | 111 | }
|
---|
| 112 | catch (ObjectDisposedException) {
|
---|
[3890] | 113 | socket = null;
|
---|
| 114 | Close();
|
---|
[3895] | 115 | throw;
|
---|
[3890] | 116 | }
|
---|
[3883] | 117 | }
|
---|
| 118 |
|
---|
[3890] | 119 | private byte[] EncodeDelimited(IMessage message) {
|
---|
| 120 | int messageSize = message.SerializedSize;
|
---|
| 121 | int headerSize = GetVarint32EncodedSize(messageSize);
|
---|
| 122 | byte[] buffer = new byte[headerSize + messageSize];
|
---|
| 123 | CodedOutputStream tmp = CodedOutputStream.CreateInstance(buffer);
|
---|
| 124 | tmp.WriteRawVarint32((uint)messageSize);
|
---|
| 125 | message.WriteTo(tmp);
|
---|
| 126 | return buffer;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | private int GetVarint32EncodedSize(int size) {
|
---|
| 130 | // Varints in Protocol Buffers are encoded using the 7 lower order bits (the MSB indicates continuation (=1) or termination (=0))
|
---|
| 131 | int sizeByteCount = 1;
|
---|
| 132 | int limit = 128;
|
---|
[3893] | 133 | while (size >= limit) {
|
---|
[3890] | 134 | sizeByteCount++;
|
---|
| 135 | limit *= 128;
|
---|
| 136 | }
|
---|
| 137 | return sizeByteCount;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[3883] | 140 | public override IMessage Receive(IBuilder builder) {
|
---|
[3890] | 141 | try {
|
---|
| 142 | byte[] buffer = GetMessageBuffer();
|
---|
| 143 | return builder.WeakMergeFrom(ByteString.CopyFrom(buffer)).WeakBuild();
|
---|
[4722] | 144 | }
|
---|
| 145 | catch (SocketException) {
|
---|
[3890] | 146 | Close();
|
---|
[3895] | 147 | throw;
|
---|
[4722] | 148 | }
|
---|
| 149 | catch (ObjectDisposedException) {
|
---|
[3890] | 150 | socket = null;
|
---|
| 151 | Close();
|
---|
[3895] | 152 | throw;
|
---|
[3890] | 153 | }
|
---|
[3883] | 154 | }
|
---|
| 155 |
|
---|
[3890] | 156 | private byte[] GetMessageBuffer() {
|
---|
| 157 | byte[] buffer;
|
---|
| 158 | int messageSize = GetReceivedMessageSize(out buffer);
|
---|
| 159 | int headerSize = GetVarint32EncodedSize(messageSize);
|
---|
| 160 | int messageBytesInHeader = Math.Min(messageSize, buffer.Length - headerSize);
|
---|
| 161 | byte[] messageBuffer = new byte[messageSize];
|
---|
| 162 | Array.Copy(buffer, headerSize, messageBuffer, 0, messageBytesInHeader);
|
---|
| 163 | ReceiveMessage(messageBuffer, messageBytesInHeader, messageSize - messageBytesInHeader);
|
---|
| 164 | return messageBuffer;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | private int GetReceivedMessageSize(out byte[] buffer) {
|
---|
| 168 | buffer = new byte[MAX_VARINT32_SIZE];
|
---|
| 169 | socket.Receive(buffer);
|
---|
| 170 | CodedInputStream tmp = CodedInputStream.CreateInstance(buffer);
|
---|
| 171 | return (int)tmp.ReadRawVarint32();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private void ReceiveMessage(byte[] buffer, int offset, int size) {
|
---|
| 175 | while (size > 0) {
|
---|
| 176 | int received = socket.Receive(buffer, offset, size, SocketFlags.None);
|
---|
| 177 | offset += received;
|
---|
| 178 | size -= received;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[3883] | 182 | public override void Close() {
|
---|
[3890] | 183 | if (socket != null) {
|
---|
[3895] | 184 | try {
|
---|
| 185 | if (socket.Connected)
|
---|
| 186 | socket.Disconnect(false);
|
---|
| 187 | socket.Close();
|
---|
[4722] | 188 | }
|
---|
| 189 | catch { }
|
---|
[3890] | 190 | socket = null;
|
---|
| 191 | }
|
---|
| 192 | bool wasInitialized = IsInitialized;
|
---|
[3883] | 193 | base.Close();
|
---|
[3890] | 194 | if (wasInitialized) OnDiconnected();
|
---|
[3883] | 195 | }
|
---|
| 196 |
|
---|
| 197 | #endregion
|
---|
[3890] | 198 |
|
---|
[6470] | 199 | #region Auxiliary Methods
|
---|
| 200 | private void UpdateName() {
|
---|
| 201 | name = string.Format("TCPChannel {0}:{1}", ipAddress, port);
|
---|
| 202 | OnNameChanged();
|
---|
| 203 | }
|
---|
| 204 | #endregion
|
---|
| 205 |
|
---|
[3890] | 206 | #region Events
|
---|
| 207 | public event EventHandler IpAddressChanged;
|
---|
| 208 | protected void OnIpAddressChanged() {
|
---|
| 209 | EventHandler handler = IpAddressChanged;
|
---|
| 210 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 211 | }
|
---|
| 212 | public event EventHandler PortChanged;
|
---|
| 213 | protected void OnPortChanged() {
|
---|
| 214 | EventHandler handler = PortChanged;
|
---|
| 215 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 216 | }
|
---|
| 217 | public event EventHandler Connected;
|
---|
| 218 | protected void OnConnected() {
|
---|
| 219 | EventHandler handler = Connected;
|
---|
| 220 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 221 | }
|
---|
| 222 | public event EventHandler Disconnected;
|
---|
| 223 | protected void OnDiconnected() {
|
---|
| 224 | EventHandler handler = Disconnected;
|
---|
| 225 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 226 | }
|
---|
| 227 | #endregion
|
---|
[3883] | 228 | }
|
---|
| 229 | }
|
---|