Skip to content

Commit

Permalink
Use TCP configuration options for connection timeout and retry attemp…
Browse files Browse the repository at this point in the history
…ts (#269)

* Use config timeout and reconnect attempts

* Update TCPCommProvider.java

* Update TCPCommProvider.java
  • Loading branch information
earocorn authored Sep 13, 2024
1 parent c369b62 commit bddb337
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,33 +70,35 @@ public OutputStream getOutputStream() throws IOException
protected void doStart() throws SensorHubException
{
TCPConfig config = this.config.protocol;

try
{
InetAddress addr = InetAddress.getByName(config.remoteHost);

if (config.enableTLS)
{
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
socket = factory.createSocket(addr, config.remotePort);
((SSLSocket)socket).startHandshake();
is = socket.getInputStream();
os = socket.getOutputStream();
}
else
{
SocketAddress endpoint = new InetSocketAddress(addr, config.remotePort);
socket = new Socket();
socket.connect(endpoint, 1000);
is = socket.getInputStream();
os = socket.getOutputStream();

int count = 0;
int retryAttempts = this.config.connection.reconnectAttempts;
// boolean isRetrying = retryAttempts >= 0;
while(true) {
try {
InetAddress addr = InetAddress.getByName(config.remoteHost);

if (config.enableTLS) {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = factory.createSocket(addr, config.remotePort);
((SSLSocket) socket).startHandshake();
is = socket.getInputStream();
os = socket.getOutputStream();
} else {
SocketAddress endpoint = new InetSocketAddress(addr, config.remotePort);
socket = new Socket();
socket.connect(endpoint, this.config.connection.connectTimeout);
is = socket.getInputStream();
os = socket.getOutputStream();
// isRetrying = false;
break;
}
} catch (IOException e) {
if(++count >= retryAttempts)
throw new SensorHubException("Cannot connect to remote host "
+ config.remoteHost + ":" + config.remotePort + " via TCP", e);
}
}
catch (IOException e)
{
throw new SensorHubException("Cannot connect to remote host "
+ config.remoteHost + ":" + config.remotePort + " via TCP", e);
}
}


Expand Down

0 comments on commit bddb337

Please sign in to comment.