public static bool saveStore(UserStore store, string file) { try { using (var fileStream = new FileStream(file, FileMode.Create)) { var ser = new XmlSerializer(typeof(TDIN_chatlib.LoginUser[])); TDIN_chatlib.LoginUser[] _save = new TDIN_chatlib.LoginUser[store.userStore.Count]; int count = 0; foreach (var u in store.userStore.Values) { _save[count++] = u; } var memoryStream = new MemoryStream(); var streamWriter = new StreamWriter(memoryStream, System.Text.Encoding.UTF8); ser.Serialize(streamWriter, _save); byte[] utf8EncodedXml = memoryStream.ToArray(); memoryStream.Close(); fileStream.Write(utf8EncodedXml, 0, utf8EncodedXml.Length); fileStream.Close(); } return true; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); return false; } }
private void button1_Click(object sender, EventArgs e) { if ( this.serverHost.Text.Length == 0 || this.serverPort.Text.Length == 0 ) alertMessage("Host e a porta do servidor não preenchidos"); else if ( this.username.Text.Length == 0 || this.password.Text.Length == 0 ) alertMessage("Username e password têm de estar preenchidos"); else if( this.username.Text.Length < 4 ) alertMessage("O username tem de ter no mínimo 4 caractéres"); else if ( this.password.Text.Length < 4 ) alertMessage("A password tem de ter no mínimo 4 caractéres"); else if( this.isRegisto && ( this.passwordConf.Text.Length == 0 || this.nome.Text.Length == 0 ) ) alertMessage("Confirmação de password e nome têm de estar preenchidos"); else if ( this.isRegisto && this.password.Text != this.passwordConf.Text ) alertMessage("Por favor repita correctamente a password no campo de confirmação"); else { this.statusLabel.Text = "Connecting to server..."; this.Enabled = false; this.Refresh(); bool success = false; try { TDIN_chatlib.LoginUser user = new TDIN_chatlib.LoginUser(this.username.Text, this.password.Text); user.hashPassword(); if (this.isRegisto) user.Name = this.nome.Text; if (controller.registerWithServer(this.serverHost.Text, Convert.ToInt32(this.serverPort.Text), user)) { Console.WriteLine("Sucefully registered with server. session: " + controller.Session.SessionHash); this.statusLabel.Text = "Success!"; success = true; } else { this.statusLabel.Text = "Error registering with server!"; } } catch (TDIN_chatlib.ChatException ex1) { this.statusLabel.Text = ex1.Message; } catch (Exception ex) { Console.WriteLine(ex); this.statusLabel.Text = "Error connecting to server!"; } finally { if (success) { Thread t = new Thread(Program.LaunchListWindow); t.TrySetApartmentState(ApartmentState.STA); t.Start(); this.Dispose(); } else this.Enabled = true; } } }
/// <summary> /// /// </summary> /// <param name="address"></param> /// <param name="user"></param> /// <returns></returns> public TDIN_chatlib.UserSession registerClient(string uid, TDIN_chatlib.InternalIPAddress address, TDIN_chatlib.LoginUser user) { //TODO: Complete according with the interface specification. // Create user and add it to active users. TDIN_chatlib.IPUser ipUser = new TDIN_chatlib.IPUser(user.Username, user.Name, address); TDIN_chatlib.UserSession session = new TDIN_chatlib.UserSession(user.Username, user.Name, TDIN_chatlib.Utils.generateRandomHash()); ipUser.generateUID(); session.UUID = ipUser.UUID; // Mudei um pouco aqui as coisas, falta então fazeres a tua classe interna com toda a informação, // tipo base de dados, ou usares o LoginUser que até agora acho que tem tudo o que é preciso relativamente ao user (como se fosse para guardar na BD) //activeClients.Add(new_user); try { string url = "tcp://" + address.IP + ":" + address.PORT + "/" + TDIN_chatlib.Constants.CLIENT_SUBSCRIBE_SERVICE, handshakeUID; TDIN_chatlib.UserSubscribeInterface usi = (TDIN_chatlib.UserSubscribeInterface)Activator.GetObject( typeof(TDIN_chatlib.ChatSeverInterface), url); handshakeUID = usi.handshake(session.SessionHash); if (handshakeUID != uid) { throw new TDIN_chatlib.ChatException("UIDs do not match"); } lock (syncLock) { sessionInterface.Add(session.SessionHash, usi); sessions.Add(session.SessionHash, ipUser); // force active client list to be rebuilt on next user query _tempIPList = null; Console.WriteLine("* New user: "******", uid: " + uid + ", hash: " + session.SessionHash); } _createUpdateClientThread(); } catch (TDIN_chatlib.ChatException ex1) { Console.WriteLine("Error: + " + ex1.Message); throw ex1; } catch (Exception e) { Console.WriteLine(e); throw new TDIN_chatlib.ChatException("Error on handshake"); } Console.WriteLine("* Registering client with user: "******" terminated"); return(session); }
/// <summary> /// /// </summary> /// <param name="address"></param> /// <param name="user"></param> /// <returns></returns> public TDIN_chatlib.UserSession registerClient(string uid, int port, TDIN_chatlib.LoginUser user) { //TODO: Complete according with the interface specification. if (uid.Length == 0 || port < 1 || port > 65535 || user == null) throw new TDIN_chatlib.ChatException("Badly constructed registration"); if (!user.isValidLogin()) throw new TDIN_chatlib.ChatException("Please fill username and password"); TDIN_chatlib.LoginUser fromStore = Program.store.getUserByUsername(user.Username); if (fromStore != null) { if (!fromStore.comparePassword(user)) throw new TDIN_chatlib.ChatException("Invalid password for this username"); } else { if (!user.isValidRegister()) throw new TDIN_chatlib.ChatException("User not registered please fill a name for this user"); else { fromStore = new TDIN_chatlib.LoginUser(user); fromStore.generateUID(); Program.addUser(fromStore); } } // Create user and add it to active users. TDIN_chatlib.IPUser ipUser = new TDIN_chatlib.IPUser(fromStore); TDIN_chatlib.UserSession session = new TDIN_chatlib.UserSession(fromStore); ipUser.IPAddress = new TDIN_chatlib.InternalIPAddress( CallContext.GetData("ClientIPAddress").ToString(), port); ipUser.UUID = session.UUID = fromStore.UUID; session.SessionHash = TDIN_chatlib.Utils.generateRandomHash(); //TDIN_chatlib.InternalIPAddress address try { string url = "tcp://" + ipUser.IPAddress.IP + ":" + port + "/" + TDIN_chatlib.Constants.CLIENT_SUBSCRIBE_SERVICE, handshakeUID; TDIN_chatlib.UserSubscribeInterface usi = (TDIN_chatlib.UserSubscribeInterface)Activator.GetObject( typeof(TDIN_chatlib.ChatSeverInterface), url); handshakeUID = usi.handshake(session.SessionHash); if (handshakeUID != uid) throw new TDIN_chatlib.ChatException("UIDs do not match"); lock (syncLock) { sessionInterface.Add(session.SessionHash, usi); sessions.Add(session.SessionHash, ipUser); // force active client list to be rebuilt on next user query _tempIPList = null; Console.WriteLine("* New user: "******", uid: " + uid + ", hash: " + session.SessionHash); } _createUpdateClientThread(); } catch (TDIN_chatlib.ChatException ex1) { Console.WriteLine("Error: + " + ex1.Message); throw ex1; } catch (Exception e) { Console.WriteLine(e); throw new TDIN_chatlib.ChatException("Error on handshake"); } Console.WriteLine("* Registering client with user: "******" terminated"); return session; }