Saturday, March 21, 2015

Need to run many instances of your project in visual studio?


Solution is simple.

Right click on your solution
Set Startup Projects
Select ‘Multiple startup projects
And select ‘Start’ option for your projects in the list.

And now when you hit start button, all the project you selected will be started.

Well, you need several instances of the same project to be started?
Right click on the project
Debug > Start New Instance

(This option will be useful to interestingly try out the Chat Application we developed previously. Because with more chat clients, it will be more interesting )

Friday, March 20, 2015

Building .NET Remoting sample (Step by Step)


I am new to .Net and for one of my projects I had to use .NET Remoting. It was bit hard to find an end to end sample for a beginner even though there are code snippets describing each functionality. So I felt that it will be useful to share this with you.

I am going to give step by step details so that someone who is not familiar with visual studio can also follow and build the sample without any issue. (I am going to use visual studio which is most widely used IDE for .NET)

.NET Remoting is a mechanism that is used to access the remote services across network. (If you are familiar with WebServices the requirement which Remoting fulfills is similar to a WebService. But Remoting works with remote objects)

We are going to implement a simple chat service using .NET Remoting. We will need to implement 3 modules for this. 
  • Shared Library
  • Server side implementation
  • Client side implementation 


Implementing Shared Library

 
We will first create the shared library because both the client module and the server module is going to need it.
The shared library is used to define the remote objects that one party is going to expose to the other party. In our chat example we will need a remote object that server expose to client which allows sending messages. And another object that client expose to server which allows receiving messages. (All this will be very clear when you start building the project, so don’t worry  )
 
So let’s start creating the project.
 
File > New > Project
Select ‘Class Library’ and give the name as ‘ChatLib’ in the field below.





We need to have 2 Interfaces for the two remote objects exposed by the server and client. So let’s create two classes ClientLib and ServerLib in our Chat lib project.

In solution explorer, right click on ChatLib
Add > New Item > Interface
And give the name for the interface in the textbox below. (In this sample I am going to use IService for the interface name)






Now add the methods that we are going to expose
Register
SendMessage


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatLib
{
    public interface IService
    {
        void register(string userName, string url);
        void sendMessage(string userName, string msg);
    }
}


Create interface IClientService as same as above and add the method
receiveMessage


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChatLib
{
    public interface IClientService
    {
        void receiveMessage(string sender, string msg);
    }
}


Now you are done with the ChatLib component

Server Side Implementation

Now lets add the server side code. For this do right click on solution




Add > New Project > Console App
And give the name as ServerApp




The new project will be visible along with ChatLib under solution view.

Since we are going to have the implementation of the IService in this module we need to add ChatLib as a reference to our new project. To do that right click on ServerApp

Add Reference > Solution > Projects
Tick ChatLib project which is under Solutions > Projects

We need one more reference
Assemblies > Framework
Tick System.Runtime.Remoting




Here is the code for ServerApp (to make it easy I am going to add the whole code to one file here)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ChatLib;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace ServerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel channel = new TcpChannel(9443);
            ChannelServices.RegisterChannel(channel, true);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server),
                "ChatServerService", WellKnownObjectMode.Singleton);

            System.Console.WriteLine("Server...... press any key");
            System.Console.ReadLine();
        }
    }

    public class Server  : System.MarshalByRefObject, IService
    {
        public Dictionary users = new Dictionary();
        public TcpChannel channel;

        public Server()
        {
            channel = new TcpChannel();
        }
        

        public void register(string userName, string port)
        {
            users.Add(userName, port);
            Console.WriteLine("user added: " + userName);     
        }

        public void sendMessage(string sender, string msg)
        {
            Console.WriteLine("msg : " + msg + " received from " + sender);

            foreach(var item in users)
            {
                //send msg to all clients 
                IClientService clientService = (IClientService)Activator.GetObject(typeof(IClientService),
                            "tcp://localhost:" + item.Value + "/ChatClientService");
                if (item.Key != sender)
                    clientService.receiveMessage(sender, msg);
            }
        }
    }
}


Now as the same way you created the ServerApp, create another console application for the ClientApp too and add the same references (ChatLib and System.Runtime.Remoting )


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChatLib;
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace ClientApp
{
    class Program 
    {
        static string userName;

        static void Main(string[] args)
        {
            Console.WriteLine("Enter username:");
            userName = Console.ReadLine();
           
            Console.WriteLine("Enter port:");
            string port = Console.ReadLine();

            Program program = new Program();
            program.register(port);

            Console.WriteLine("Now you can send messages");
            program.sendMessage();
            Console.ReadLine();
        }

        public void register(string port)
        {
            TcpChannel clientChannel = new TcpChannel(Convert.ToInt32(port));
            ChannelServices.RegisterChannel(clientChannel, true);

            ClientService clientLib = new ClientService();
            RemotingServices.Marshal(clientLib, "ChatClientService", typeof(ClientService));

            IService service = (IService)Activator.GetObject(typeof(IService),
               "tcp://localhost:9443/ChatServerService");

            service.register(userName, port);
            
        }

        public void sendMessage()
        {
            Console.WriteLine("mmmmm");
            IService service = (IService)Activator.GetObject(typeof(IService),
               "tcp://localhost:9443/ChatServerService");
            
            while (true)
            {
                Console.WriteLine("me:");
                string msg = Console.ReadLine();
                service.sendMessage(userName, msg);
            }
        }
        
    }

    public class ClientService : MarshalByRefObject, IClientService
    {
        public void receiveMessage(string sender, string msg)
        {
            Console.WriteLine(sender + " : " + msg);
        }
    }
}

OK... You are done with your chat application with .NET Remoting. Now you can run the application and see.





Sunday, March 8, 2015

I love Traveling...






Sri Lanka (Home Country)
Thailand
Idaho (US)
Seattle (US)
Italy
India
Vatican City
Portugal
Spain
France
Belgium

Thursday, February 19, 2015

Changing font style in Tumblr posts

Tumblr is a simple and nice microblogging site. I wanted to use it for very short posts which I think are important. But when I started using Tumblr for this I felt that the font size of the posts are bit bigger. ( This may not be the case if you create posts with a considerable content. But it my case I mostly wanted to post few lines of posts, so the title of the posts looked bit bigger than expected)

I could not find a simple setting to change the font size of the post titles, but I could find a simple way  to do that by editing the html code.

1. Go to the home page of your Tumblr account and click on 'Customize' menu



2. In the left side panel, select 'Edit HTML'


3. Search for 'block:Posts' section in the html code. (use ctrl + f to get the search box )
Then you can see the 'block:Title' section in that. You can add the font style that you need for the title of the posts in that section.


4. You can use 'Update Preview' button and see how the new font style looks like.
Make the changes you need and hit 'Save' and you have what you need.

Sunday, January 25, 2015

Issue in put files to hadoop - 0 datanodes running

I setup hadoop as I have described in my previous posts. While I was working in some interesting projects on Map-Reduce, I faced an issue on sending some of my local files to the hdfs node. I have started dfs already, but exception says
14/10/25 17:46:20 WARN hdfs.DFSClient: DataStreamer Exception org.apache.hadoop.ipc.RemoteException(java.io.IOException): File /user/ashansa/input._COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There are 0 datanode(s) running and no node(s) are excluded in this operation. at org.apache.hadoop.hdfs.server.blockmanagement.BlockManager.chooseTarget(BlockManager.java:1471) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getAdditionalBlock(FSNamesystem.java:2791) at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.addBlock(NameNodeRpcServer.java:606) at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.addBlock(ClientNamenodeProtocolServerSideTranslatorPB.java:455) at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java) at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:585)
I faced this issue several times, so I think it will be helpful if I tell you how to overcome this issue. ( I believe this is a common issue we get while running hdfs, since I got the same issue several times and I found that many others also have faced the same issue )

What worked for me?


1. Stop the hadoop cluster with
             bin/stop-all.sh

2. Clean hadoop tmp directory
    You can find the path to your hadoop tmp directory in hdfs-site.xml.
    Check for the following tag in hdfs-site.xml file.
             <name>hadoop.tmp.dir</name>
             <value>root/data/hdfstmp</value>
    You can find the path to your hadoop temp directory in <value> tag.

3. Format node
             bin/hadoop namenode -format

4. Start hadoop cluster
             bin/start-all.sh

With these few simple steps, I was able to overcome this issue.
Hope this will help you too.

Friday, November 7, 2014

Specify download directory at the time of downloading - Safari

When I download something I used to save it to a relevant directory in my machine. When I moved to Mac and to Safari, one of the most inconvenient things I experienced was that Safari download everything to the download directory and I cannot choose the place at the time of downloading (Even though I can change the path from downloads directory to some other directory, it downloads everything to that and it was not the thing I was looking for)

After much trouble going through different sources finally found something reasonable. Since I saw that there are many more people out there with the same problem thought that it would be useful to share this simple tip (though the tip is simple, it was very useful to me + had to spend a considerable amount of time to find this out)

So simply if you need to specify the download directory at the time of downloading, without just clicking the link to download

Right click on link and choose ‘Download Linked File As…’

This will do the trick.
Hope this would make your life easier with Safari ☺

Saturday, November 1, 2014

Run a jar file in command line

This is a very simple and short post on running a jar file in command line.
Simplest command that you can try is
java –jar [jarFileName].jar

But you may need to have the MANIFEST file in the jar to run it simply with the above command.

So how to bundle MANIFEST.MF to your jar?

You can add the following maven plugin to your pom.xml and get it done. Remember to add the main class name of your app here.




How to run the jars with external dependencies?

If you need to use the simple command java –jar [jarFileName].jar to run your application which has external dependencies, you can bundle the dependencies you need to the executable jar itself. Again use the below maven plugin to get it done.




The highlighted part is doing this for you and the part above is to bundle MANIFEST.MF file to that jar file too.