summaryrefslogtreecommitdiff
path: root/src/server/unixserver.rs
blob: dc9feabc2f263c61c7c23c5573927df9f2c8acbf (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109


use std::io;
use std::path::Path;
use std::os::unix::io::AsRawFd;
use mio_uds::{UnixListener, UnixStream};
use slab::Slab;
use nix::sys::socket::getsockopt;
use nix::sys::socket::sockopt;
use users;

use self::super::streamconnection::StreamConnection;


pub struct UnixServer {
	listener: UnixListener,
	connections: Slab<StreamConnection<UnixStream>>
}

impl UnixServer {

	pub fn new(addr: &Path) -> Result<UnixServer, io::Error> {
		let listener = UnixListener::bind(addr)?;
		Ok( UnixServer {
			listener,
			connections: Slab::new()
		})
	}
	
	
}

impl super::Server for UnixServer {

	fn accept_pending_connections(&mut self) -> Vec<usize> {
		let mut new_connections = Vec::new();
		loop {
			match self.listener.accept() {
				Ok(Some((stream, _address))) => {
					let con = StreamConnection::new(stream);
					let id = self.connections.insert(con);
					new_connections.push(id);
				}
				Ok(None) => {
					break;
				}
				Err(_e) => {
					break;
				}
			}
		}
		new_connections
	}


	fn recv_pending_messages(&mut self) -> (Vec<(usize, String)>, Vec<usize>){
	// 	let mut buf = [0; 2048];
		let mut messages: Vec<(usize, String)> = Vec::new();
		let mut to_remove = Vec::new();
		for (key, connection) in self.connections.iter_mut(){
			match connection.read() {
				Err(_e) => {
					to_remove.push(key);
				}
				Ok((con_messages, closed)) => {
					for message in con_messages {
						messages.push((key, message));
					}
					if closed {
						to_remove.push(key);
					}
				}
			}
		}
		for key in to_remove.iter() {
			self.connections.remove(*key);
		}
		(messages, to_remove)
	}

	fn broadcast(&mut self, text: &str) {
		for (_id, conn) in self.connections.iter_mut() {
			let _ = conn.send(text);
		}
	}
	
	fn send(&mut self, id: usize, text: &str) -> Result<(), io::Error> {
		match self.connections.get_mut(id){
			Some(conn) => {
				conn.send(text)
			}
			None => Err(io::Error::new(io::ErrorKind::Other, "index is empty"))
		}
	}
	
	
	fn get_name(&self, id: usize) -> Option<String> {
		let connection = self.connections.get(id)?;
		let fd = connection.stream.as_raw_fd();
		if let Ok(peercred) = getsockopt(fd, sockopt::PeerCredentials) {
			let uid = peercred.uid();
			let user = users::get_user_by_uid(uid)?;
			let name = user.name();
			Some(name.to_string_lossy().to_string())
		} else { None }
	}

}