geek

All posts tagged geek

Hello,

Today on my linux box i tried shooting Cheese app after many days. Apparently to my surprise it failed to load couple of times. Immediately launching the same app via Terminal shows me that it cant load shared library libcogl.

The error shows like below:

libcogl.so.5: cannot open shared object file: No such file or directory

On googling, i came to know i need to re-compile x11-libs/xm package. Which i was unsure of why i should :O

To my surprise the solution is very simple and yet i failed to rectify it myself. Below is the solution:

  1. Log in as root (via command line also no problem)
  2. Run below commands:

ln /usr/lib/libffi.so /usr/lib/libffi.so.5
ln /usr/lib/libcogl.so /usr/lib/libcogl.so.5

Thanks,
Zen :)

Dear Reader,

Today i am very happy to write about the upgrade i did on my laptop last night. As always, upgrading my boxes to the new version is an exciting thing to me. Not just because its super easy, it’s because the new things i am going to experience about it and the post tweaking stuffs i am supposed to do. It’s always brings excitation on the table. I must say, i am proud zenwalk user since past 4-5 years, and ever since it always kept me happy and made me proud user of it.

Anyways, zenwalk 7.2 released after almost 7 months or so. So i previously had 7.0 which had xfce 4.8 on it. XFCE 4.8 infact had very good interface and good application as well. It was fast, slick and snappy ;) . But hey common, who doesn’t need a new version every now and then? But for me it was never a mandatory to look for an updates. I was very happy with the performance of XFCE, Zenwalk, Kernel 2.6 and its total performance.

So downloaded the new ISO file (700MB), this is one of those reason i love about zenwalk. That is, its philosophy: “One App per Task”. However, in the new 7.2 Live version this has been ruled out, hence live version is available only on DVD style. Anyhow, once downloaded, backed up all my tweaking settings and even had to brainstorm a bit to remember what ever else changes done. Once all done, off to installation.

Installation is pretty much the same old style (CLI) since zenwalk 6 or earlier, but amigo, do not make a weird face reading this line. Though you might think WTF about this, but still its very easy to install and very simple. I again applaud for this great job done by the zenwalk folks. One such screen shot is below:

Courtesy: http://www.linuxbsdos.com/2011/04/13/zenwalk-7-review/

The new version installation is done almost within 20 minutes or so (didn’t calculate accurately though). Once installation is done and remaining configuration is done, the system restarts within 21 secs approx till login screen on my i7, 4 GB laptop. That’s a pretty much surprise and happy news. I never estimated it with 7.0 earlier, but with 7.2 its definitely better, faster and i am happy about it.

The redesigned login screen was pretty much a surprising thing to me as soon as first reboot was done. And i am happy to share the screen with you. I am sure you shall love it :)

Once logged in, did couple of tweaking as per my needs. Next step was to wander around and see whats working and whats not in the new version. The first thing i noticed was the new revamped interface design of XFCE 4.10. The design, theme and icons, etc. are really awesome. I just wandered around it for a while. I must say great job folks.

Next impressive change is network app. Both wireless and wired interfaces were configured automatically and loaded by the network manager. Immediately i noticed that in 7.2, zenwalk team has chosen network manager applet for managing networks and i must say its way better than WICD used in the 7.0 and earlier. WICD did give me some pain in the ass moments when i was in 7.0.

Zenwalk 7.2 comes out with Firefox 15 and immediately i upgraded to 16 from the repos. So its pretty good to see a support for the latest version of my favorite browser. Next few things i wander around and noticed was my graphics chip recognized and working good compared to 7.0. So by default zenwalk 7.2 supports nouveau driver, but i still preferred official nvidia driver and hence installed it because it provides a configuration app for me to do some tweakings.

Next step was to see if sound is identified and voila, it’s there and working kool. So immediately i see if music app provided or upgraded is good? Yep there it was Gmusicbrowser, I must admit that i never used it before though i remember it being supported in the older versions, so i always preferred exaile so far. But this one seem to provide same kind of features and the whole UI is highly customizable, so its kool. My touchpad is now officially recognized by the kernel and appropriate driver is loaded as well as XFCE provides more mouse/touchpad options for it, hence again its kool :) .

Last but not least, one problem i found is that my bluetooth isn’t working OOTB (Out of the box). I have reported to the zenwalk team and hopefully i shall get a solution soon, but it’s not a show stopper issue. So i am not left disappointed. :)

There is a lot to say, but at this moment i am still exploring. So far i could see a lot improvements in terms of applications, artwork, performance, memory, etc.. Almost all apps i came across so far is upgraded to the new versions and they have their own improvements in terms of usability and bugs fixes.

Hence all in all, i am very much impressed and again happy. All hail to teams of kernel, zenwalk, xfce and other apps. Kudos to all of them for their great hard work.

Thanks & Happy Linux-ing,
Zen :)

Dear Reader,

I am working on a legacy code base which not only is quite challenging but troublesome as well, sometimes shit, sometimes pleasant, all in all its a roller coaster ride for me. So lately i picked up a task of implementing a feature where in had to provide a functionality of Import and Exporting some data by other indirect means i.e no manual user intervention. Based on my analysis, i found out that this feature is already present for one particular type via GUI intervention. Now i need to replicate this feature as a background activity.

Yes it might sound like what is the big fuss to blog about it? Right, even before writing this blog i though of this point many times. But i did face 1 challenge in solving this problem w.r.t design issue which i am going to share it soon. But before let me give you a brief overview about the current design and then later i shall share how i got stuck for like half day thinking about possible best solution.

Below image shows the current class design:

As you can see from the above hierarchy design structure which looks pretty much good, but for this feature which i am implementing poses a challenge because this feature which is already provided via a manual invocation via GUI menu is only supported for Master Types specifically supported for MasterC type. But unfortunately the earlier developer didn’t see this through well and implemented all the code in base class i.e BaseType.

But the similar feature which i have to implement should only be supported by all Master types which is almost similar to already implemented for MasterC, but remember the full implementation is in BaseType which logically is wrong but i am guessing that original developer didn’t had the requirement at that time of supporting all other derived types, but still it was wrong.

Any way, to implement my feature for all Master Types i had this problem of how to override this already implemented code which is in BaseType.  So i came up with couple of possible solution which has pro’s and con’s. Lemme list them below:

Solution 1:

  •  Just move the code (marking that method as virtual and override in MasterC type) which is already implemented for MasterC from BaseType to MasterC, because as of now it just only supported for MasterC as far as i know.
  • Very simple and easy to do, but has problems.
  • Product is legacy, so its high risk to move it. I never know which corner test case i could break.
  • Overriding this feature in MasterC, poses another problem where in, the feature which i am implementing should world in the background as well as same functionality (Import and Export) should work when manually invoked via GUI menu for MasterC.
  • So overriding this method for my feature will break the manual intervention feature.

Solution 2:

  • Implement subclass for this feature for MasterC, lets call the feature as FeatureX, thus naming the class as MasterCFetureX as shown, since MasterC is the odd type here having both features to be supported.

  • This way, as thought in solution 1, i can mark that method as virtual and override in MasterCFeatureX to execute a customize version of the same code already implemented for my requirement/feature and when manual invocation is done which is already existing feature for MasterC, then the virtual method in MasterC will get execute. This way, i get what i want and existing functionality is not broken.
  • But the problem is, the source where the object MasterC is created based on the menu action is not implemented via any creational patterns viz Factory. If it had been, for this sub menu item action i could create object of MasterCFeatureX an played with it. But since it is not there, implementing such patten at the UI level till business is  huge and very risky because changes and impact is huge in terms of effort and time.
  • Another problem is the level of hierarchy could grow more, thus making code maintainability tedious.
  • Many test cases are risk to be broken if chosen this way. Hence dropped.

Solution 3:

  • Very simple and straightforward but crude and filthy which i myself still do not like. But i am forced, so that feature gets delivered in time.
  • The method which is already implemented BaseType takes an argument (some container from another library). So i added a enum type called ViewFeatureTypes which had None, FeatureX, FeatureY, etc. into this container. Now when i am invoking this call for my feature from View, i set this enum type value in the container and check this value in the code in BaseType.
  • This enum can be used in various other places where i am sure such design constraints exists. So for future purposes added Enum than a bool member to this container type.
  • As said, very safe and simple yet crude and dirty. :x

Thanks & Happy Designing,
Zen :)

P.S: Please drop your feedback. I am not a good designer, but see myself there in career. So any suggestions are welcome.

Dear All,

Tired of making sure you have not done any RESERVED checked out in your development branch whenever you wish to check out a file every time? Or you have always got irritated to uncheck the option (manually) Reserved in the Clear case Checkout dialog whenever you wish to check out a file especially when all your team members share same branch for development.

Do not worry, you can permanently disable automatic reserved check out in the clear case plug-in of Visual studio as shown in the below screen shot. Just uncheck Reserved option as highlighted in the below screen.

Steps to do:

  1. Go to Visual studio -> Tools -> Options
  2. Select Rational Clear Case -> General (As shown below)
  3. Click Clearcase User options button as shown below.


Hope it helps.

Thanks & Happy Coding :)
Zen

Dear Reader,

Today i had to do some unit testing of a class which had some private instance and static methods along with non privates in it. These methods had some good core logic in it which i determined to do unit testing. Though many articles and experts say that its ok not to do so for a private method,  but i wanted my logic to be fail proof. Hence only way to do this is to use reflection APIs to access the private members.

Below is the simulated code (for article sake) shown which needs to be tested:

class ToBeUnitTested
    {
        private void PrivateMethod()
        {
            //Some business code logic
        }

        private static void StaticMethod()
        {
            //Some business code logic
        }
    }

So, i started to write the unit testing code which had the reflection APIs to invoke these members. Instead of embedding the reflection code in my unit test class, i refactored it to a helper class as shown below:

class Helper
    {
        private Helper() { }

        public static void InvokePrivateMethod(Type type,
            string methodName, object instance, object[] parameters)
        {
            BindingFlags flags = BindingFlags.Instance | 
                BindingFlags.InvokeMethod | BindingFlags.NonPublic;

            RunMethod(type, methodName, instance, parameters, flags);
        }

        public static void InvokePrivateStaticMethod(Type type,
            string methodName, object[] parameters)
        {
            BindingFlags flags = BindingFlags.InvokeMethod |
                BindingFlags.NonPublic | BindingFlags.Static;
            RunMethod(type, methodName, null, parameters, flags);
        }        private static void RunMethod(Type type, string methodName, 
            object instance, object[] parameters, BindingFlags flags)
        {
            //Reflection code
        }
    }

As shown above code, i have couple of public helper methods for private instance and private static. Of course i had couple more for protected , etc. But shown only 2 for simplicity sake. As you can see in the above code, i have more methods for each of the method with different access modifiers in my testable class. I some how did not like the way it is done above. I just wanted the helper class to be simple and concise with only one method rather than 3 methods. The only changing thing is the BindingFlags combination. So i decided to do a bit of refactoring and having only single method RunMethod(….) in the helper class which is very simple and understandable for Helper class.

To do this refactoring, i though of using Enum to specify combination choice for PrivateInstance and PrivateStatic, etc. But BindingFlags is already an Enum. So Enum for Enum is not a good choice and makes it more difficult to understand. Hence the next thing occurred to me was to use Extension methods with Enum. I really like how the Extension method offers a great deal of flexibility and maintainability of the code.
Below is the refactored code:

public static class BindingFlagsExtension
    {
        public static BindingFlags GetInstancePrivateMethodFlags(this BindingFlags flags)
        {
            return BindingFlags.Instance |
                BindingFlags.InvokeMethod | BindingFlags.NonPublic;
        }

        public static BindingFlags GetStaticPrivateMethodFlags(this BindingFlags flags)
        {
            return BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Static;
        }
    }

    class Helper
    {
        private Helper() { }        public static void InvokeMethod(Type type, string methodName,
            object instance, object[] parameters,BindingFlags flags)
        {
            //Invoke reflection code
        }
    }

Now the helper class became very simple and very concise too, even Helper class adheres much more towards SOC principle. Even maintaining and extension of the code (SOLID principle) for Helper and Extension class is high.

In fact i feel good that i did some thing cool today, though its not amazing one. Love to hear suggestions. So please feel free to comment on other ways of improvement.

Thanks & Happy Coding,
Zen :)

English: The photograph image of Nikola Tesla ...

Nikola Tesla

Truly a great genius i always worship/admire/honor. A great man like Einstein, Newton, etc. Its awkwardly shame that the people around him was such a**holes and i feel very sorry about most of his works were stolen/lost. Even today the same kind of sick maniacs do exits around most of the genius guys out there. Hope their true hard work does not go unnoticed like Nikola Tesla‘s due to some sick bastards creepy intentions.

Hats off to you genius. Wish i too had at least 1% of your genius brains… :(

A must watch video:

Nikola Tesla Museum

Nikola Tesla Museum (Photo credit: Xevi V)

Dear Reader,

Yesterday was, for a while pretty frustrating moments for me. Because, i had to add a assembly from GAC i.e System.Data.Linq for example: Yea, so whats frustrating you might ask? Well read on.

I was working on .NET 3.5 framework in VS 2008. So as usual, i added a static reference to the assembly from Add Reference dialog of Visual Studio. Then the added assembly started to show an error in the references list as shown below:

This issue started to bug me up for a while, i was under a pressure of completing the assigned task and this issue hold me up bad.

I tried googling, i tried asking couple of smart colleagues around and no use. Next, I tried looking for the path of this assembly. I noticed a bit difference in it.

As you can see from the above screen, the assembly what i want does reside in a different path than the normal system core assemblies like System.Data, though it was a bit of surprise to me but i thought thats how its been done by M$. But this made me a bit curious as this path could be making VS to show this error or some registry contents did not got updated or some thing? Oh well, googling, and searching further did not help either, neither could my colleagues spot out the issue.

We tried changing the framework from 3.5 to 2.0 and back again, though it did solve the error, but we could not find why or whats causing this issue, because repeating this use case again did put us in the same unresolved state.

After about 2 hrs of searching and headache, i managed to find this simple thing which was causing the problem. I just had to uncheck the “client only subset…” option in the project properties.

I never knew this small feature was causing the issue though i went through this option earlier. Finally felt a bit relaxed after fixing this mysterious issue :)

Hope it was helpful to you. Happy Coding..
Zen :)

Dear Reader,

Today i got a bit curious about AppDomains concept so i began to go indepth to learn about it. As we know AppDomains do play a vital role in the application developement. These .net feature do comes in handy in various situations. One such situation i would like to share with you from my previous project.

We had to monitor multiple ports parallely in a industrial PC via our software for some inputs, based on which we had to do some other processing. As you might think off, we could have done this via creating multple threads. But the constraints we had is to have the latency of just 5 ms difference in getting values. Our POC on thread showed that (.net 3.5, so no TPL), some times the latency in getting values from some ports (max 6 ports) when loaded full was taking more than 1 sec to get the values. The problem was that some times some threads do not get time slice to get processed.

For our next POC, we chose to use AppDomains in a single process. Thus we created 6 AppDomains to monitor 6 ports and 1 default AppDomain which .NET application process creates. At first this might sounds like not light weight than a thread, because AppDomain creates more memory and has more stuffs which it creates than a thread. So what we did was to create as light as possible operations in each of these appdomains from the application point of view.

So we carefully developed an assembly which just reads the port and gives to other process running parallely which will take care of further processing such as saving to DB/File. We had to optimize this assembly code as much as possible to improve its performance. So this POC proved in most cases to be much reliable than with just threads in terms of getting accurate values every time slice as we wanted.
Other usage of this AppDomains is thats its very easy to unload the whole assembly if there is a crash/exception and re-create/load it back on as if nothing has happened which can not be done with threads, because If one of the threads throws an exception and if its not handled then whole application crashes.

Recently i had a argument with a colleague about the default AppDomains which gets created when you launch a .net application. I was arguing that it would create only 1 appdomain which is default, where as he stated that it would create 3 appdomains. I also searched CLR via C# if there is such thing going on in .net, alas could not find it. I think he got confused from the native appdomains with the .net world.

So today i was just rethinking about the same argument and wanted to dig more and see if i can get all the currently running appdomains from the process to test his argument. So i looked for how to do that. So below is the code i learned on how to do it. So i started to play with the code for a bit.

Below code is for .NET 4 version.

using System;
using mscoree;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static object o = new object();

        static void Main(string[] args)
        {
            AppDomain appDom = AppDomain.CreateDomain("Domain1");
            AppDomain appDom1 = AppDomain.CreateDomain("Domain2");

            GetAppDomains();

            AppDomain.Unload(appDom);
            GetAppDomains();
            Console.ReadLine();

        }

        public static void GetAppDomains()
        {
            IntPtr enumHandle = IntPtr.Zero;
            ICorRuntimeHost host = new mscoree.CorRuntimeHost();
            try
            {
                host.EnumDomains(out enumHandle);
                object domain = null;

                while (true)
                {
                    host.NextDomain(enumHandle, out domain);
                    if (domain == null) break;
                    Console.WriteLine(((AppDomain)domain).FriendlyName);
                    Console.WriteLine(((AppDomain)domain).IsDefaultAppDomain());
                    Console.WriteLine();
                } 
            }
            catch (Exception e) { Console.WriteLine(e.ToString()); return; }
            finally
            {
                host.CloseEnum(enumHandle); Marshal.ReleaseComObject(host);
            }
        }
    }
}

For the above code to work, you have to add a static COM reference of C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb via Project -> Add References window.

This code lists out all the AppDomains created inside the process. This can help us to control AppDomanins in what ever way we require at a later point of time with out needing to the hold the same reference of original Appdomain object.

Hope it helps.

Thanks & Happy coding,

Dear Reader,

Today i was just going through some product code at work. There was this code which used multithreading concepts to do some work. At first it just went through the code without worrying much about its pitfalls. I didn’t even think there was one (how stupid of me?). But at some point down further some things started ringing some alarms in my brain. So at that point i went back to the pitfall code where my brain started give some alarms.

At this point i did not find any cranky things in the code, some thing i over saw i guess. So i started debugging because i got curious or perhaps my alarmed brain wasnt able to see the exact problems. So upon starting to debug the code step by step i suddenly realised reading some thing about locking object in Jeffrey ritchers book.

Thanks to him and my brain, to just pop that picture at that instant. I stopped debugging and began to analyze the code. Below is that production code though it’s not completely shown for obvious reasons:

static object lockObj = new object();

static void Main(string[] args)
{
    LockObject();
    try
    {
        //Some code
    }
    catch(/*Specific type*/)
    {
        //Some state recovery code
    }
    finally
    {
        Monitor.Exit(lockObj);
    }
}

public static void LockObject()
{
  Monitor.Enter(lockObj);
  //Some code here..
}

First the problem with the code is that lock entering method is called outside Try block, which could cause an exceptiom before or after obtaining the lock. This could result in ending up locking the object forever. Which is very problematic.

Second issue is that the Monitor.Lock API could throw some weird exceptions internally: this statement i recalled from Jeffreys book. So i began to go a bit depth into this API via ILDASM tool. I found that it internally uses many other API’s to do the job. First API was that it gets a resource string via Environment.GetResourceString(..), this API uses a resource file to get the data, now this can cause a failure right? That means Enter() API can cause a failure thus locking the object forever.

Hence, the best is be a bit careful here and use alternative APIs provided by the framework itself. As jeffrey suggests, use Monitor.TryEnter(..) API rather than just plain old. This will decrease the possiblity of the issue. Then put the statement inside Try block. So that there is always a cleanup taken care off by finally, thus fool proffing more than before.

Thats all my friend. Thanks for reading and thanks to Jeffrey.

Happy Coding,
Zen :)

EDIT: Further to my analysis on the same code, i once encountered an exception being thrown by Exit(..) API, this is because synchronization was not done over the locking object; in other words Enter API call did not succeed. Hence exiting on the same object will throw away exception. It is recommended to use TryEnter API via which you can make sure the object was really locked before even calling Exit API.