Install latest skype 2.1 beta failed on Ubuntu 8.04

August 5, 2010 Leave a comment

A lib dependency is missing: libasound2 when installing latest skype version 2.1 Beta on my Ubuntu 8.04. even after I reinstalled libasound2 and libasound-plugins, it gives me the same error.

Did quite a bit search, seems that in 2.1 beta release, skype added some new sound related lib which is not supported in Ubuntu 8.04.
The problem is skype doesn’t provided any older version on their official download site, it forces you to download the latest version (which doesn’t work on certain distro/version of Linux btw).
Did another round of search, finally, in this post I found the link for skype-debian_2.0.0.68-1_i386.deb
http://skype-old-version.blogspot.com/2010/03/skype-old-version.html
This package works fine on my Ubuntu 8.04 (hardy).
Thanks for the author of the post, and if anyone can’t find the link, leave your email in the comment, I will send you a copy.

Categories: technical notes

Json Serializer and Deserializer

July 29, 2010 Leave a comment

Json stands for JavaScript Object Notation. Json is a lightweight syntax for representing data. JASON’s elegance comes from the fact that it’s a subset of the JavaScript language itself.

The typical json format {object} [array]

{

“class”: “Person”,

“name”: “William Shakespeare”,

“birthday”: -12802392000000,

“nickname”: “Bill”

“phoneNumbers”: [

{

"class": "Phone",

"name": "cell",

"number": "555-123-4567"

},

{

"class": "Phone",

"name": "home",

"number": "555-987-6543"

},

{

"class": "Phone",

"name": "work",

"number": "555-678-3542"

}

]

}

You can follow the JSON format and easily convert objects to JSON string objects.

// Create a new instance of Person object

Person p = new Person(5, “Bilal Haidar”);

// Construct the JSON string object

StringBuilder sb = new StringBuilder();

sb.Append(“{“);

sb.AppendFormat(“\”{0}\”: \”{1}\”", p.ID, p.Name);

sb.Append(“}”);

Response.Write(sb.ToString());

Then on the client side, you just need to parse the JSON string object into a real JSON object to be accessed as a normal object.

function ReadyStateChangeHandler()
{
if (xmlHttpRequest.readyState == 4)
// Completed {
if (xmlHttpRequest.status == 200)
// Response OK {
// Process data
var jsonStringObj = xmlHttpRequest.responseText;
// Parse the JSON string object to a real JSON object
var jsonObj = jsonStringObj.parseJSON();
// display some data
alert(“Person ID: ” + jsonObj.ID.toJSONString());
}
else
{
alert(“Error: HTTP ” + xmlHttpRequest.status + ” ” +
xmlHttpRequest.statusText);
}
}
}

Reference:

  1. Introducing JSON, describes the syntax of JSON, http://json.org/
  2. JSON serialization and Deserialization in ASP.Net, http://www.codedigest.com/CodeDigest/112-JSON-Serialization-and-Deserialization-in-ASP-Net.aspx
  3. An ActionResult to return JSON from ASP.NET MVC to the browser using Json.NET, http://code.google.com/p/sharp-architecture/source/browse/trunk/src/SharpArch/SharpArch.Web/JsonNet/JsonNetResult.cs?spec=svn370&r=266
  4. API Documentation, http://james.newtonking.com/projects/json/help/html/N_Newtonsoft_Json.htm
Categories: daily notes

Design Pattern 3: the Command Pattern

June 27, 2010 Leave a comment

The command pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

When you need to decouple an object making requests[Invoker of a request] from the objects that know how to perform the requests[Receiver of the request], use the command pattern.

The command object contains both receiver and actions. The command object provides one method, execute(), which encapsulates the actions and can be called to invoke the actions on the Receiver.

The client is responsible for creating a ConcreateCommand and setting its Receiver.

The Invoker holds a command and at some point asks the command to carry out a request by calling its execute() method.

The Receiver knows how to perform the work needed to carry out the request. Any class can act as a Receiver.

Reference:

  1. Definition, participants, and sample code in C#, http://www.dofactory.com/Patterns/PatternCommand.aspx#_self1
Categories: Design Patterns

How to use the Selenium-RC to test your first web application in Windows

June 24, 2010 Leave a comment

Tools needed:

Java run time environment: Jre6 http://www.java.com/en/download/manual.jsp

Selenium-RC, http://seleniumhq.org/download/

Nunit

 

  1. Install Jre6 and add java path to the environment, echo %path% to verify
  2. Install the selenium-rc server.
  • Go to the directory where selenium-rc’s server is located and run the following, “java –jar selenium-server.jar”
  • If an “Unrecognized windows socket error exception” is thrown, using “netstats -a” to verify if port 4444(default selenium socket port) is already in use. More details, please refer to http://serverfault.com/questions/124022/selenium-server-wont-start
  1. Creating a batch (.bat) file to execute step 2.
  2. Open visual studio(or other preferred IDE) to create a new test project, select new a class library.
  3. Add references to the following DLLs: nmock.dll, nunit.core.dll, nunit. framework.dll, ThoughtWorks.Selenium.Core.dll, ThoughtWorks.Selenium. IntegrationTests.dll and ThoughtWorks.Selenium.UnitTests.dll. See http://seleniumhq.org/docs/appendix_installing_dotnet_driver_client.html#configuring-selenium-rc-net-reference
  4. Write the selenium test in a .net language, or export a script from selenium IDE to a C# and copy the code into the class file just created.
  5. Open Nunit. Open the test project.
  6. Run the test from the visual studio Nunit GUI or from the command line.

 

Reference:

  1. Selenium-RC document, http://seleniumhq.org/docs/05_selenium_rc.html
  2. Selenium RC in C# using Nunit, an end to end example, http://thetestingblog.com/2009/09/10/selenium-rc-in-c-using-nunit-an-end-to-end-example/
Categories: technical notes

Design Pattern 2: Double Dispatch and Visitor Pattern

June 22, 2010 Leave a comment

I will start with an example in wiki about the double dispatch, http://en.wikipedia.org/wiki/Double_dispatch.
Given the following C++ code,

class SpaceShip {};
class GiantSpaceShip : public SpaceShip {};
class Asteroid {
public:
virtual void CollideWith(SpaceShip&)
{
cout << "Asteroid hit a SpaceShip" << endl;
}
virtual void CollideWith(GiantSpaceShip&)
{
cout << "Asteroid hit a GiantSpaceShip" << endl;
}
};
class ExplodingAsteroid : public Asteroid
{
public: 
virtual void CollideWith(SpaceShip&)
{
cout << "ExplodingAsteroid hit a SpaceShip" << endl;
}
virtual void CollideWith(GiantSpaceShip&)
{
cout << "ExplodingAsteroid hit a GiantSpaceShip" << endl;
}
};

If we want to print out sentences like this, Asteroid hit a GiantSpaceShip and ExplodingAsteroid hit a GiantSpaceShip and we try the following, it won’t work any way.
Asteroid asteroid;
Asteroid& theAsteroidReference = ExploadingAsteroid();
Spaceship& theGiantSpaceshipReference = GiantSpaceship();
asteroid.CollideWith(theGiantSpaceship);
theAsteroidReference.CollideWith(theGiantSpaceship);

The key is that while virtual functions are dispatched dynamically in C++, function overloading is done statically. The above problem could be solved if we use a visitor pattern.
virtual void CollideWith( Asteroid& inAsteroid)
{ inAsteroid.CollideWith(*this); }

As a summary, virtual function or function overloading implements the single dispatch. It is determined by only one argument. If calling a method is determined by both, then it is double dispatch. And for a more official definition, visit http://www.c2.com/cgi/wiki?DoubleDispatch.
Next, I will move to the visitor pattern.
I could not understand the visit pattern at all until I found this article, http://butunclebob.com/ArticleS.UncleBob.IuseVisitor.

Emp# Name QTD-hours QTD-pay
1429 Bob Martin 432 $22,576
1532 James Grenning 490 $28,776

If we want to print hourly rate for each employee, what we could do is that create an employee class and for each employee class, we contain an hour rate class or by extending the current employee class. However, this will couple the format and content of a table to the Employee.
So another to do that is we have employee and table/report class (visitor in this case).

public class Employee 
{  
public abstract void accept(EmployeeVisitor v);
}
public class HourlyEmployee extends Employee
{
public void accept(EmployeeVisitor v){v.visit(this);}
}
interface EmployeeVisitor {
public void visit(HourlyEmployee he);
public void visit(SalariedEmployee se);
}
public class QtdHoursAndPayReport implements EmployeeVisitor
{
public void visit(HourlyEmployee he)
{
// generate the line of the report.
}
public void visit(SalariedEmployee se) {} // do nothing}
}

To gernerate the report now, we use,
QtdHoursAndPayReport v = new QtdHoursAndPayReport();
for (...) // each employee e
{
e.accept(v);
}

The visitor pattern demonstrated above falls into the following UML.

Categories: Design Patterns

Design Pattern 1: Factory Method, Abstract Factory and Abstract server

June 8, 2010 Leave a comment

Design patterns are common solutions to common problems. They also have a particular context in which they fit.

Factory method and Abstract Factory, both patterns encapsulate object creation and allow you to decouple your code from concrete types. They are important as we have a DIP principle: Dependency Inversion Principle, which guide us to keep things abstract whenever possible. “Depend on abstractions. Do not depend on concrete classes.”

Abstract Factory provide an interface for creating families of related or dependent objects without specifying their concrete classes; Factory method defines an interface for creating an object, but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to the subclasses.

A factory method seems always lurk inside an abstract factory pattern, which makes them confused and hard to differentiate. There are several differences between them as concluded in “Head First Design Patterns” P159. Both patterns create objects. Factory method does it through inheritance, using a subclass to do the creation, and abstract factory does it through object composition, providing an abstract type for creating a family of products. In abstract factory, subclasses of this type define how those products are produced. Apparently abstract factory has one advantage of grouping together a set of related products.

Use abstract factory whenever you have families of products you need to create and you want to make sure your clients create products that belong together.

Use factory method to decouple client code from the concrete class you need to instantiate.

Reference:

  1. Example , http://www.netobjectives.com/PatternRepository/index.php?title=TheAbstractFactoryPattern
  2. Further reading, http://sourcemaking.com/design_patterns/abstract_factory

When a client depends directly on a server, the DIP is violated. Changes to the server will propagate to the client, and the client will be unable to easily use similar servers. This can be rectified by inserting an abstract interface between the client and the server. In the following, button is a client and light is a server. After using the abstract server pattern, client can easily use other similar server such as “turn on ” the Fan.

Reference:

1. A very good article, http://today.java.net/pub/a/today/2004/06/8/patterns.html

Categories: Design Patterns

Short C++ Interview Questions (Updating)

March 24, 2010 Leave a comment

1.class Sample
{
public:
        int *ptr;
        Sample(int i)
        {
        ptr = new int(i);
        }
        ~Sample()
        {
        delete ptr;
        }
        void PrintVal()
        {
        cout << “The value is ” << *ptr;
        }
};
void SomeFunc(Sample x)
{
cout « “Say i am in someFunc ” « endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
Answer:

Say i am in someFunc
Null pointer assignment(Run-time error)
Explanation:

As the object is passed by value to SomeFunc the destructor of the object is called when the control

returns from the function. So when PrintVal is called it meets up with ptr that has been freed.The

solution is to pass the Sample object by reference to SomeFunc:

2. void main()
{
 int a, *pa, &ra;
 pa = &a;
 ra = a;
 cout <<”a=”<<a <<”*pa=”<<*pa <<”ra”<<ra ;
}
Answer:

Compiler Error: ‘ra’,reference must be initialized
Explanation:

Pointers are different from references. One of the main differences is that the pointers can be both

initialized and assigned, whereas references can only be initialized. So this code issues an error.
3. const int size = 5;
void print(int *ptr)
{
 cout<<ptr[0];
}

void print(int ptr[size])
{
 cout<<ptr[0];
}

void main()
{
 int a[size] = {1,2,3,4,5};
 int *b = new int(size);
 print(a);
 print(b);
}
Answer:

Compiler Error : function ‘void print(int *)’ already has a body
Explanation:

Arrays cannot be passed to functions, only pointers (for arrays, base addresses) can be passed. So

the arguments int *ptr and int prt[size] have no difference as function arguments. In other words,

both the functoins have the same signature and so cannot be overloaded.

4. class opOverload{
public:
 bool operator==(opOverload temp);
};

bool opOverload::operator==(opOverload temp){
 if(*this  == temp ){
  cout<<”The both are same objects\n”;
  return true;
 }
 else{
  cout<<”The both are different\n”;
  return false;
 }
}

void main(){
 opOverload a1, a2;
 a1 == a2;
}
Answer :

Runtime Error: Stack Overflow
Explanation:

Just like normal functions, operator functions can be called recursively. This program just

illustrates that point, by calling the operator == function recursively, leading to an infinite

loop.

Categories: Interview Questions
Follow

Get every new post delivered to your Inbox.