示例#1
0
        /// <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, 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;
        }
        public bool registerWithServer(string host, int serverPort, TDIN_chatlib.LoginUser user)
        {
            string serverURL = "tcp://" + host + ":" + serverPort + "/" + TDIN_chatlib.Constants.SERVER_SERVICE;

            // Create an instance of the remote object
            remoteServer = (TDIN_chatlib.ChatSeverInterface)Activator.GetObject(
                                        typeof(TDIN_chatlib.ChatSeverInterface), serverURL);

            userSession = remoteServer.registerClient(_session_uid, MY_SERVICE_PORT, user);

            if (    userSession.SessionHash == null
                 || userSession.SessionHash != this._handshakeSessionHash )
            {
                this._handshakeSessionHash = null;

                throw new TDIN_chatlib.ChatException("Received wrong session hash from handshake");
            }

            return userSession != null ;
        }
        /// <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;
        }