forked from tango-controls/JTango
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
218 additions
and
52 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
41 changes: 41 additions & 0 deletions
41
common/src/main/java/org/tango/network/EndpointAvailabilityChecker.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,41 @@ | ||
package org.tango.network; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public class EndpointAvailabilityChecker implements Predicate<String> { | ||
private final Logger logger = LoggerFactory.getLogger(EndpointAvailabilityChecker.class); | ||
|
||
@Override | ||
public boolean test(String endpoint) { | ||
logger.debug("Check endpoint: {}", endpoint); | ||
URI uri = null; | ||
try { | ||
uri = new URI(endpoint); | ||
} catch (URISyntaxException e) { | ||
logger.debug("Bad endpoint: " + endpoint, e); | ||
return false; | ||
} | ||
|
||
// Try to connect | ||
InetSocketAddress ip = new InetSocketAddress(uri.getHost(), uri.getPort()); | ||
try (Socket socket = new Socket()) { | ||
socket.connect(ip, 10); | ||
return true; | ||
} catch (IOException e) { | ||
logger.debug("Failed to connect to " + ip, e); | ||
return false; | ||
} | ||
} | ||
} |
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,18 @@ | ||
package org.tango.transport; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
public interface Transport extends Closeable { | ||
boolean isConnected(); | ||
|
||
void connect(String endpoint) throws IOException; | ||
|
||
void disconnect(String endpoint) throws IOException; | ||
|
||
byte[] send(byte[] data) throws IOException; | ||
} |
43 changes: 43 additions & 0 deletions
43
common/src/main/java/org/tango/transport/TransportMeta.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,43 @@ | ||
package org.tango.transport; | ||
|
||
import com.google.common.collect.Lists; | ||
import fr.esrf.Tango.DevVarLongStringArray; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 18.02.2020 | ||
*/ | ||
public class TransportMeta { | ||
private List<String> endPoints = Lists.newArrayList(); | ||
|
||
public TransportMeta() { | ||
} | ||
|
||
public static TransportMeta fromDevVarLongStringArray(DevVarLongStringArray array) { | ||
TransportMeta transportMeta = new TransportMeta(); | ||
transportMeta.setEndPoints(Arrays.asList(array.svalue)); | ||
return transportMeta; | ||
} | ||
|
||
public List<String> getEndPoints() { | ||
return endPoints; | ||
} | ||
|
||
public void setEndPoints(List<String> endPoints) { | ||
this.endPoints = endPoints; | ||
} | ||
|
||
public void addConnectionPoint(String connectionPoint) { | ||
endPoints.add(connectionPoint); | ||
} | ||
|
||
public DevVarLongStringArray toDevVarLongStringArray() { | ||
DevVarLongStringArray result = new DevVarLongStringArray(new int[0], | ||
endPoints.toArray(new String[0])); | ||
|
||
return result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.tango.transport; | ||
|
||
import org.zeromq.ZContext; | ||
import org.zeromq.ZMQ; | ||
|
||
/** | ||
* @author Igor Khokhriakov <[email protected]> | ||
* @since 19.02.2020 | ||
*/ | ||
//@ThreadSafe | ||
public class ZmqTransport implements Transport { | ||
private final ZContext zcontext = new ZContext(); | ||
private ZMQ.Socket socket; | ||
|
||
@Override | ||
public synchronized boolean isConnected() { | ||
return socket != null; | ||
} | ||
|
||
@Override | ||
public synchronized void connect(String endpoint) { | ||
ZMQ.Socket socket = zcontext.createSocket(ZMQ.REQ); | ||
|
||
socket.connect(endpoint); | ||
|
||
this.socket = socket; | ||
} | ||
|
||
@Override | ||
public synchronized void disconnect(String endpoint) { | ||
zcontext.getSockets() | ||
.forEach(socket -> socket.disconnect(endpoint)); | ||
} | ||
|
||
@Override | ||
public synchronized byte[] send(byte[] data) { | ||
socket.send(data); | ||
|
||
return socket.recv(); | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
zcontext.close(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
server/src/main/java/org/tango/server/transport/TransportManager.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
29 changes: 0 additions & 29 deletions
29
server/src/main/java/org/tango/server/transport/TransportMeta.java
This file was deleted.
Oops, something went wrong.