use std::io::Read;
use std::io::Write;
use std::os::unix::net::UnixStream;
use api::containers::Containers;
use api::version::Version;
use api::DockerApiClient;
use errors::DockerClientError;
use utils;
pub struct DockerClient {
socket: UnixStream,
protocol: ConnectionProtocol,
}
enum ConnectionProtocol {
UNIX,
}
impl DockerClient {
pub fn new(
connection_addr: &'static str,
) -> Result<DockerClient, DockerClientError> {
let addr_components =
match utils::validate_unix_socket_address(connection_addr) {
Some(addr_comps) => addr_comps,
None => {
return Err(DockerClientError::InvalidTargetAddress(
connection_addr,
))
}
};
let unix_socket = match UnixStream::connect(addr_components[1]) {
Ok(sock) => sock,
Err(_err) => {
return Err(DockerClientError::SocketConnectionError(
addr_components[1],
))
}
};
let protocol = match addr_components[0] {
"unix" => ConnectionProtocol::UNIX,
_ => {
return Err(DockerClientError::InvalidTargetAddress(
connection_addr,
))
}
};
let docker_client = DockerClient {
socket: unix_socket,
protocol: protocol,
};
Ok(docker_client)
}
}
impl Clone for DockerClient {
fn clone(&self) -> DockerClient {
let sock = self.socket
.try_clone()
.expect("Error while trying to clone the socket");
let protocol = match self.protocol {
ConnectionProtocol::UNIX => ConnectionProtocol::UNIX,
};
let docker_client_clone = DockerClient {
socket: sock,
protocol: protocol,
};
return docker_client_clone;
}
}
impl DockerApiClient for DockerClient {
fn request(&self, request: &str) -> Option<Vec<u8>> {
let mut client = self.socket.try_clone().unwrap();
let buf = request.as_bytes();
match client.write_all(buf) {
Ok(_) => println!("Wrote all data to socket"),
Err(_) => return None,
};
const BUFFER_SIZE: usize = 1024;
let mut buffer: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
let mut raw_resp: Vec<u8> = Vec::new();
loop {
let len = match client.read(&mut buffer) {
Ok(len) => len,
Err(_) => return None,
};
for i in 0..len {
raw_resp.push(buffer[i]);
}
if len < BUFFER_SIZE {
break;
}
}
Some(raw_resp)
}
}
impl Version for DockerClient {}
impl Containers for DockerClient {}