Tuesday, May 17, 2011

HUAWEI mobile connect - USB Fake driver

Two days ago I have bought mobile internet for my laptop from Swisscom (the biggest swiss telco company), and I have got a HUAWEI modem for that.

I went home happily and installed the  "Unlimited Data Manager". After the installation it prompted me to restart and I said yes. Well, the first bad sign was, that after the restart my Windows 7 machine just showed me a black screen. After waiting for a while I have forced the computer to shut down and I have started it again. This time it has started but when I plugged in my beautiful new HUAWEI modem it was not recognized correctly (in the device manager I saw HUAWEU mobile connect - 3G USB Fake) and I had no access to the internet.

As a blond girl my first attempt was to restart the machine. Well, the second symptom was that my machine did not want to shut down any more. After waiting for a while it just  crashed while shutting down (blue screen, dumping physical memory to disk....).

Then my machine did not really want to start. I took him ages. If the modem was plugged in while starting then at least it was properly recognized. But if I took it out and then put it back nothing again.

So all in all the situation looked like the following: I could connect with my HUAWEI modem to the internet only if I have freshly restarted my machine while the modem was plugged in. However my machine got sick somehow and it did not want to shut down nor did it want to turn on so a restart took half an hour at least (and this is not exaggeration at all....). 

So after two days of struggling, almost getting a nervous breakdown, crying a bit, wanting to throw my machine out the window and so on I have found the solution. I have found it here, but it is deeply buried in the comments, and I thought that it definitely deserves to be in a blog post.

So the reason why my HUAWEI modem drivers could not be installed correctly, is that I have also some VMware products installed on my machine. Apparently VMware and HUAWEI modem do not like each other at all.

The solution was to stop the following service: VMware USB Arbitration Service. I did not only stop it, but set the startup type to manual. Then I have rebooted my system once again, and this time it was willing to shut down and turn on again. After my machine was up, I plugged in the modem, and the drivers were successfully installed and I could connect to the internet. After that I was so happy that I thought I will write a whole blog post about it. :)

Friday, February 4, 2011

Efficient reporting in a web application

I know that everyone who has a webpage has Google Analytics as well, and monitors the visitors, clicks and many-many things. Google Analytics is free and cool and really great. (BTW, if you would like to become a real expert in web analitycs check out this blog.) However I have missed one thing very much: I can not track with it how users actually move through my site. I mean that I would like to see that a user arrives to my site, first she views page A, from page A she goes to page B and then to page C and then she leaves. If you want to understand your users I think it is nice to be able to see how they exactly move from page to page. Because of this, I have decided that in addition to using Analytics I will as well do a very simple reporting thing that will basically log every request with a user id and like this I will be able to check user movements. :)

My biggest concern was performance though. I did not want to make a database write for every request, as I thought it would make the read requests slower. So here is what I have done:

I have created a UserActivity table with columns like: IPAddress, Uid, UserName, CretOn, Url. If a user is logged in then I put the username in the UserName column, it is empty otherwise. If user is not logged in only Uid is filled out with a guid I generate for everyone who visits the site for the first time. Than I store the Uid in a persistant cookie, so that I can track the movement of unauthenticated users as well.

Ok so, the interesting part is how do I insert the data in this UserActivity table?
  • Reporter class: Reporter class has an Activities property that is a list of UserActivity. The class has an Add method that puts an activity in the Activities list, and it has a Report method, that inserts everything into the database from the Activities list and empties the Activities.
            public void Add(UserActivity activity) {
                lock (this)
                {
                    Activities.Add(activity);
                    if (Activities.Count >= ReportingTreshhold)
                    {
                        Monitor.PulseAll(this);
                    }
                }
            }
    
            public void Report() {
                List copyOfActivities;
                while (true)
                {
                    try
                    {
                        lock (this)
                        {
                            Monitor.Wait(this);
                            copyOfActivities = Activities;
                            Activities = new List();
                        }
                        // Report the Activity to the database
                        ReportingEntities reportingdb = new ReportingEntities();
                        foreach (var item in copyOfActivities)
                 {
                            reportingdb.UserActivities.AddObject(item);
                        }
                        reportingdb.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.RaiseErrorSignal(ex);
                    }
                }
            }
    
  • Start a background thread in Global.asax: For the reporting thing to run I start a background thread when the application starts in the Global.asax.
       Thread reporterThread = new Thread(new ThreadStart(Reporter.Report));
       reporterThread.Start();
    
  • As I have an ASP.NET MVC application I have created a ReprotingFilter that runs on every request and adds a UserActivity to the Activities list.
            public void OnActionExecuted(ActionExecutedContext filterContext)
            {
                UserActivity activity = new UserActivity();
                HttpRequest currentRequest = HttpContext.Current.Request;
                // We filter out search engines
                if(!currentRequest.Browser.Crawler){
    
                    // Create the UserActivity Object here...
      
                    Application.Reporter.Add(activity);
                }
    
            }
    
Actually I am absolutely satisfied so far with this reporting thing. My site is still quick, user requests are now logged. There was only one big mistake I have made for the fist time: Do not forget to filter out search bot requests, otherwise you will see logged tons of requests from search engines!

Sunday, January 16, 2011

How Google, Facebook and forms authentication can work together?

According to my experience a real word authentication thing is till not very easy if you want to support 3rd party logins (like Google and Facebook) and traditional forms authentication and you want them to work together. So what do I mean by working together and real world?
  • In the real word you will want to collect some extra information about users when they register or login via 3rd party for the first time. For example in my case I make a beauty site so I collect user's skin type and biggest skin concern.
  • It is nice if different login options can work together: users can connect their accounts to the one profile they have on your site and after connecting the accounts they can log in with whatever method they want. This is especially needed if you add 3rd party login support after the site is launched and you have already users registered in the traditional way.
So how do you implement something like this with ASP.NET MVC?
  • You need for this the right database structure: To support forms authentication I just use what comes out of the box, the asp.net membership system with sql membership provider. To collect extra information about the users I have created my own user table that contains skin type, skin concern, nick name and so on. To support the one profile can have many logins I have created a connection table that has two columns: nick name that points to my own user table and username that points to the user in the membership system.
  • In case of 3rd party login I also create a membership entry for that login. For username I use the unique identifier of the user I get back from Google or Facebook. Like this I know if user is logging in for the first time or not: if the unique identifier has already a membership entry, it is not the first time otherwise it is.
  • If it is a first time login I tell the user to create a profile on my page or ask if she has already a profile. If the user creates a profile I make an entry in UserProfile and also in the UserNickConnection table. If user has already a profile, I ask her to authenticate as before and then I just have to make a new entry in the UserNickConnection table. After the accounts are connected user can log with whatever method she wants.
Ok, so this was the high level overview of the thing. To get started more concreatly the following things are needed: