Saturday, April 28, 2012

How to increase the hard disk size of the virtual machine

Open a terminal and navigate to the folder with the VirtualBox disk image, then use the following command:
VBoxManage modifyhd YOUR_HARD_DISK.vdi --resize SIZE_IN_MB 
replacing YOUR_HARD_DISK and SIZE_IN_MB with your image name and desired size.

My guest OS is Ubuntu. To manage it's partitions I used GParted Live CD.

Wednesday, April 25, 2012

Arduino

Today my college told me about Arduino. I think it would be interesting to try it when I have some free time. Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. To read more go here.

Thursday, April 19, 2012

The linux sockets

I spend a few evenings reading about linux sockets. I created client and server's applications which use sockets with TCP/IP protocol. Client sends a request to the server and gets a response. I also found an interesting article about linux sockets and networking programming. The article can be found here.

Here is a simple diagram which shows how sockets work:


Here is a header file Channel.h of the channel class which encapsulates socket:
#ifndef LAB_CHANNEL
#define LAB_CHANNEL

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

namespace lab {
 class Channel {
   int socket_id;
   sockaddr_in socket_address;
   void CreateSocket(const char* ip, int port);
   void Write(int connection_id, const char* message);
   void Read(int connection_id);
  public:
   Channel(const char* ip, int port);
   ~Channel();
   void Listen();
   void Send(const char *message);
   void Close();
 };
}

#endif
Here is a source file Channel.cpp of the Channel class:
#include "Channel.h"
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

lab::Channel::Channel(const char* ip, int port) {
 this->CreateSocket(ip, port); 
}

lab::Channel::~Channel() {
 this->Close();
}

void lab::Channel::CreateSocket(const char* ip, int port) {
 // socket() creates an endpoint for communication and returns a descriptor.
 // AF_INET - IPv4 Internet protocols.
 // SOCK_STREAM - Provides sequenced, reliable, two-way, connection-based byte 
 // streams.
 socket_id = socket(AF_INET, SOCK_STREAM, 0);
 if (socket_id == -1) {
  std::cout << "Failed to create socket." << std::endl;
 } else {
  std::cout << "Socket was created successfully." << std::endl;
 }

 socket_address.sin_family = AF_INET;
 socket_address.sin_port = htons(port);
 socket_address.sin_addr.s_addr = inet_addr(ip);
 memset(&(socket_address.sin_zero), 0, 8);
 // bind - bind a name to a socket.
 if (bind(socket_id, (struct sockaddr *)&socket_address, sizeof(struct sockaddr)) != -1) {
  std::cout << "Name was binded successfully to the socket." << std::endl;
 } else {
  std::cout << "Failed to bind a name to the socket." << std::endl;
 } 
}

void lab::Channel::Write(int connection_id, const char* message) {
 if (write(connection_id, message, strlen(message)) != -1) {
  std::cout << "Message was send: " << message << std::endl;
 } else {
  std::cout << "Failed to send a message." << std::endl;
 }
}

void lab::Channel::Read(int connection_id) {
 char buffer[32];
 memset(buffer, 0, 32);
 if (read(connection_id, buffer, 32) != -1) {
  std::cout << "Message was read: " << buffer << std::endl;
 } else {
  std::cout << "Failed to read a message." << std::endl;
 }
}

void lab::Channel::Listen() {
 // listen - listen for connections on a socket.
 // The backlog argument defines the maximum length to which the queue of 
 // pending connections for sockfd may grow.
 if (listen(socket_id, 4) != -1) {
  std::cout << "Listening the socket." << std::endl;
 } else {
  std::cout << "Failed to listen the socket." << std::endl;
 }

 // accept - accept a connection on a socket.
 sockaddr_in remote_address;
 int connection_id;
 int remote_address_size = sizeof(remote_address);
 connection_id = accept(socket_id, (sockaddr*)&remote_address, (socklen_t*)&remote_address_size);
 if (connection_id != -1) {
  std::cout << "Connection was accepted." << std::endl;
 } else {
  std::cout << "Failed to accept connection." << std::endl;
 }

 this->Read(connection_id);
 this->Write(connection_id, "Reply");

 close(connection_id);
}

void lab::Channel::Send(const char *message) {
 // connect - initiate a connection on a socket.
 if (connect(socket_id, (sockaddr*)&socket_address, sizeof(sockaddr)) != -1) {
  std::cout << "Connection was estableshed successfully." << std::endl;
 } else {
  std::cout << "Failed to connect." << std::endl;
 }
 this->Write(socket_id, message);
 this->Read(socket_id);
}

void lab::Channel::Close() {
 close(socket_id);
}
Here is a file Client.cpp of the client application:
#include <iostream>
#include "Channel.h"

int main() {
 std::cout << "Client started..." << std::endl;

 lab::Channel *channel = new lab::Channel("127.0.0.1", 2590);
 channel->Send("Request");
 delete channel;

 return 0;
}
Here is a file Server.cpp of the client application:
#include <iostream>
#include "Channel.h"

int main() {
 std::cout << "Service started..." << std::endl;

 lab::Channel *channel = new lab::Channel("127.0.0.1", 2590);
 channel->Listen();
 delete channel;

 return 0;
}
Also I added makefile which allows to compile source code:
CPP = g++
CFLAGS = -Wall -lrt
CHANNEL_FILES = src/Channel.cpp
SERVICE_FILES = src/Service.cpp
CLIENT_FILES = src/Client.cpp

all: service client

service: channel $(SERVICE_FILES)
 $(CPP) $(CFLAGS) -o bin/service obj/channel.o $(SERVICE_FILES)

client: channel $(CLIENT_FILES)
 $(CPP) $(CFLAGS) -o bin/client obj/channel.o $(CLIENT_FILES)

channel: $(CHANNEL_FILES)
 $(CPP) $(CFlAGS) -o obj/channel.o -c $(CHANNEL_FILES)

clean:
 rm -f ./obj/*.o
 rm -f ./bin/service
 rm -f ./bin/client

run-service:
 ./bin/service

run-client:
 ./bin/client

Wednesday, April 18, 2012

Forking vs Threading

I found interesting article about forking and threading in Linux. In this article you can discover advantages and disadvantages of forking and threading. This article you can find here.

Sunday, April 15, 2012

Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation

When .NET 3.0 was released I decided to learn WPF and XAML. I read the book Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation by Charles Petzold. Later this book was useful when I was learning Silverlight wich is similar to WPF.

In this book I discovered:
  • Create and enhance controls including menus, toolbars, tree views, and list views
  • Use dynamic layout to automate the positioning of controls and graphics
  • Work with dependency properties and routed input events
  • Use XAML resources, styles, and templates to alter the appearance of your UI
  • Use data binding techniques in XAML to help simplify and streamline your applications
  • Develop visually-stunning UIs with interactive graphics, media, and animation

Tuesday, April 10, 2012

JavaScript Patterns

One of the last books I read is JavaScript Patterns written by Stoyan Stefanov. This book improved my knowledge about Javascript.

This book is dedicated to experienced developer which are looking how to solve problems related to objects, functions and inheritance.

  • Explore useful habits for writing high-quality JavaScript code, such as avoiding globals, using single var declarations, and more
  • Learn why literal notation patterns are simpler alternatives to constructor functions
  • Discover different ways to define a function in JavaScript
  • Create objects that go beyond the basic patterns of using object literals and constructor functions
  • Learn the options available for code reuse and inheritance in JavaScript
  • Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more
  • Examine patterns that apply specifically to the client-side browser environment

Tuesday, April 3, 2012

C++ first program

A few days ago I thought it would be good to renew my knowledge about C++. I decided to create simple application and compile it on Linux and Windows. Here is application source code:
#include <iostream>

int main() {
 std::cout << "My first program..." << std::endl;
 return 0;
}

Linux

I created makefile which allows to compile and run application. To read more about how to create makefile you can read here.

CPP = g++
CFLAGS = -Wall -lrt

all: program
 $(CPP) $(CFLAGS) -o bin/program obj/program.o

program:
 $(CPP) $(CFLAGS) -o obj/program.o -c src/program.cpp

clean:
 rm -f ./obj/*.o
 rm -f ./bin/program

run:
 ./bin/program
To compile application just type make:
make
If you want to run application, type such command:
make run

To compile application on Linux I used GNU C++ compiler (g++). The -Wall command line option enables all warnings. If you want to get more information about compiler read here.

Windows

I created .mak file which allows to compile and run application. To read more about how to create .mak file and use NMAKE utility you can read here.

CPP = cl
CFLAGS = /EHsc /W4

all: program
 $(CPP) $(CFLAGS) /Febin/program.exe  obj/program.obj

program:
 $(CPP) $(CFLAGS) /c src/program.cpp /Foobj/

clean:
 del bin\*.exe obj\*.obj

run: 
 bin\program.exe
To compile application on Windows I used Microsoft C/C++ compiler (cl). The /EHsc command line option instructs the compiler to enable C++ exception handling. The /c command line option instructs the compiler to compile without linking. The /W command line option sets warning level. If you want to get more information about compiler read here.
To compile application, type such command:
nmake -f build.mak
To run application, type such command:
nmake -f build.mak run