Move server_id into session_id, to that sessions can be replaced

This commit is contained in:
Lucien Greathouse 2018-08-19 22:06:10 -07:00
parent ba89a8bf6a
commit 1f6a5b5cbe
2 changed files with 13 additions and 12 deletions

View file

@ -2,6 +2,8 @@ use std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
use rand;
use ::{ use ::{
message_queue::MessageQueue, message_queue::MessageQueue,
rbx::RbxTree, rbx::RbxTree,
@ -10,13 +12,17 @@ use ::{
pub struct Session { pub struct Session {
project: Project, project: Project,
pub session_id: String,
pub message_queue: Arc<MessageQueue>, pub message_queue: Arc<MessageQueue>,
pub tree: Arc<RwLock<RbxTree>>, pub tree: Arc<RwLock<RbxTree>>,
} }
impl Session { impl Session {
pub fn new(project: Project) -> Session { pub fn new(project: Project) -> Session {
let session_id = rand::random::<u64>().to_string();
Session { Session {
session_id,
project, project,
message_queue: Arc::new(MessageQueue::new()), message_queue: Arc::new(MessageQueue::new()),
tree: Arc::new(RwLock::new(RbxTree::new())), tree: Arc::new(RwLock::new(RbxTree::new())),

View file

@ -3,7 +3,6 @@ use std::collections::HashMap;
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc};
use rouille::{self, Request, Response}; use rouille::{self, Request, Response};
use rand;
use ::{ use ::{
id::Id, id::Id,
@ -16,7 +15,7 @@ use ::{
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ServerInfoResponse<'a> { pub struct ServerInfoResponse<'a> {
pub server_id: &'a str, pub session_id: &'a str,
pub server_version: &'a str, pub server_version: &'a str,
pub protocol_version: u64, pub protocol_version: u64,
pub root_instance_id: Id, pub root_instance_id: Id,
@ -25,7 +24,7 @@ pub struct ServerInfoResponse<'a> {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ReadResponse<'a> { pub struct ReadResponse<'a> {
pub server_id: &'a str, pub session_id: &'a str,
pub message_cursor: u32, pub message_cursor: u32,
pub instances: HashMap<Id, Cow<'a, RbxInstance>>, pub instances: HashMap<Id, Cow<'a, RbxInstance>>,
} }
@ -33,7 +32,7 @@ pub struct ReadResponse<'a> {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SubscribeResponse<'a> { pub struct SubscribeResponse<'a> {
pub server_id: &'a str, pub session_id: &'a str,
pub message_cursor: u32, pub message_cursor: u32,
pub messages: Cow<'a, [Message]>, pub messages: Cow<'a, [Message]>,
} }
@ -41,17 +40,13 @@ pub struct SubscribeResponse<'a> {
pub struct Server { pub struct Server {
session: Arc<Session>, session: Arc<Session>,
server_version: &'static str, server_version: &'static str,
server_id: String,
} }
impl Server { impl Server {
pub fn new(session: Arc<Session>) -> Server { pub fn new(session: Arc<Session>) -> Server {
let server_id = rand::random::<u64>().to_string();
Server { Server {
session: session, session: session,
server_version: env!("CARGO_PKG_VERSION"), server_version: env!("CARGO_PKG_VERSION"),
server_id,
} }
} }
@ -70,7 +65,7 @@ impl Server {
Response::json(&ServerInfoResponse { Response::json(&ServerInfoResponse {
server_version: self.server_version, server_version: self.server_version,
protocol_version: 2, protocol_version: 2,
server_id: &self.server_id, session_id: &self.session.session_id,
root_instance_id: tree.root_instance_id, root_instance_id: tree.root_instance_id,
}) })
}, },
@ -87,7 +82,7 @@ impl Server {
if new_messages.len() > 0 { if new_messages.len() > 0 {
return Response::json(&SubscribeResponse { return Response::json(&SubscribeResponse {
server_id: &self.server_id, session_id: &self.session.session_id,
messages: Cow::Borrowed(&[]), messages: Cow::Borrowed(&[]),
message_cursor: new_cursor, message_cursor: new_cursor,
}) })
@ -109,7 +104,7 @@ impl Server {
let (new_cursor, new_messages) = message_queue.get_messages_since(cursor); let (new_cursor, new_messages) = message_queue.get_messages_since(cursor);
return Response::json(&SubscribeResponse { return Response::json(&SubscribeResponse {
server_id: &self.server_id, session_id: &self.session.session_id,
messages: Cow::Owned(new_messages), messages: Cow::Owned(new_messages),
message_cursor: new_cursor, message_cursor: new_cursor,
}) })
@ -149,7 +144,7 @@ impl Server {
} }
Response::json(&ReadResponse { Response::json(&ReadResponse {
server_id: &self.server_id, session_id: &self.session.session_id,
message_cursor, message_cursor,
instances, instances,
}) })