1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use log::trace;
use std::ptr;
use std::ffi::CString;
use crate::bindings;


/// Represents a pbs server
pub struct Server {
    conn: std::os::raw::c_int,
}


impl Server {
    /// Connect to the default PBS server
    pub fn new() -> Server {
        trace!("Connecting to pbs server");
        Server{conn: unsafe{pbs_sys::pbs_connect(ptr::null_mut())}}
    }

    /// Connect to the specified pbs server
    /// takes a server address of the form <hostname>[:<port>]
    pub fn connect_to(srv: &str) -> Result<Server,String> {
        trace!("Connecting to pbs server {}", srv);
        let server = CString::new(srv.to_string()).unwrap();
        match unsafe{pbs_sys::pbs_connect(server.as_ptr() as *mut i8)} {
            -1 => Err(bindings::get_err()),
            x => Ok(Server{conn: x}),
        }
    }
    pub(crate) fn conn(&self) -> std::os::raw::c_int {
        self.conn
    }
}

impl Drop for Server {
    fn drop(&mut self) {
        if 0 != unsafe{pbs_sys::pbs_disconnect(self.conn)} {
            println!("Error disconnecting {}", bindings::get_err());
        }
    }
}

impl Default for Server {
    fn default() -> Self {Self::new()}
}