|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appulse.epmd.java.client; |
| 18 | + |
| 19 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 20 | +import static lombok.AccessLevel.PRIVATE; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.Closeable; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.InetAddress; |
| 26 | +import java.net.InetSocketAddress; |
| 27 | +import java.net.Socket; |
| 28 | + |
| 29 | +import io.appulse.epmd.java.client.exception.EpmdConnectionException; |
| 30 | +import io.appulse.epmd.java.core.mapper.deserializer.MessageDeserializer; |
| 31 | +import io.appulse.epmd.java.core.mapper.serializer.MessageSerializer; |
| 32 | +import io.appulse.epmd.java.core.model.response.RegistrationResult; |
| 33 | + |
| 34 | +import lombok.NonNull; |
| 35 | +import lombok.RequiredArgsConstructor; |
| 36 | +import lombok.SneakyThrows; |
| 37 | +import lombok.experimental.FieldDefaults; |
| 38 | +import lombok.extern.slf4j.Slf4j; |
| 39 | +import lombok.val; |
| 40 | + |
| 41 | +/** |
| 42 | + * |
| 43 | + * @author Artem Labazin |
| 44 | + * @since 0.2.1 |
| 45 | + */ |
| 46 | +@Slf4j |
| 47 | +@RequiredArgsConstructor |
| 48 | +@FieldDefaults(level = PRIVATE, makeFinal = true) |
| 49 | +class Connection implements Closeable { |
| 50 | + |
| 51 | + private static final int CONNECT_TIMEOUT; |
| 52 | + |
| 53 | + private static final MessageSerializer SERIALIZER; |
| 54 | + |
| 55 | + private static final MessageDeserializer DESERIALIZER; |
| 56 | + |
| 57 | + static { |
| 58 | + CONNECT_TIMEOUT = (int) SECONDS.toMillis(5); |
| 59 | + SERIALIZER = new MessageSerializer(); |
| 60 | + DESERIALIZER = new MessageDeserializer(); |
| 61 | + } |
| 62 | + |
| 63 | + @NonNull |
| 64 | + InetAddress address; |
| 65 | + |
| 66 | + int port; |
| 67 | + |
| 68 | + Socket socket = new Socket(); |
| 69 | + |
| 70 | + @SneakyThrows |
| 71 | + public void send (@NonNull Object request) { |
| 72 | + log.debug("Sending: {}", request); |
| 73 | + |
| 74 | + val bytes = SERIALIZER.serialize(request); |
| 75 | + |
| 76 | + connect(); |
| 77 | + |
| 78 | + socket.getOutputStream().write(bytes); |
| 79 | + socket.getOutputStream().flush(); |
| 80 | + |
| 81 | + log.debug("Message {} was sent", request); |
| 82 | + } |
| 83 | + |
| 84 | + public <T> T send (@NonNull Object request, @NonNull Class<T> responseType) throws IOException { |
| 85 | + send(request); |
| 86 | + |
| 87 | + val messageBytes = read(responseType); |
| 88 | + |
| 89 | + log.debug("Received bytes:\n{}", messageBytes); |
| 90 | + val result = DESERIALIZER.deserialize(messageBytes, responseType); |
| 91 | + |
| 92 | + log.debug("Received message:\n{}", result); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + @SneakyThrows |
| 98 | + public void close () { |
| 99 | + if (socket.isClosed()) { |
| 100 | + log.debug("EPMD connection was already closed"); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + socket.close(); |
| 105 | + |
| 106 | + log.debug("EPMD connection (from localhost:{}, to {}) was closed", |
| 107 | + socket.getLocalPort(), socket.getRemoteSocketAddress()); |
| 108 | + } |
| 109 | + |
| 110 | + public boolean isClosed () { |
| 111 | + return socket.isClosed(); |
| 112 | + } |
| 113 | + |
| 114 | + private void connect () { |
| 115 | + if (socket.isConnected()) { |
| 116 | + log.debug("EPMD connection was already connected"); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + val socketAddress = new InetSocketAddress(address, port); |
| 121 | + try { |
| 122 | + socket.setTcpNoDelay(true); |
| 123 | + socket.connect(socketAddress, CONNECT_TIMEOUT); |
| 124 | + } catch (IOException ex) { |
| 125 | + val message = String.format("Couldn't connect to EPMD server (%s:%d), maybe it is down", |
| 126 | + address.toString(), port); |
| 127 | + log.error(message); |
| 128 | + throw new EpmdConnectionException(message, ex); |
| 129 | + } |
| 130 | + |
| 131 | + log.debug("EPMD connection to {}:{} was established", address, port); |
| 132 | + } |
| 133 | + |
| 134 | + private byte[] read (Class<?> responseType) throws IOException { |
| 135 | + val outputStream = new ByteArrayOutputStream(32); |
| 136 | + val buffer = new byte[32]; |
| 137 | + |
| 138 | + while (true) { |
| 139 | + val length = socket.getInputStream().read(buffer); |
| 140 | + if (length == -1) { |
| 141 | + break; |
| 142 | + } |
| 143 | + outputStream.write(buffer, 0, length); |
| 144 | + if (responseType == RegistrationResult.class) { |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return outputStream.toByteArray(); |
| 150 | + } |
| 151 | +} |
0 commit comments