Skip to content

Commit

Permalink
Move iterator wrappers into the separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Jan 8, 2024
1 parent ea66150 commit 187803c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 79 deletions.
79 changes: 2 additions & 77 deletions src/socket/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// ------------------------------------------------------------------------
// Gufo SNMP: SnmpClientSocket
// ------------------------------------------------------------------------
// Copyright (C) 2023, Gufo Labs
// Copyright (C) 2023-24, Gufo Labs
// See LICENSE.md for details
// ------------------------------------------------------------------------

use super::iter::{GetBulkIter, GetNextIter};
use super::RequestId;
use crate::ber::{BerEncoder, SnmpOid, ToPython};
use crate::buf::Buffer;
Expand Down Expand Up @@ -37,19 +38,6 @@ pub struct SnmpClientSocket {
buf: Buffer,
}

#[pyclass]
pub struct GetNextIter {
start_oid: SnmpOid,
next_oid: SnmpOid,
}

#[pyclass]
pub struct GetBulkIter {
start_oid: SnmpOid,
next_oid: SnmpOid,
max_repetitions: i64,
}

#[pymethods]
impl SnmpClientSocket {
/// Python constructor
Expand Down Expand Up @@ -438,66 +426,3 @@ impl SnmpClientSocket {
Ok(())
}
}

#[pymethods]
impl GetNextIter {
/// Python constructor
#[new]
fn new(oid: &str) -> PyResult<Self> {
let b_oid = SnmpOid::try_from(oid).map_err(|_| PyValueError::new_err("invalid oid"))?;
Ok(GetNextIter {
start_oid: b_oid.clone(),
next_oid: b_oid,
})
}
}

impl GetNextIter {
pub fn get_next_oid(&self) -> SnmpOid {
self.next_oid.clone()
}
// Save oid for next request.
// Return true if next request may be send or return false otherwise
pub fn set_next_oid(&mut self, oid: &SnmpOid) -> bool {
if self.start_oid.contains(oid) {
self.next_oid = oid.clone();
true
} else {
false
}
}
}

#[pymethods]
impl GetBulkIter {
/// Python constructor
#[new]
fn new(oid: &str, max_repetitions: i64) -> PyResult<Self> {
let b_oid = SnmpOid::try_from(oid).map_err(|_| PyValueError::new_err("invalid oid"))?;
Ok(GetBulkIter {
start_oid: b_oid.clone(),
next_oid: b_oid,
max_repetitions,
})
}
}

impl GetBulkIter {
pub fn get_next_oid(&self) -> SnmpOid {
self.next_oid.clone()
}
// Save oid for next request.
// Return true if next request may be send or return false otherwise
pub fn set_next_oid(&mut self, oid: &SnmpOid) -> bool {
if self.start_oid.contains(oid) {
self.next_oid = oid.clone();
true
} else {
false
}
}
//
pub fn get_max_repetitions(&self) -> i64 {
self.max_repetitions
}
}
85 changes: 85 additions & 0 deletions src/socket/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// ------------------------------------------------------------------------
// Gufo SNMP: Iterator wrappers
// ------------------------------------------------------------------------
// Copyright (C) 2023-24, Gufo Labs
// See LICENSE.md for details
// ------------------------------------------------------------------------

use crate::ber::SnmpOid;
use pyo3::{exceptions::PyValueError, prelude::*};

#[pyclass]
pub struct GetNextIter {
start_oid: SnmpOid,
next_oid: SnmpOid,
}

#[pyclass]
pub struct GetBulkIter {
start_oid: SnmpOid,
next_oid: SnmpOid,
max_repetitions: i64,
}

#[pymethods]
impl GetNextIter {
/// Python constructor
#[new]
fn new(oid: &str) -> PyResult<Self> {
let b_oid = SnmpOid::try_from(oid).map_err(|_| PyValueError::new_err("invalid oid"))?;
Ok(GetNextIter {
start_oid: b_oid.clone(),
next_oid: b_oid,
})
}
}

impl GetNextIter {
pub fn get_next_oid(&self) -> SnmpOid {
self.next_oid.clone()
}
// Save oid for next request.
// Return true if next request may be send or return false otherwise
pub fn set_next_oid(&mut self, oid: &SnmpOid) -> bool {
if self.start_oid.contains(oid) {
self.next_oid = oid.clone();
true
} else {
false
}
}
}

#[pymethods]
impl GetBulkIter {
/// Python constructor
#[new]
fn new(oid: &str, max_repetitions: i64) -> PyResult<Self> {
let b_oid = SnmpOid::try_from(oid).map_err(|_| PyValueError::new_err("invalid oid"))?;
Ok(GetBulkIter {
start_oid: b_oid.clone(),
next_oid: b_oid,
max_repetitions,
})
}
}

impl GetBulkIter {
pub fn get_next_oid(&self) -> SnmpOid {
self.next_oid.clone()
}
// Save oid for next request.
// Return true if next request may be send or return false otherwise
pub fn set_next_oid(&mut self, oid: &SnmpOid) -> bool {
if self.start_oid.contains(oid) {
self.next_oid = oid.clone();
true
} else {
false
}
}
//
pub fn get_max_repetitions(&self) -> i64 {
self.max_repetitions
}
}
6 changes: 4 additions & 2 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// ------------------------------------------------------------------------

pub mod client;
pub mod reqid;
pub use client::{GetBulkIter, GetNextIter, SnmpClientSocket};
mod iter;
mod reqid;
pub use client::SnmpClientSocket;
pub use iter::{GetBulkIter, GetNextIter};
use reqid::RequestId;

0 comments on commit 187803c

Please sign in to comment.