Example #1
0
        string IDarPoolingMobile.HandleDarPoolingMobileRequest(Command c)
        {
            Console.WriteLine("{0} Received Mobile Request", LogTimestamp);
            string requestID = mainServiceImpl.generateGUID();

            c.CommandID = requestID;

            // Handle the command using the main service.
            mainServiceImpl.HandleMobileDarPoolingRequest(c);

            Console.WriteLine("Returning Request ID to client");
            //string requestIDTest = DateTime.Now.ToString();
            //return requestIDTest;
            return(requestID);
        }
        }// End RegisterUser

        /// <summary>
        /// Login the DarPooling Service network using username and password.
        /// Check if the current node is responsible for holding data about the user.
        /// If so, the given credential are checked, otherwise the request is
        /// scheduled for forwarding.
        /// </summary>
        /// <param name="username">The username provided by the client</param>
        /// <param name="pw_hash">The password provided by the client</param>
        /// <returns>
        /// a Result instance that represent the result of the Join operation; Specifically:
        ///  - a ConnectionErrorResult if the username has an invalid format
        ///  - a LoginErrorResult instance if the credentials don't match in the database
        ///  - a LoginOkResult if the credentials are valid.
        /// </returns>
        public Result Join(string username, string pw_hash)
        {
            Result joinResult;

            // Determine the name of the node where the user registered. Based to
            // the format, it MUST be the final token. Es.  user@baseaddress/NODENAME
            string registrationNode = username.Split('/').Last();

            // The username has an invalid format, i.e. it is impossible to retrieve the
            // name of the node where the user registered
            if (registrationNode.Length == 0)
            {
                joinResult         = new LoginErrorResult();
                joinResult.Comment = "Invalid username and/or password";
                return(joinResult);
            }

            // The user has registered in a different node. To confirm the join, we MUST
            // forward the Join request to the appropriate node.
            if (!registrationNode.Equals(this.NodeName))
            {
                ForwardRequiredResult forwardRequest = new ForwardRequiredResult();
                forwardRequest.RequestID   = serviceImpl.generateGUID();
                forwardRequest.Destination = baseForwardAddress + registrationNode;
                forwardRequest.Comment     = "You were not registered in this node";
                joinResult = forwardRequest;

                return(joinResult);
            }

            // Obtain the Read lock to determine if the user is actually registered.
            userDatabaseLock.EnterReadLock();
            try
            {
                //Console.WriteLine("{0} thread obtain the read lock in Join()", Thread.CurrentThread.Name);

                userDatabase = XDocument.Load(userDatabasePath);

                // Determine if the username and password have a match in the database
                var registeredUserQuery = (from user in userDatabase.Descendants("User")
                                           where user.Element("UserName").Value.Equals(username) &&
                                           user.Element("Password").Value.Equals(pw_hash)
                                           select user);

                // The provided username and password don't match in the database.
                if (registeredUserQuery.Count() == 0)
                {
                    joinResult         = new LoginErrorResult();
                    joinResult.Comment = "Invalid username and/or password";
                    return(joinResult);
                }
                else
                {   /** The Login is successful. */
                    LoginOkResult success = new LoginOkResult();
                    success.AuthorizedUsername = username;
                    success.Comment            = "Account successfully verified. You can now access DarPooling";
                    joinResult = success;
                    return(joinResult);
                }
            }
            finally
            {
                //Console.WriteLine("{0} thread releases the read lock in Join()", Thread.CurrentThread.Name);
                userDatabaseLock.ExitReadLock();
            }
        }