Merge pull request #23 from JayRod12/master

Use canonical username when subscribing to spirc.
This commit is contained in:
Paul Lietar 2015-12-31 01:00:09 +01:00
commit 877f4f7b93
3 changed files with 17 additions and 7 deletions

View file

@ -63,6 +63,7 @@ fn main() {
device_id: name.clone(), device_id: name.clone(),
cache_location: PathBuf::from(cache_location) cache_location: PathBuf::from(cache_location)
}; };
let session = Session::new(config); let session = Session::new(config);
session.login(username.clone(), password); session.login(username.clone(), password);
session.poll(); session.poll();
@ -76,7 +77,7 @@ fn main() {
let player = Player::new(&session); let player = Player::new(&session);
let mut spirc_manager = SpircManager::new(&session, player, username, name); let mut spirc_manager = SpircManager::new(&session, player, name);
spirc_manager.run(); spirc_manager.run();
} }

View file

@ -29,6 +29,7 @@ pub struct Config {
pub struct SessionData { pub struct SessionData {
pub country: String, pub country: String,
pub canonical_username: String,
} }
pub struct SessionInternal { pub struct SessionInternal {
@ -125,6 +126,7 @@ impl Session {
config: config, config: config,
data: RwLock::new(SessionData { data: RwLock::new(SessionData {
country: String::new(), country: String::new(),
canonical_username: String::new(),
}), }),
rx_connection: Mutex::new(cipher_connection.clone()), rx_connection: Mutex::new(cipher_connection.clone()),
@ -179,7 +181,14 @@ impl Session {
}, },
0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data), 0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data),
0xac => eprintln!("Authentication succeedded"), 0xac => {
let welcome_data : protocol::authentication::APWelcome =
protobuf::parse_from_bytes(&data).unwrap();
self.0.data.write().unwrap().canonical_username =
welcome_data.get_canonical_username().to_string();
eprintln!("Authentication succeeded")
},
0xad => eprintln!("Authentication failed"), 0xad => eprintln!("Authentication failed"),
_ => () _ => ()
} }

View file

@ -15,7 +15,6 @@ pub struct SpircManager<'s, D: SpircDelegate> {
delegate: D, delegate: D,
session: &'s Session, session: &'s Session,
username: String,
state_update_id: i64, state_update_id: i64,
seq_nr: u32, seq_nr: u32,
@ -61,12 +60,11 @@ pub trait SpircState {
impl <'s, D: SpircDelegate> SpircManager<'s, D> { impl <'s, D: SpircDelegate> SpircManager<'s, D> {
pub fn new(session: &'s Session, delegate: D, pub fn new(session: &'s Session, delegate: D,
username: String, name: String) -> SpircManager<'s, D> { name: String) -> SpircManager<'s, D> {
SpircManager { SpircManager {
delegate: delegate, delegate: delegate,
session: &session, session: &session,
username: username.clone(),
state_update_id: 0, state_update_id: 0,
seq_nr: 0, seq_nr: 0,
@ -91,7 +89,8 @@ impl <'s, D: SpircDelegate> SpircManager<'s, D> {
} }
pub fn run(&mut self) { pub fn run(&mut self) {
let rx = self.session.mercury_sub(format!("hm://remote/3/user/{}/", self.username)); let rx = self.session.mercury_sub(format!("hm://remote/3/user/{}/",
self.session.0.data.read().unwrap().canonical_username.clone()));
let updates = self.delegate.updates(); let updates = self.delegate.updates();
loop { loop {
@ -192,7 +191,8 @@ impl <'s, D: SpircDelegate> SpircManager<'s, D> {
self.session.mercury(MercuryRequest{ self.session.mercury(MercuryRequest{
method: MercuryMethod::SEND, method: MercuryMethod::SEND,
uri: format!("hm://remote/user/{}", self.username), uri: format!("hm://remote/user/{}",
self.session.0.data.read().unwrap().canonical_username.clone()),
content_type: None, content_type: None,
payload: vec![ pkt.write_to_bytes().unwrap() ] payload: vec![ pkt.write_to_bytes().unwrap() ]
}).await().unwrap(); }).await().unwrap();