Skip to content

Commit

Permalink
Add support for dynamic replacements
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed May 2, 2024
1 parent 87c073b commit 8e93ccf
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ private void scan() {
}

private void scanBroadcast(AddonInfo candidate, String request, String requestPlain, String response, int timeoutMs,
int destPort) {
int destPort) throws ParseException {
if (request.isEmpty() && requestPlain.isEmpty()) {
logger.warn("{}: match-property request and requestPlain \"{}\" is unknown", candidate.getUID(),
TYPE_IP_BROADCAST);
Expand All @@ -394,7 +394,8 @@ private void scanBroadcast(AddonInfo candidate, String request, String requestPl
try (DatagramSocket socket = new DatagramSocket()) {
socket.setBroadcast(true);
socket.setSoTimeout(timeoutMs);
byte[] sendBuffer = requestPlain.isEmpty() ? buildByteArray(request) : buildByteArrayPlain(requestPlain);
byte[] sendBuffer = requestPlain.isEmpty() ? buildRequestArray(socket.getLocalSocketAddress(), request)
: buildRequestArrayPlain(socket.getLocalSocketAddress(), requestPlain);
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length,
InetAddress.getByName(broadcastAddress), destPort);
socket.send(sendPacket);
Expand Down Expand Up @@ -433,11 +434,6 @@ private byte[] buildByteArray(String input) {
return requestFrame.toByteArray();
}

private byte[] buildByteArrayPlain(String input) {
String reqUnEscaped = StringUtils.unEscapeXml(input);
return reqUnEscaped != null ? reqUnEscaped.getBytes() : new byte[0];
}

private void scanMulticast(AddonInfo candidate, String request, String requestPlain, String response, int timeoutMs,
int listenPort, @Nullable InetAddress destIp, int destPort) throws ParseException {
List<String> ipAddresses = NetUtil.getAllInterfaceAddresses().stream()
Expand All @@ -449,8 +445,9 @@ private void scanMulticast(AddonInfo candidate, String request, String requestPl
.bind(new InetSocketAddress(localIp, listenPort))
.setOption(StandardSocketOptions.IP_MULTICAST_TTL, 64).configureBlocking(false);
Selector selector = Selector.open()) {
byte[] requestArray = "".equals(requestPlain) ? buildRequestArray(channel, Objects.toString(request))
: buildRequestArrayPlain(channel, Objects.toString(requestPlain));
byte[] requestArray = "".equals(requestPlain)
? buildRequestArray(channel.getLocalAddress(), Objects.toString(request))
: buildRequestArrayPlain(channel.getLocalAddress(), Objects.toString(requestPlain));
if (logger.isTraceEnabled()) {
InetSocketAddress sock = (InetSocketAddress) channel.getLocalAddress();
String id = candidate.getUID();
Expand Down Expand Up @@ -496,9 +493,9 @@ private void scanMulticast(AddonInfo candidate, String request, String requestPl
}

// build from plaintext string
private byte[] buildRequestArrayPlain(DatagramChannel channel, String request)
private byte[] buildRequestArrayPlain(SocketAddress address, String request)
throws java.io.IOException, ParseException {
InetSocketAddress sock = (InetSocketAddress) channel.getLocalAddress();
InetSocketAddress sock = (InetSocketAddress) address;

// replace first
StringBuilder req = new StringBuilder(request);
Expand All @@ -519,9 +516,8 @@ private byte[] buildRequestArrayPlain(DatagramChannel channel, String request)
}

// build from hex string
private byte[] buildRequestArray(DatagramChannel channel, String request)
throws java.io.IOException, ParseException {
InetSocketAddress sock = (InetSocketAddress) channel.getLocalAddress();
private byte[] buildRequestArray(SocketAddress address, String request) throws java.io.IOException, ParseException {
InetSocketAddress sock = (InetSocketAddress) address;

ByteArrayOutputStream requestFrame = new ByteArrayOutputStream();
StringTokenizer parts = new StringTokenizer(request);
Expand Down

0 comments on commit 8e93ccf

Please sign in to comment.