/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.util.ipc.loopback;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.util.ipc.IpcEndpoint;
import org.apache.ignite.internal.util.typedef.internal.U;
/**
* Loopback IPC endpoint based on socket.
*/
public class IpcClientTcpEndpoint implements IpcEndpoint {
/** Client socket. */
private Socket clientSock;
/**
* Creates connected client IPC endpoint.
*
* @param clientSock Connected client socket.
*/
public IpcClientTcpEndpoint(Socket clientSock) {
assert clientSock != null;
this.clientSock = clientSock;
}
/**
* Creates and connects client IPC endpoint.
*
* @param port Port.
* @param host Host.
* @throws IgniteCheckedException If connection fails.
*/
public IpcClientTcpEndpoint(String host, int port) throws IgniteCheckedException {
clientSock = new Socket();
try {
clientSock.connect(new InetSocketAddress(host, port));
}
catch (IOException e) {
throw new IgniteCheckedException("Failed to connect to endpoint [host=" + host + ", port=" + port + ']', e);
}
}
/** {@inheritDoc} */
@Override public InputStream inputStream() throws IgniteCheckedException {
try {
return clientSock.getInputStream();
}
catch (IOException e) {
throw new IgniteCheckedException(e);
}
}
/** {@inheritDoc} */
@Override public OutputStream outputStream() throws IgniteCheckedException {
try {
return clientSock.getOutputStream();
}
catch (IOException e) {
throw new IgniteCheckedException(e);
}
}
/** {@inheritDoc} */
@Override public void close() {
U.closeQuiet(clientSock);
}
}