AssociationHandler
The AssociationHandler
interface handles all the per-association callbacks that are unrelated to measurement data. It contains a single method (get_current_time
)
to retrieve a UTC timestamp used to synchronize the time on the outstation.
Typically, you'd return the system time in get_current_time
which is kept synchronized via NTP. However, you can use a different clock if desired. For example, you might want a data concentrator to receive time from an upstream DNP3 master while maintaining an offset from an internal clock. The examples below demonstrate how to implement an AssociationHandler
that retrieves the system time.
- Rust
- C
- C++
- Java
- C#
#[derive(Copy, Clone)]
pub struct ExampleAssociationHandler;
impl AssociationHandler for ExampleAssociationHandler {}
note
Rust's 'AssociationHandler' trait has a default implementation of get_current_time
that retrieves the system time.
dnp3_utc_timestamp_t get_system_time(void *arg)
{
time_t timer = time(NULL);
return dnp3_utc_timestamp_valid(timer * 1000);
}
dnp3_association_handler_t get_association_handler()
{
return (dnp3_association_handler_t){
.get_current_time = get_system_time,
.on_destroy = NULL,
.ctx = NULL,
};
}
class AssociationHandler : public dnp3::AssociationHandler {
dnp3::UtcTimestamp get_current_time() override
{
const auto time_since_epoch = std::chrono::system_clock::now().time_since_epoch();
return dnp3::UtcTimestamp::valid(std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch).count());
}
};
class TestAssociationHandler implements AssociationHandler {
@Override
public UtcTimestamp getCurrentTime() {
return UtcTimestamp.valid(ulong(System.currentTimeMillis()));
}
}
class TestAssociationHandler : IAssociationHandler
{
public UtcTimestamp GetCurrentTime()
{
return UtcTimestamp.Valid((ulong)DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalMilliseconds);
}
}