Skip to content

Commit

Permalink
Add java master udp example
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain committed Apr 25, 2024
1 parent 00548c4 commit 3b1a908
Showing 1 changed file with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,35 @@ private static void runTls(Runtime runtime, TlsClientConfig tlsConfig) throws Ex
}
}

private static void runUdp(Runtime runtime) throws Exception {
// ANCHOR: create_udp_channel
MasterChannel channel =
MasterChannel.createUdpChannel(
runtime,
getMasterChannelConfig(),
"127.0.0.1:20001",
LinkReadMode.DATAGRAM,
Duration.ofSeconds(5)
);
// ANCHOR_END: create_udp_channel

try {
AssociationId association =
channel.addUdpAssociation(
ushort(1024),
"127.0.0.1:20000",
getAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation());

runAssociation(channel, association);
}
finally {
channel.shutdown();
}
}

private static void runSerial(Runtime runtime) throws Exception {
// ANCHOR: create_serial_channel
MasterChannel channel =
Expand Down Expand Up @@ -434,6 +463,9 @@ private static void run(Runtime runtime, String[] args) throws Exception {
case "tcp":
runTcp(runtime);
break;
case "udp":
runUdp(runtime);
break;
case "serial":
runSerial(runtime);
break;
Expand Down Expand Up @@ -612,24 +644,12 @@ private static List<UByte> getFileLine() {
return bytes;
}

private static void runChannel(MasterChannel channel) {

// Create the association
// ANCHOR: association_create
AssociationId association =
channel.addAssociation(
ushort(1024),
getAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation());
// ANCHOR_END: association_create

private static void runAssociation(MasterChannel channel, AssociationId association) {
// Create a periodic poll
// ANCHOR: add_poll
PollId poll =
channel.addPoll(
association, Request.classRequest(false, true, true, true), Duration.ofSeconds(5));
channel.addPoll(
association, Request.classRequest(false, true, true, true), Duration.ofSeconds(5));
// ANCHOR_END: add_poll

// start communications
Expand All @@ -651,6 +671,22 @@ private static void runChannel(MasterChannel channel) {
}
}

private static void runChannel(MasterChannel channel) {

// Create the association
// ANCHOR: association_create
AssociationId association =
channel.addAssociation(
ushort(1024),
getAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation());
// ANCHOR_END: association_create

runAssociation(channel, association);
}

private static void printFileInfo(FileInfo info) {
System.out.println("file name: " + info.fileName);
System.out.println(" type: " + info.fileType);
Expand Down

0 comments on commit 3b1a908

Please sign in to comment.