Skip to content

Commit

Permalink
don't export MasterChannelType publicly
Browse files Browse the repository at this point in the history
  • Loading branch information
jadamcrain committed May 20, 2024
1 parent df9ae61 commit 3df4fa2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
100 changes: 63 additions & 37 deletions dnp3/examples/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,37 +243,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// ANCHOR_END: logging

// spawn the master channel based on the command line argument
let mut channel = create_channel()?;

let mut association = match channel.get_channel_type() {
MasterChannelType::Udp => {
// ANCHOR: association_create_udp
channel
.add_udp_association(
EndpointAddress::try_new(1024)?,
"127.0.0.1:20000".parse()?,
get_association_config(),
ExampleReadHandler::boxed(),
Box::new(ExampleAssociationHandler),
Box::new(ExampleAssociationInformation),
)
.await?
// ANCHOR_END: association_create_udp
}
MasterChannelType::Stream => {
// ANCHOR: association_create
channel
.add_association(
EndpointAddress::try_new(1024)?,
get_association_config(),
ExampleReadHandler::boxed(),
Box::new(ExampleAssociationHandler),
Box::new(ExampleAssociationInformation),
)
.await?
// ANCHOR_END: association_create
}
};
let (mut channel, mut association) = create_channel_and_association().await?;

// create an event poll
// ANCHOR: add_poll
Expand Down Expand Up @@ -497,7 +467,8 @@ impl CliHandler {
}

// create the specified channel based on the command line argument
fn create_channel() -> Result<MasterChannel, Box<dyn std::error::Error>> {
async fn create_channel_and_association(
) -> Result<(MasterChannel, AssociationHandle), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().collect();
let transport: &str = match args.as_slice() {
[_, x] => x,
Expand All @@ -508,14 +479,34 @@ fn create_channel() -> Result<MasterChannel, Box<dyn std::error::Error>> {
}
};
match transport {
"tcp" => create_tcp_channel(),
"udp" => create_udp_channel(),
"tcp" => {
let mut channel = create_tcp_channel()?;
let assoc = add_association(&mut channel).await?;
Ok((channel, assoc))
}
"udp" => {
let mut channel = create_udp_channel()?;
let assoc = add_udp_association(&mut channel).await?;
Ok((channel, assoc))
}
#[cfg(feature = "serial")]
"serial" => create_serial_channel(),
"serial" => {
let mut channel = create_serial_channel()?;
let assoc = add_association(&mut channel).await?;
Ok((channel, assoc))
}
#[cfg(feature = "tls")]
"tls-ca" => create_tls_channel(get_tls_authority_config()?),
"tls-ca" => {
let mut channel = create_tls_channel(get_tls_authority_config()?)?;
let assoc = add_association(&mut channel).await?;
Ok((channel, assoc))
}
#[cfg(feature = "tls")]
"tls-self-signed" => create_tls_channel(get_tls_self_signed_config()?),
"tls-self-signed" => {
let mut channel = create_tls_channel(get_tls_self_signed_config()?)?;
let assoc = add_association(&mut channel).await?;
Ok((channel, assoc))
}
_ => {
eprintln!(
"unknown transport '{}', options are (tcp, serial, tls-ca, tls-self-signed)",
Expand All @@ -526,6 +517,41 @@ fn create_channel() -> Result<MasterChannel, Box<dyn std::error::Error>> {
}
}

async fn add_association(
channel: &mut MasterChannel,
) -> Result<AssociationHandle, Box<dyn std::error::Error>> {
// ANCHOR: association_create
let association = channel
.add_association(
EndpointAddress::try_new(1024)?,
get_association_config(),
ExampleReadHandler::boxed(),
Box::new(ExampleAssociationHandler),
Box::new(ExampleAssociationInformation),
)
.await?;
// ANCHOR_END: association_create
Ok(association)
}

async fn add_udp_association(
channel: &mut MasterChannel,
) -> Result<AssociationHandle, Box<dyn std::error::Error>> {
// ANCHOR: association_create_udp
let association = channel
.add_udp_association(
EndpointAddress::try_new(1024)?,
"127.0.0.1:20000".parse()?,
get_association_config(),
ExampleReadHandler::boxed(),
Box::new(ExampleAssociationHandler),
Box::new(ExampleAssociationInformation),
)
.await?;
// ANCHOR_END: association_create_udp
Ok(association)
}

// ANCHOR: master_channel_config
fn get_master_channel_config() -> Result<MasterChannelConfig, Box<dyn std::error::Error>> {
let mut config = MasterChannelConfig::new(EndpointAddress::try_new(1)?);
Expand Down
6 changes: 1 addition & 5 deletions dnp3/src/master/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::util::session::Enabled;

/// Master channels may be Udp or of a "stream" type such as TCP
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MasterChannelType {
/// UDP aka datagram based
Udp,
Expand Down Expand Up @@ -105,11 +106,6 @@ impl MasterChannel {
}
}

/// retrieve the channel type
pub fn get_channel_type(&self) -> MasterChannelType {
self.channel_type
}

/// enable communications
pub async fn enable(&mut self) -> Result<(), Shutdown> {
self.send_master_message(MasterMsg::EnableCommunication(Enabled::Yes))
Expand Down

0 comments on commit 3df4fa2

Please sign in to comment.