Install/Configure Jenkins continuous integration server on Linux.

In this blog post I will describe installing and configuring Jenkins Continuous integration server. This set-up was used for one of  my current project to build Linux Application software.

I used Ubuntu 12.04 virtual machine running inside VirtualBox for this tutorial.

Jenkins is an continuous integration server written Java . Jenkins monitor configuration management ( CVS,SVN,Perforce.. ) servers  for changes , such as source code check-ins. Once it detects changes it will update the local working directory with the source from CM and performs series of build steps defined by the user on the source code. These build steps could be simple as invoking the shell script or  build script such as make,ant .

Jenkins has many plug-ins available to extend its feature set and to integrate with other Software tools ( unit test, code coverage, code analysis ).

Jenkins is Java based software , hence it requires Java Runtime as prerequisite on a system.

Jenkins software is available as Debian package . To Install latest Jenkins software on Ubuntu  execute following steps from command line

image

Once the installation completes make sure that Jenkins server up and running by opening web browser and pointing to http://localhost:8080 .

If Jenkins running you should see following page in the web browser . This completes the installation.

image

Configuring Jenkins  with Perforce Configuration Management.

Jenkins natively support CVS and SVN CM. In my application I use Perforce for configuration management. Since Jenkins natively does not support Perforce CM I need to install Perforce plugin for Jenkins.   Installing plugin is easy, Select “Jenkins->Manage Jenkins” option from the menu , select available tab to list the plugin and select and install perforce plugin. Following picture shows the “Manage Jenkins” page.

Jenkins-inst-4-perforce

You can discover and install additional plug-in similar to perforce plugin from the page.

Configure source code build.

From the Jenkins main page select NewJob to create simple build step.

In the next page enter job name and select “Build free style software project” option . You can read about the various option by selecting help icon next to option.

Jenkins-inst-4-perforce-1

Next page allows user to enter Configuration management specific details, in case Perforce CM, it user name,password and server details as shown below. These options differ based on the selected Configuration management .

Jenkins-inst-4-perforce-2

Other configuration details such as source code depot ( Perforce specific ), workspace details , Poll interval ( how often Jenkins should poll CM for changes ), where to copy the build output   are entered on the same page . Once the Configuration Management specific details are entered next step is provide instruction Jenkins to build software.

For this demo I have selected option to “Execute Shell” option to enter build command. With this option you can enter any Linux shell command . I will Apache ant to build my source code . Jenkins will execute these command once it check-out source code from the CM . I have entered following command to illustrate the build setup

cd projecta-src/

echo “Build started…”

ant BuildRelease

echo “Build Ended”

Jenkins-inst-4-perforce-4

After saving this step Jenkins will start monitoring  Configuration Management for changes, if changes detected it will pull changes to local workspace and  execute the build scripts.

Resources:

1. Jenkins website.

2. Jenkins Plugin repository.

Advertisements

Push notification from server to client using SignalR

There are number of scenarios where we need send push notification from server to client, client gets update from the server when ever server has new data.

Some of the scenario which benefit from push notification(real-time web) are

  • Monitoring Server application over the web(Diagnostic reports or Error reports )
  • User signup notification.
  • Stock tickers.
  • Chat application.
  • Monitoring of hardware instruments.

Now we know the possible usecases ,so how can we implement server to send arbitrary data to client.

Currently there are four established technique to do push notification on web

    • Polling : In case of polling Web browser make connection to server, server responds immediately with data if present else empty response. After predefined time(polling time) Web browser makes same request to server.This process repeats. Even though this solution works, it is not scalable solution. It can put heavy load on the server depending on the polling interval and number of clients . Polling from JavaScript shown below
	function getInfo() {

	$.ajax("url", function ( newInfo){
		if ( newInfo != null) {
			// do something with the data
		}
	});
	// poll again after 20 seconds
	setTimeout(getInfo,20000);
}
// start the polling loop
getInfo();
    • Long polling: It is similar to polling , where Web browser makes request to server , but server does not respond to client immediately , it will leave the request hanging open till it has some data to deliver. Eventually server responds and client uses the data and starts new request, whole process repeats. This technique is improvement over polling as it does creates unnecessary connections.  Long polling from JavaScript shown below
function getNewInfo(){
  $.ajax("url", function (newinfo) {
      // do something with the data
	  // start the new request
      getNewINfo();
  });
}
// start the polling loop
getNewInfo();
  • EventSource: It is similar to long polling client make request ,server leaves the request hanging open till it has some data to deliver, difference is after sending data back server does not close the connection. Server will keep the connection open .  This solution provide good scalability in handling requests , also it is implemented in natively by Web Browser it can perform better compared to previous techniques.But EventSource is  not iimplemented IE.  Event source from JavaScript shown below
var eventSrc = new EventSource("url");
	// register event handler for the message
    eventSrc.addEventListener( "message",function (evt) {
        //process the data
    });
  • WebSocket:Is the new approach to Web browser/server communication. It is bidirectional communication channel compared to unidirectional Event Source.  At high-level client sends HTTP Connection with “Upgrade: websockets” header;server leaves connection open , client and server negotiate which version of web socket protocol to use. Client and Server exchange messages once connection is upgraded to web socket. This technique is natively implemented by the Web Browser/Server . Refer to caniuse website to check the latest websocket implementation in Web Browser. WebSocket usage from JavaScript shown below
var socket = new WebSocket("url");
socket.onmessage = function (msg) {
	var newInfo = msg.data;
	// do something with the data
}
// client can also send request to server
socket.send(.... )

As we seen ,each technique has some pros and cons and not all browser have implemented all the features.
What we need is some abstraction over the different implementation  so that developer need not worry about various implementations.

SignalR to rescue.

SignalR is an abstraction on top of a persistent HTTP connection. It provides on consistent API over different implementations(long polling,websocket).

SignalR can use websocket if it is implemented by the Web browser and server and fallback to long polling if it is not implemented.
SignalR uses C# dynamic feature on the server side ( asp.net ) and Javascript dynamic features on the client side to create server and client proxy.

It is similar to Nodejs+socket.io implementation based on JavaScript.

CPU usage monitor sample

To illustrate the SignalR usage I wrote sample web application which allows to monitor % cpu usage on the server in real-time. Sample  application uses SignalR on server/client and Google web chart  for visualization.

Tools used: Free Microsoft Visual Web Developer 2010 Express, SignalR on Nuget and Google Chart Tools.

Here is the step by step instruction.

1. Create “ASP.NET Empty Web Application” in VS Web Developer express.

2. Use Nuget Package manager console in VS to install SignalR and associated dependency from Nuget.

3.Create class to hold the cpu usage information.

public class CpuUsageInfo
    {
        public double CPUUsage { get; set; }
        public String DateString { get; set; }
    }

4. Create new class on the server side which derives from SignalR PersistentConnection class.

 public class Perfmon : PersistentConnection
    {
        protected override System.Threading.Tasks.Task OnConnectedAsync(SignalR.Hosting.IRequest request, IEnumerable groups, string connectionId)
        {
            return base.OnConnectedAsync(request, groups, connectionId);
        }
    }

5.Update the Global.asax file to map ASP.NET route

protected void Application_Start(object sender, EventArgs e)
        {

            // add routing
            RouteTable.Routes.MapConnection("perfmon", "perfmon/{*operations}");

            Task.Factory.StartNew(() =>
                {
                    // get connection to global client connections
                    IConnectionManager connectonManager = AspNetHost.DependencyResolver.Resolve();
                    var connection = connectonManager.GetConnection();

                    var counter = new PerformanceCounter();
                    counter.CategoryName = "Processor";
                    counter.CounterName = "% Processor Time";
                    counter.InstanceName = "_Total";

                    while (true)
                    {
                        var item = new CpuUsageInfo()
                        {
                            DateString = DateTime.Now.ToString("hh:mm:ss"),
                            CPUUsage = Math.Round(counter.NextValue(), 2),
                        };

                        connection.Broadcast(item);

                        Thread.Sleep(500);
                    }
            });

        }

6. Inside Application_Start method add code to get send CPU usage events to connected clients using SignalR library.

7. Create new html file with that plots line chart using Google charting tools. In the JavaScript make connection to server using SignalR js library. As the data arrives from the server update the chart data collection and redraw the chart.

    var output;
    $(document).ready(function () {

        output = $("#output");

    });

    var data;
    var chart;
    var options;
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1.0', { 'packages': ['corechart'] });
    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);
    // Callback that creates and populates a data table,
    // instantiates the pie chart, passes in the data and
    // draws it.
    function drawChart() {

        // Create the data table.
        data = new google.visualization.DataTable();
        data.addColumn('string', 'Time');
        data.addColumn('number', 'CPU Usage in %');

        // Set chart options
        options = { 'title': 'CPU usage history', 'width': 600, 'height': 400 };

        // Instantiate and draw our chart, passing in some options.
        chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }

    connection = $.connection('/perfmon');

    connection.received(function (dataItem) {
        var newItem = dataItem;

        output.text("Received text from the server .: " + newItem.DateString + "," + newItem.CPUUsage);

        // update chart with data received from the server
        data.addRow([newItem.DateString, newItem.CPUUsage]);
        if (data.getNumberOfRows() > 100) {
            data.removeRow(0);
        }
        chart.draw(data, options);
    });

    connection.start();

Here is the demo output in Browser

Complete sample code
Resources
1. SignalR Channel9 Video
2. SignalR on GitHub
3. Socket.IO

Newest C++ compiler

Yesterday, I was watching “Going Native 2012” conference session. This conference is show what is coming in C++ 11. Speaker include Bjarne Stroustrup, Herb Sutter, Andrei Alexandrescu,

Very good discussion of C++11.

One of the session speaker is guy from Google , talked about new C++ compiler ‘Clang” .It seems this compiler helps to catch programming errors better than GCC.

He was explaining how new compiler can provide static analysis as part of compilation. Very good talk. Lots of examples showing capability of clang compiler to catch programming errors.

Even if we do not want to use clang compiler for our product compilation , we can still try the compiler on code base and see what kind of coding errors it reports.

http://clang.llvm.org/comparison.html

CLANG presentation

http://ecn.channel9.msdn.com/events/GoingNative12/GN12Clang.pdf

Session video

http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Clang-Defending-C-from-Murphy-s-Million-Monkeys

GPGPU programming using c++

In recent AMD Fusion developer summit, Herb Sutter (Principal Architect of Visual C++ and former c++ committee chair) announced C++ AMP (Accelerated Massive Parallelism) for developing C++ application which can take advantage of GPU present in our computers.

Currently if you want to program against GPU , you have use either HLSL ,Open CL, or CUDA which are ‘C’ language
extension requires special step to compile source against these languages. This model allows to use c++ OOP capability to implement programs which can take advantage of GPU’s

C++ AMP uses existing C++ 0x language features (lambda expression) and existing c++ compilers to produce code which can run on any GPU which supports Direct X 11. Also specification of C++ AMP is open to compiler vendors. In future we can look forward for other compiler to implement this on top of existing open gl interface on Linux and other platforms.

More information about c++ AMP.

Herb Sutter keynote

VC++ Team Blog

.Net Reflector alternatives

If you are in the .net programming world , you might have used free tool .Net Reflector, tool which allows you browse analyze and decompile .net assembly. It can be used to inspect, navigate, search, analyze, and browse the contents of a
.NET component such as an assembly and translates the binary information (IL) to a human-readable form.

Unfortunately this tool no longer available as free tool . Red gate software which maker this tool announced that  they will  be charging license fee to use this software.You can use the existing version without license fee. If you want to upgrade you have to buy.

If you are .Net Reflector user , still wants free tool to analyze assembly , here are two alternatives. Both products listed below are still beta