Skip to main content

Posts

Showing posts from 2015

What is Apache Maven?

Apache Maven is a cool java project management tool that I recently came across at work. I was asked to set-up a test suite for our low latency Market Data System project using an existing test framework built using TestNG. I was handed over something called a pom.xml file and that was it! Using IntelliJ IDE you can import this pom.xml as a Maven project and everything is basically set-up for you. The folder structure is pretty standard in Maven so that every developer get the same same project structure. I think the main advantage of maven is that it is a great way of managing dependencies. Imagine that something changes in the the so called test framework. The framework developers update the repository and when I re-export the pom file updates the correct jar file will already be in my project. By changing the SNAPSHOT version of the pom.xml file I can switch through different versions of the test framework and other dependencies. Maven will first look for dependencies in the inte

State Pattern Anyone?

Here's two exciting link I found about the state pattern. Two very good reads. http://gameprogrammingpatterns.com/state.html http://angry-architect.blogspot.com/2006/08/problems-with-state-pattern.html

Visitor Pattern

It is often require to separate an algorithms from the data it operates on. For example say that we have some inter process communication messages that needs to be sent out from a TCP channel. The message is the parent class and different messages are derived from this class. The sub-message class object that has some member variables that are used to pack the real messages to be sent out. The straightforward way of doing this is to have a virtual function pack() defined in the message class itself and implement it in every sub-message class and call it whenever we need the message to be generated. However this is not that efficient because if we need to change the structure or the format of the messages that we need to send out, the pack () of the that specific class should be modified. If all messages require a addition of a different field all messages have to be changed. Not if you have introduced the visitor pattern. Compare the visitor pattern with the standard approach.

Linux file permissions - what's with the numbers?

Everybody uses commands such as chomd +x "some file" , chomd u+x "some file" (for the user only) or chmod 775 "some file"   The available options are read (r, or number 6), write (w or number 2) and Execute (x or number1). Each of these can be applied to user, group or others making sense of 777 type permission commands. A good explanation can be found here . With the basic understanding of the Classes and Permissions, let’s delve into it further and see how the “777” or “775” come about. Every file and folder contain a 8-bit data that control the permissions. At its basi c binary form, it will be “000”, which means no permissions of any form is granted. When you set a “Read” permission, it will add 4-bit to the data, making it “100” (in binary format) or a “4” in the usual decimal format. Setting a “Write” permission will add 2-bit to the data, making it “010” and “2” in decimal form. Lastly, setting an “Execute” permission adds 1-bit

How would you implement a StringToInt from scratch?

sYou are given a integer in string format; String sNum = "25"; int iNum = StrToInt(sNum); How does StrToInt() work? Crudely it's as follows. You go digit by digit. Each digit has a hex value. 0   0x30   48 1   0x31   49 2   0x32   50 3   0x33   51 4   0x34   52 .... 9   0x39   57 The algorithm pseduo-code is as follows. 1. result = 0; 2. for each character i in sNum 3.   result = result*10; 4.   result = result + ASCIIVal(sNum[i]) - ASCIIVal('0'); Let's take 25 for example. For the first iteration; sNum[0] = 2; result = 0*10=0; result = 0 + (50-48) = 2; For the second iteration; sNum[1] = 5; result=2*10=20; result=20+(53-48)=25

memcpy and it's usage with structs

A good question on stack overflow can be found here It discusses about the size of the array being different to the sum of the size of each member. e.g. struct Item{     int i; // 4     char buf1[10]; // 10     char buf2[20]; // 20 }; sizeof(test) is actually 36!!! This is because of padding the members... Memory is usually organized into 8 byte chunks, so sometime the compiler will add 4 additional padding bytes for the int... Anyways memcpy is on the spotlight and it's a generic function that can be used to copy the values of num bytes from the location pointed by source directly to the memory block pointed by destination ., void * memcpy ( void * destination, const void * source, size_t num );   You can use it to copy char buffers, arrays, structs or any other memory. This is a fast operation and should be used in performance oriented programs instead of assignment. The following example illustrates it's usage pointing out that you can't really use p

Initialize a char array/C-String

char *Type = new char[10]; Say you have a char array in heap. You want to fill this array with something; say "Access". If you do; Type = "Access" This would be a memory leak!!!! What happens? You'll get a warning/error; error : deprecated conversion from string constant to 'char*'   but this is not regarding the memory leak. What actually happens is that the statically allocated string literal will be assigned to the pointer Type, i.e. Type will now point to a different memory and you have no handle to the new'ed memory you actually allocated by new char[10] !!! Ouch! The proper way to do this is; strcpy( Type, "Access" ); But the actual error/warning is because the actual type created by saying Type="Access" is a const char*. The string literal "Access" is usually not inteded to be modifed by something like Type[0] = "a". Fix would be to do; const char *Type = "Access";

sprintf, snprintf, strcpy, strncpy and sizeof operator

"C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." "snprintf is safer than sprintf" What do these statements really mean? int sprintf ( char * str , const char * format , ... )  int snprintf ( char * s, size_t n, const char * format, ... );  char * strcpy ( char * destination, const char * source ); char * strncpy ( char * destination, const char * source, size_t num );   The usage is something like; char* msg1 = new char[10]; strcpy(msg1, "test"); // 1 char buffer[128]; sprintf(buffer, "%s", msg); //2 strcpy : Copies bytes until it finds a 0-byte in the source code. The string literal "test" has 4 characters and a terminating null character at end, therefore needs 5 characters at least on msg1.  Is this dangerous? Yes, because if the source message is not null terminated it will read until a null character

Casting an int to void pointer in c++

I was a bit confused with the following piece of code which just creates a number of threads and assigns them an id. #include "pthread.h" #include "stdio.h" #include "stdlib.h" #define N 5 void *worker_thread(void *arg) {     printf("This is worker_thread #%ld\n", (long) arg);     pthread_exit(NULL); } int main() {     pthread_t my_thread[N];     long id;     for (id = 1; id <= N; id++) {         int ret = pthread_create(&my_thread[id], NULL, &worker_thread, (void*) id);         if (ret != 0) {             printf("Error: pthread_create() failed\n");             exit(EXIT_FAILURE);         }     }     pthread_exit(NULL); } The function prototype was; int pthread_create(pthread_t * thread , const pthread_attr_t * attr , void *(* start_routine ) (void *), void * arg );   The arg was void*, Shouldn't we have; int ret = pthread_create(&my_thread[id], NULL, &wor

Disk Usage for Directories in Linux

The Linux command du -h --block-size=MiB --max-depth=3 | sort -rn shows the disk usage for each directory upto a depth of 3. MiB is required to avoid showing all those long bytes numbers. -h is for human readable format. sort -rn sorts on the reverse numeric order, i.e. the most disk usage first

Signal Handling - Linux

A program running always face unexpected situations, running out of memory, lost network connectivity, losing permissions, read write errors etc. Therefore a robust program should be resilient to these conditions and should gracefully exit if the program cannot continue. Signals should be handled.... I found this to be extremely resourceful and I'll just give a summary of what the author Alex says..... Signal is a notification, a message sent by either operating system or some application to your program (or one of its threads). In addition to informative nature of signals, they also interrupt your program. I.e to handle a signal, one of the threads in your program, stops its execution and temporarily switches to signal handler. Signals, as their name implies, used to signal something. There are several types of signals, each indicating something of its own. For instance SIGINT that I already mentioned, tells your program that someone tries to interrupt it with CTRL-C.

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript.

What is GWT ? Google Web Toolkit

The GWT project states that, GWT is a development toolkit for building and optimizing complex browser-based applications. Its goal is to enable productive development of high-performance web applications without the developer having to be an expert in browser quirks, XMLHttpRequest, and JavaScript. GWT is used by many products at Google, including AdWords, AdSense, Flights, Hotel Finder, Offers, Wallet, Blogger. It’s open source, completely free, and used by thousands of developers around the world. I quote the development lifecycle of GWT App from the project site as well. Write : The GWT SDK provides a set of core Java APIs and Widgets. These allow you to write AJAX applications in Java and then compile the source to highly optimized JavaScript that runs across all browsers, including mobile browsers for Android and the iPhone. Constructing AJAX applications in this manner is more productive thanks to a higher level of abstraction on top of common concepts like DOM

Dynamic Web Content - From HTML to Servlets and beyond (Part II)

Towards Servlets  From the earlier post I've discussed about the techniques of dynamic web content loading. This post is about servlets, java servlet pages (jsps) and java. Java Servlets are an efficient and powerful solution for creating dynamic content for the Web. Over the past few years Servlets have become the fundamental building block of mainstream server-side Java. The power behind Servlets comes from the use of Java as a platform and from interaction with a Servlet container. The Java platform provides a Servlet developer with a robust API, object-orientated programming, platform neutrality, strict types, garbage collection, and all the security features of the JVM. Complimenting this, a Servlet container provides life cycle management, a single process to share and manage application-wide resources, and interaction with a Web server. Together this functionality makes Servlets a desirable technology for server-side Java developers.  Serv

Dynamic Web Content - From HTML to Servlets and beyond (Part I)

Web development involves a typical client server model. Client Server communications is built on the OSI stack and different communication protocols exist at each layer. Internet is built upon TCP/IP as the transport and internet layer and HTTP/HTTPS as the application layer. A client communicating with a web server via a web browser needs to establish a TCP connection on the listening port of the serer. After establishing a TCP connection, the client communicates over HTTP. HTTP requests in the form of GET/POST are sent to the server and the server responds with metadata and HTML content. This HTML content is interpreted by the web browser to render web pages on client's browser. Web development is a vast field with thousands of libraries, frameworks, protocols and technologies. It's impossible to study all of them but the important thing to realize is that there are many ways to accomplish a similar outcome. All these techniques can be grouped into 2 aspects, Cl

RAII and Smart Pointers - Resource Management in C++

RAII stands for Resource Acquisition Is Initialization. What is it and why it is needed? We used different resource in out software such as file streams, network pipes, database connections and even locks are considered as resources. The standardized procedure of using a resource is to acquire the resource, use it and then release it. Holding onto resources is bad practice and starves other processes for resources slowing down the entire system in multicore systems. OK, what's the big deal? So we want to open a file, write some stuff to it and then close it. We just do. file.open(); file.write("test"); file.close(); Caution here. If it's a trivial task where you don't have anything in between opening and closing the file (or any other resource) this seems fine but this is often not the case. You have many other things that you do between you opening the file and closing the file. Chances are these operations might fail, or worse throw. In such case

C++ Callbacks using function pointers vs boost bind +boost function

In C, the most common uses of callbacks are as parameters to library functions like qsort , and as callbacks for Windows functions, etc. For example you might have a library that provides some sorting functions but you want to allow the library user to provide his own sorting function. Since the arguments and the return values do not change depending on the sorting algorithm, this can be facilitated in a convenient manner using function callbacks. Callbacks are also used as event listeners. onMouseClick(), onTerminalInput(), onData(), onConnectionStatus(), onRead() are probably some examples you've already seen in different libraries. The libraries have these callback functions and the users of the library are supposed to implement them. The library has a function pointer to these functions and calls them on their event loop which will invoke the code of the inherited classes of the library user. The implementation of function pointers is simple: they are just "code p

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut