Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
352 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tools/src/main/java/com/navercorp/pinpoint/tools/network/AbstractNetworkChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.navercorp.pinpoint.tools.network; | ||
|
||
import com.navercorp.pinpoint.tools.utils.HostResolver; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public abstract class AbstractNetworkChecker implements NetworkChecker { | ||
|
||
private static final String WHITE_SPACE = " "; // 4space | ||
private static final String LINE_SEPARATOR = "\r\n"; | ||
|
||
private final String testName; | ||
|
||
private final InetSocketAddress hostAddress; | ||
private final List<InetSocketAddress> ipAddressList; | ||
|
||
public AbstractNetworkChecker(String testName, InetSocketAddress hostAddress) throws UnknownHostException { | ||
this.testName = testName; | ||
this.hostAddress = hostAddress; | ||
this.ipAddressList = HostResolver.getIPList(hostAddress); | ||
} | ||
|
||
protected abstract boolean check(InetSocketAddress address) throws IOException; | ||
|
||
protected abstract boolean check(InetSocketAddress address, byte[] requestData, byte[] expectedResponseData) throws IOException; | ||
|
||
@Override | ||
public void check() throws IOException { | ||
StringBuilder report = new StringBuilder(32); | ||
|
||
String hostName = getHostName(hostAddress); | ||
report.append(testName).append(":// ").append(hostName).append(LINE_SEPARATOR); | ||
|
||
for (InetSocketAddress ipAddress : ipAddressList) { | ||
boolean check = check(ipAddress); | ||
report.append(createReport(ipAddress, check)); | ||
} | ||
|
||
System.out.println(report.toString()); | ||
|
||
} | ||
|
||
@Override | ||
public void check(byte[] requestData, byte[] expectedResponseData) throws IOException { | ||
StringBuilder report = new StringBuilder(32); | ||
|
||
String hostName = getHostName(hostAddress); | ||
report.append(testName).append(":// ").append(hostName).append(LINE_SEPARATOR); | ||
|
||
for (InetSocketAddress ipAddress : ipAddressList) { | ||
boolean check = check(ipAddress, requestData, expectedResponseData); | ||
report.append(createReport(ipAddress, check)); | ||
} | ||
|
||
System.out.println(report.toString()); | ||
} | ||
|
||
private String getHostName(InetSocketAddress hostAddress) { | ||
String hostName = hostAddress.getHostName(); | ||
return hostName; | ||
} | ||
|
||
private String createReport(InetSocketAddress socketAddress, boolean check) { | ||
String ip = getIp(socketAddress, socketAddress.getHostName()); | ||
int port = socketAddress.getPort(); | ||
|
||
StringBuilder report = new StringBuilder(); | ||
report.append(WHITE_SPACE).append("=> ").append(ip).append(":").append(port); | ||
report.append(" [").append(check ? "SUCCESS" : "FAIL").append("]").append(LINE_SEPARATOR); | ||
return report.toString(); | ||
} | ||
|
||
protected String getIp(InetSocketAddress socketAddress, String defaultValue) { | ||
InetAddress address = socketAddress.getAddress(); | ||
if (address != null) { | ||
return address.getHostAddress(); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
tools/src/main/java/com/navercorp/pinpoint/tools/network/NetworkChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.navercorp.pinpoint.tools.network; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public interface NetworkChecker { | ||
|
||
void check() throws IOException; | ||
|
||
void check(byte[] requestData, byte[] expectedResponseData) throws IOException; | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
tools/src/main/java/com/navercorp/pinpoint/tools/network/TCPChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.navercorp.pinpoint.tools.network; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* @author Taejin Koo | ||
*/ | ||
public class TCPChecker extends AbstractNetworkChecker { | ||
|
||
public TCPChecker(String testName, String hostName, int port) throws UnknownHostException { | ||
this(testName, new InetSocketAddress(hostName, port)); | ||
} | ||
|
||
public TCPChecker(String testName, InetSocketAddress hostAddress) throws UnknownHostException { | ||
super(testName, hostAddress); | ||
} | ||
|
||
|
||
@Override | ||
protected boolean check(InetSocketAddress address) throws IOException { | ||
Socket socket = null; | ||
try { | ||
socket = createSocket(address); | ||
return socket.isConnected(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (socket != null) { | ||
socket.close(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean check(InetSocketAddress address, byte[] requestData, byte[] expectedResponseData) throws IOException { | ||
Socket socket = null; | ||
try { | ||
socket = createSocket(address); | ||
|
||
write(socket, requestData); | ||
byte[] responseData = read(socket, 100); | ||
|
||
return Arrays.equals(expectedResponseData, responseData); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (socket != null) { | ||
socket.close(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private Socket createSocket(InetSocketAddress socketAddress) throws IOException { | ||
Socket socket = new Socket(); | ||
socket.connect(socketAddress); | ||
socket.setSoTimeout(3000); | ||
return socket; | ||
} | ||
|
||
private void write(Socket socket, byte[] requestData) throws IOException { | ||
OutputStream outputStream = socket.getOutputStream(); | ||
outputStream.write(requestData); | ||
outputStream.flush(); | ||
} | ||
|
||
private byte[] read(Socket socket, int readSize) throws IOException { | ||
byte[] buf = new byte[readSize]; | ||
|
||
InputStream inputStream = socket.getInputStream(); | ||
inputStream.read(buf); | ||
|
||
return buf; | ||
} | ||
|
||
} |
Oops, something went wrong.