scaproust/core/
session.rs

1// Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
5// This file may not be copied, modified, or distributed except according to those terms.
6
7use std::collections::HashMap;
8use std::sync::mpsc;
9use std::io;
10
11use core::{BuildIdHasher, SocketId, DeviceId, ProbeId, PollReq, socket, device, probe};
12use sequence::Sequence;
13
14pub enum Request {
15    CreateSocket(socket::ProtocolCtor),
16    CreateDevice(SocketId, SocketId),
17    CreateProbe(Vec<PollReq>),
18    Shutdown
19}
20
21pub enum Reply {
22    Err(io::Error),
23    SocketCreated(SocketId, mpsc::Receiver<socket::Reply>),
24    DeviceCreated(DeviceId, mpsc::Receiver<device::Reply>),
25    ProbeCreated(ProbeId, mpsc::Receiver<probe::Reply>),
26    Shutdown
27}
28
29pub struct Session {
30    reply_sender: mpsc::Sender<Reply>,
31    sockets: SocketCollection,
32    devices: DeviceCollection,
33    probes: ProbeCollection
34}
35
36struct SocketCollection {
37    ids: Sequence,
38    sockets: HashMap<SocketId, socket::Socket, BuildIdHasher>
39}
40
41struct DeviceCollection {
42    ids: Sequence,
43    mapping: HashMap<SocketId, DeviceId, BuildIdHasher>,
44    devices: HashMap<DeviceId, device::Device, BuildIdHasher>
45}
46
47struct ProbeCollection {
48    ids: Sequence,
49    mapping: HashMap<SocketId, ProbeId, BuildIdHasher>,
50    probes: HashMap<ProbeId, probe::Probe, BuildIdHasher>
51}
52
53impl Session {
54    pub fn new(seq: Sequence, reply_tx: mpsc::Sender<Reply>) -> Session {
55        Session {
56            reply_sender: reply_tx,
57            sockets: SocketCollection::new(seq.clone()),
58            devices: DeviceCollection::new(seq.clone()),
59            probes: ProbeCollection::new(seq.clone())
60        }
61    }
62
63    fn send_reply(&self, reply: Reply) {
64        let _ = self.reply_sender.send(reply);
65    }
66
67/*****************************************************************************/
68/*                                                                           */
69/* Sockets                                                                   */
70/*                                                                           */
71/*****************************************************************************/
72
73    pub fn add_socket(&mut self, protocol_ctor: socket::ProtocolCtor) {
74        let (tx, rx) = mpsc::channel();
75        let protocol = protocol_ctor(tx.clone());
76        let id = self.sockets.add(tx, protocol);
77
78        self.send_reply(Reply::SocketCreated(id, rx));
79    }
80
81    pub fn get_socket_mut(&mut self, id: SocketId) -> Option<&mut socket::Socket> {
82        self.sockets.get_socket_mut(id)
83    }
84
85    pub fn remove_socket(&mut self, sid: SocketId) {
86        self.sockets.remove(sid);
87    }
88
89/*****************************************************************************/
90/*                                                                           */
91/* Devices                                                                   */
92/*                                                                           */
93/*****************************************************************************/
94
95    pub fn add_device(&mut self, left: SocketId, right: SocketId) {
96        let (tx, rx) = mpsc::channel();
97        let id = self.devices.add(tx, left, right);
98
99        self.send_reply(Reply::DeviceCreated(id, rx));
100    }
101
102    pub fn get_device_mut(&mut self, id: DeviceId) -> Option<&mut device::Device> {
103        self.devices.get_device_mut(id)
104    }
105
106    pub fn find_device_mut(&mut self, id: SocketId) -> Option<&mut device::Device> {
107        self.devices.find_device_mut(id)
108    }
109
110    pub fn remove_device(&mut self, did: DeviceId) {
111        self.devices.remove(did);
112    }
113
114/*****************************************************************************/
115/*                                                                           */
116/* Probes                                                                   */
117/*                                                                           */
118/*****************************************************************************/
119
120    pub fn add_probe(&mut self, poll_opts: Vec<PollReq>) {
121        let (tx, rx) = mpsc::channel();
122        let id = self.probes.add(tx, poll_opts);
123
124        self.send_reply(Reply::ProbeCreated(id, rx));
125    }
126
127    pub fn get_probe_mut(&mut self, id: ProbeId) -> Option<&mut probe::Probe> {
128        self.probes.get_probe_mut(id)
129    }
130
131    pub fn find_probe_mut(&mut self, id: SocketId) -> Option<(&ProbeId, &mut probe::Probe)> {
132        self.probes.find_probe_mut(id)
133    }
134
135    pub fn remove_probe(&mut self, id: ProbeId) {
136        self.probes.remove(id);
137    }
138}
139
140/*****************************************************************************/
141/*                                                                           */
142/* Socket collection                                                         */
143/*                                                                           */
144/*****************************************************************************/
145
146impl SocketCollection {
147    fn new(seq: Sequence) -> SocketCollection {
148        SocketCollection {
149            ids: seq,
150            sockets: HashMap::default()
151        }
152    }
153
154    fn add(&mut self, reply_tx: mpsc::Sender<socket::Reply>, proto: Box<socket::Protocol>) -> SocketId {
155        let id = SocketId::from(self.ids.next());
156        let socket = socket::Socket::new(id, reply_tx, proto);
157
158        self.sockets.insert(id, socket);
159
160        id
161    }
162
163    fn get_socket_mut(&mut self, id: SocketId) -> Option<&mut socket::Socket> {
164        self.sockets.get_mut(&id)
165    }
166
167    fn remove(&mut self, id: SocketId) {
168        self.sockets.remove(&id);
169    }
170}
171
172/*****************************************************************************/
173/*                                                                           */
174/* Device collection                                                         */
175/*                                                                           */
176/*****************************************************************************/
177
178impl DeviceCollection {
179    fn new(seq: Sequence) -> DeviceCollection {
180        DeviceCollection {
181            ids: seq,
182            mapping: HashMap::default(),
183            devices: HashMap::default()
184        }
185    }
186
187    fn add(&mut self, reply_tx: mpsc::Sender<device::Reply>, left: SocketId, right: SocketId) -> DeviceId {
188        let id = DeviceId::from(self.ids.next());
189        let device = device::Device::new(reply_tx, left, right);
190
191        self.devices.insert(id, device);
192        self.mapping.insert(left, id);
193        self.mapping.insert(right, id);
194
195        id
196    }
197
198    fn get_device_mut(&mut self, id: DeviceId) -> Option<&mut device::Device> {
199        self.devices.get_mut(&id)
200    }
201
202    fn find_device_mut(&mut self, sid: SocketId) -> Option<&mut device::Device> {
203        if let Some(did) = self.mapping.get(&sid) {
204            self.devices.get_mut(did)
205        } else {
206            None
207        }
208    }
209
210    fn remove(&mut self, id: DeviceId) {
211        if let Some(device) = self.devices.remove(&id) {
212            self.mapping.remove(device.get_left_id());
213            self.mapping.remove(device.get_right_id());
214        }
215    }
216}
217
218/*****************************************************************************/
219/*                                                                           */
220/* Probe collection                                                          */
221/*                                                                           */
222/*****************************************************************************/
223
224impl ProbeCollection {
225    fn new(seq: Sequence) -> ProbeCollection {
226        ProbeCollection {
227            ids: seq,
228            mapping: HashMap::default(),
229            probes: HashMap::default()
230        }
231    }
232
233    fn add(&mut self, reply_tx: mpsc::Sender<probe::Reply>, poll_opts: Vec<PollReq>) -> ProbeId {
234        let id = ProbeId::from(self.ids.next());
235        
236        for poll_opt in &poll_opts {
237            self.mapping.insert(poll_opt.sid, id);
238        }
239
240        let probe = probe::Probe::new(reply_tx, poll_opts);
241
242        self.probes.insert(id, probe);
243
244        id
245    }
246
247    fn get_probe_mut(&mut self, id: ProbeId) -> Option<&mut probe::Probe> {
248        self.probes.get_mut(&id)
249    }
250
251    fn find_probe_mut(&mut self, sid: SocketId) -> Option<(&ProbeId, &mut probe::Probe)> {
252        if let Some(pid) = self.mapping.get(&sid) {
253            self.probes.get_mut(pid).map(|probe| (pid, probe))
254        } else {
255            None
256        }
257    }
258
259    fn remove(&mut self, id: ProbeId) {
260        if let Some(probe) = self.probes.remove(&id) {
261            let ids = probe.get_socket_ids();
262
263            for id in &ids {
264                self.mapping.remove(id);
265            }
266        }
267    }
268}
269