Skip to content

Commit

Permalink
UDP examples (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain authored Apr 25, 2024
1 parent 995ba4e commit 116a976
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 45 deletions.
54 changes: 42 additions & 12 deletions ffi/bindings/c/master_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,18 +420,8 @@ void run_command(const std::string &cmd, dnp3::MasterChannel &channel, dnp3::Ass
}
}

void run_channel(dnp3::MasterChannel &channel)
void run_association(dnp3::MasterChannel &channel, dnp3::AssociationId assoc)
{
// ANCHOR: association_create
auto assoc = channel.add_association(
1024,
get_association_config(),
std::make_unique<ReadHandler>(),
std::make_unique<AssociationHandler>(),
std::make_unique<AssociationInformation>()
);
// ANCHOR_END: association_create

// ANCHOR: add_poll
auto event_scan = dnp3::Request::class_request(false, true, true, true);
const auto event_poll = channel.add_poll(assoc, event_scan, std::chrono::seconds(10));
Expand All @@ -450,13 +440,23 @@ void run_channel(dnp3::MasterChannel &channel)
try {
run_command(cmd, channel, assoc, event_poll);
}
catch (const std::exception& ex) {
catch (const std::exception &ex) {
std::cout << "Exception: " << ex.what() << std::endl;
}
}
}
}

void run_channel(dnp3::MasterChannel &channel)
{
// ANCHOR: association_create
auto assoc = channel.add_association(1024, get_association_config(), std::make_unique<ReadHandler>(), std::make_unique<AssociationHandler>(),
std::make_unique<AssociationInformation>());
// ANCHOR_END: association_create

run_association(channel, assoc);
}

void run_tcp_client(dnp3::Runtime &runtime)
{
// ANCHOR: create_master_tcp_channel
Expand All @@ -474,6 +474,33 @@ void run_tcp_client(dnp3::Runtime &runtime)
run_channel(channel);
}

void run_udp(dnp3::Runtime &runtime)
{
// ANCHOR: create_master_udp_channel
dnp3::EndpointList endpoints(std::string("127.0.0.1:20000"));

auto channel = dnp3::MasterChannel::create_udp_channel(
runtime,
get_master_channel_config(),
"127.0.0.1:20001",
dnp3::LinkReadMode::datagram,
std::chrono::seconds(5)
);
// ANCHOR_END: create_master_udp_channel

// ANCHOR: create_udp_association
auto assoc = channel.add_udp_association(
1024, "127.0.0.1:20000",
get_association_config(),
std::make_unique<ReadHandler>(),
std::make_unique<AssociationHandler>(),
std::make_unique<AssociationInformation>()
);
// ANCHOR_END: create_udp_association

run_association(channel, assoc);
}

void run_serial(dnp3::Runtime &runtime)
{
// ANCHOR: create_master_serial_channel
Expand Down Expand Up @@ -565,6 +592,9 @@ int main(int argc, char *argv[])
if (strcmp(type, "tcp") == 0) {
run_tcp_client(runtime);
}
else if (strcmp(type, "udp") == 0) {
run_udp(runtime);
}
else if (strcmp(type, "serial") == 0) {
run_serial(runtime);
}
Expand Down
25 changes: 25 additions & 0 deletions ffi/bindings/c/outstation_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,28 @@ void run_serial(dnp3::Runtime &runtime)
run_outstation(outstation);
}

void run_udp(dnp3::Runtime &runtime)
{
// ANCHOR: create_udp
dnp3::OutstationUdpConfig udp_config(
"127.0.0.1:20000",
"127.0.0.1:20001"
);

auto outstation = dnp3::Outstation::create_udp(
runtime,
udp_config,
get_outstation_config(),
std::make_unique<MyOutstationApplication>(),
std::make_unique<MyOutstationInformation>(),
std::make_unique<MyControlHandler>()
);

// ANCHOR_END: create_udp

run_outstation(outstation);
}

void run_tls_server(dnp3::Runtime &runtime, const dnp3::TlsServerConfig &config)
{
// ANCHOR: create_tls_server
Expand Down Expand Up @@ -440,6 +462,9 @@ int main(int argc, char *argv[])
else if (strcmp(type, "tcp-client") == 0) {
run_tcp_client(runtime);
}
else if (strcmp(type, "udp") == 0) {
run_udp(runtime);
}
else if (strcmp(type, "serial") == 0) {
run_serial(runtime);
}
Expand Down
70 changes: 56 additions & 14 deletions ffi/bindings/dotnet/examples/master/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,39 @@ private static void RunTls(Runtime runtime, TlsClientConfig tlsConfig)
}
}

private static void RunUdp(Runtime runtime)
{
// ANCHOR: create_udp_channel
var channel = MasterChannel.CreateUdpChannel(
runtime,
GetMasterChannelConfig(),
"127.0.0.1:20001",
LinkReadMode.Datagram,
TimeSpan.FromSeconds(5)
);
// ANCHOR_END: create_serial_channel

try
{
// ANCHOR: association_create_udp
var association = channel.AddUdpAssociation(
1024,
"127.0.0.1:20000",
GetAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation()
);
// ANCHOR_END: association_create_udp

RunAssociation(channel, association);
}
finally
{
channel.Shutdown();
}
}

private static void RunSerial(Runtime runtime)
{
// ANCHOR: create_serial_channel
Expand Down Expand Up @@ -447,6 +480,9 @@ public static void Main(string[] args)
case "tcp":
RunTcp(runtime);
break;
case "udp":
RunUdp(runtime);
break;
case "serial":
RunSerial(runtime);
break;
Expand All @@ -469,18 +505,8 @@ public static void Main(string[] args)
}
}

private static void RunChannel(MasterChannel channel)
private static void RunAssociation(MasterChannel channel, AssociationId association)
{
// ANCHOR: association_create
var association = channel.AddAssociation(
1024,
GetAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation()
);
// ANCHOR_END: association_create

// ANCHOR: add_poll
var poll = channel.AddPoll(association, Request.ClassRequest(false, true, true, true), TimeSpan.FromSeconds(5));
// ANCHOR_END: add_poll
Expand All @@ -491,11 +517,12 @@ private static void RunChannel(MasterChannel channel)
while (true)
{
var input = Console.ReadLine();

if(input == "x") {

if (input == "x")
{
return;
}

try
{
RunOneCommand(channel, association, poll, input).GetAwaiter().GetResult();
Expand All @@ -507,6 +534,21 @@ private static void RunChannel(MasterChannel channel)
}
}

private static void RunChannel(MasterChannel channel)
{
// ANCHOR: association_create
var association = channel.AddAssociation(
1024,
GetAssociationConfig(),
new TestReadHandler(),
new TestAssociationHandler(),
new TestAssociationInformation()
);
// ANCHOR_END: association_create

RunAssociation(channel, association);
}

private static async Task RunOneCommand(MasterChannel channel, AssociationId association, PollId poll, String input)
{
switch (input)
Expand Down
24 changes: 24 additions & 0 deletions ffi/bindings/dotnet/examples/outstation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ private static void RunSerial(Runtime runtime)
RunOutstation(outstation);
}

private static void RunUdp(Runtime runtime)
{
// ANCHOR: create_udp
var config = new OutstationUdpConfig(
"127.0.0.1:20000",
"127.0.0.1:20001"
);

var outstation = Outstation.CreateUdp(
runtime,
config,
GetOutstationConfig(),
new TestOutstationApplication(),
new TestOutstationInformation(),
new TestControlHandler()
);
// ANCHOR_END: create_udp

RunOutstation(outstation);
}

private static void RunTls(Runtime runtime, TlsServerConfig config)
{
// ANCHOR: create_tls_server
Expand Down Expand Up @@ -387,6 +408,9 @@ public static void Main(string[] args)
case "tcp":
RunTcp(runtime);
break;
case "udp":
RunUdp(runtime);
break;
case "serial":
RunSerial(runtime);
break;
Expand Down
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
Loading

0 comments on commit 116a976

Please sign in to comment.