Sunday, 7 July 2013

ns 2 Packet

Packet



A NS packet is composed of a stack of headers, and an optional data space (see Figure 12). As briefly mentioned in the "Simple Simulation Example" section, a packet header format is initialized when a Simulator object is created, where a stack of all registered (or possibly useable) headers, such as the common header that is commonly used by any objects as needed, IP header, TCP header, RTP header (UDP uses RTP header) and trace header, is defined, and the offset of each header in the stack is recorded. What this means is that whether or not a specific header is used, a stack composed of all registered headers is created when a packet is allocated by an agent, and a network object can access any header in the stack of a packet it processes using the corresponding offset value.


Figure 12. NS Packet Format
Usually, a packet only has the header stack (and a data space pointer that is null). Although a packet can carry actual data (from an application) by allocating a data space, very few application and agent implementations support this. This is because it is meaningless to carry data around in a non-real-time simulation. However, if you want to implement an application that talks to another application cross the network, you might want to use this feature with a little modification in the underlying agent implementation. Another possible approach would be creating a new header for the application and modifying the underlying agent to write data received from the application to the new header. The second approach is shown as an example in a later section called "Add New Application and Agent".

ns-2

ns-2 tutorial

Introduction

NS (version 2) is an object-oriented, discrete event driven network simulator developed at UC Berkely written in C++ and OTcl. NS is primarily useful for simulating local and wide area networks. Although NS is fairly easy to use once you get to know the simulator, it is quite difficult for a first time user, because there are few user-friendly manuals. Even though there is a lot of documentation written by the developers which has in depth explanation of the simulator, it is written with the depth of a skilled NS user. The purpose of this project is to give a new user some basic idea of how the simultor works, how to setup simulation networks, where to look for further information about network components in simulator codes, how to create new network components, etc., mainly by giving simple examples and brief explanations based on our experiences. Although all the usage of the simulator or possible network simulation setups may not be covered in this project, the project should help a new user to get started quickly
This document gathers the authors’ own experience in implementing MANET routing protocols for the ns-2 Network Simulator. The text is licensed under the GNU Free Documentation License (FDL). It was written back in 2004, you can update it and share the result.

Download

You can either download the tutorial or view it online.

Authors

Francisco J. Ros and Pedro M. Ruiz.

MAC Address

 MAC Address using Java

Free Source Code
Free Source Code
Objective :
* How to get the MAC Address of a computer?
* How to get the MAC Address of a computer using Java?
* How to get the MAC Address of a computer via programming?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.net.*;
 
class GetMac
{
public static void main(String arg[])
{
try
{
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Note : This code will return only the first net address; if you need other detils too, get a list.
Posted from WordPress for Android

Smallest Projector

 Smallest Projector Up to date...

iGo, introduces the UP-2020 palm size pocket projector based on the Digital light processing System.


 The Projector features 854x480 native resolution and able to project viewable screen up to 70 inches. 
It also supports built in media playback function like MP4,JPG and BMP files and has micro-SD card slot, USB port and built in speaker.

Linux

Future and Past of Linux Then and Now

Linux one of the most growing operating system in today most of the people using this because you can gain lot of advantages over windows. So here I found some good picture about Linux then and now statistics.

Java Reflection

  Private methods and Fields Access with Java Reflection

Java Reflection, guess you heard it.But I will give a little bit of introduction and show some code sample to get familiar with this.
OK so the first thing first.
What is Java Reflection ?
Java Reflection can be used to dynamically find java classes , locating and execute methods, access fields at run time.


You can find More on : http://docs.oracle.com/javase/tutorial/reflect/index.html

Here is the sample project that I did.

First I create a class called dynamic test and add one private field and four methods including one private methods. Here is the code and its straight forward.


 package rd.test;  
 public class DynamicTest {  
   private int count;  
   public void printStar() {  
     System.out.println("*");  
   }  
   public void printStarWithInt(int i) {  
     System.out.println("* Integer : " + i);  
   }  
   public void printStarWithString(String param) {  
     System.out.println("* String : " + param);  
   }  
   public void printStarWithStrings(String param1, String param2) {  
     System.out.println("* String : " + param1 + " , and " + param2);  
   }  
   private void printStarInPrivate() {  
     System.out.println("* Private : *");  
   }  
 }  

and here is the class with main method.


 package rd;  
 import rd.test.DynamicTest;  
 import java.lang.reflect.Field;  
 import java.lang.reflect.InvocationTargetException;  
 import java.lang.reflect.Method;  
 public class Main {  
   public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException,  
       InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {  
     Class cls = Class.forName("rd.test.DynamicTest");  
     Object obj = cls.newInstance();  
     /**  
      * calling printstart method without passing any parameters  
      * */  
     Method method = cls.getDeclaredMethod("printStar", null);  
     method.invoke(obj, null);  
     /**  
      * calling printStarWithInt method with int parameter  
      */  
     Class[] intParam = new Class[1];  
     intParam[0] = Integer.TYPE;  
     method = cls.getDeclaredMethod("printStarWithInt", intParam);  
     method.invoke(obj, 100);  
     /**  
      * calling printStarWithStrings methos with two string parameters  
      */  
     Class[] stringParams = new Class[2];  
     stringParams[0]=String.class;  
     stringParams[1]=String.class;  
     method = cls.getDeclaredMethod("printStarWithStrings", stringParams);  
     method.invoke(obj, "hi","rajith");  
     /**  
      * calling private method in DynamicTest class  
      */  
     method = cls.getDeclaredMethod("printStarInPrivate", null);  
     method.setAccessible(true);  
     method.invoke(obj, null);  
     /**  
      * setting and getting private fileds  
      */  
     DynamicTest dynamicTest = new DynamicTest();  
     Field field = cls.getDeclaredField("count");  
     field.setAccessible(true);  
     field.setInt(dynamicTest,10);  
     System.out.println(field.get(dynamicTest));  
   }  
 }  

Operating Systems Programming

Operating Systems Programming via Java Producer Consumer Problem with Blocking Queue

Producer Consumer problem is a popular problem domain in SE industry.
Before that I will give a quick introduction of Blocking Queue.

Blocking Queue is an interface locate in java concurrent package.It mainly support operations that wait for the queue to become non empty when retrieving and removing element and wait for space become available when adding an element. All the blocking queue implementation are thread-safe and methods are atomic.

In this demo I will use ArrayBlockingQueue as the implementation of BlockQueue.

So first we create domain model for this demo.

Message.java

 package rd.domain;  
 public class Message {  
   private String description;  
   public String getDescription() {  
     return description;  
   }  
   public void setDescription(String description) {  
     this.description = description;  
   }  
 }  

then we create Producer.java
create 100 message and finish message and add in to the queue.


 package rd.concurent;  
 import rd.domain.Message;  
 import java.util.concurrent.BlockingQueue;  
 public class Producer implements Runnable {  
   private BlockingQueue<Message> queue;  
   public Producer(BlockingQueue<Message> queue) {  
     this.queue = queue;  
   }  
   @Override  
   public void run() {  
     // create messages and adding to queue  
     for (int i = 1; i <= 100; i++) {  
       Message message = new Message();  
       message.setDescription(" Message " + i);  
       try {  
         Thread.sleep(i);  
         queue.put(message);  
         System.out.println("Produced " + message.getDescription());  
       } catch (InterruptedException e) {  
         e.printStackTrace();  
       }  
     }  
     // adding exit message  
     Message message = new Message();  
     message.setDescription("finish");  
     try {  
       queue.put(message);  
     } catch (InterruptedException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Consumer.java

 package rd.concurent;  
 import rd.domain.Message;  
 import java.util.concurrent.BlockingQueue;  
 public class Consumer implements Runnable {  
   private BlockingQueue<Message> queue;  
   public Consumer(BlockingQueue<Message> queue) {  
     this.queue = queue;  
   }  
   @Override  
   public void run() {  
     Message message = null;  
     try {  
       while (!(message = queue.take()).getDescription().endsWith("finish")){  
         Thread.sleep(10);  
         System.out.println("Consumed " + message.getDescription());  
       }  
     } catch (InterruptedException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

So the basic implementation are done. Now its time to test it. Here I create Main.java and create producer consumer thread and start those threads.

Main.java

 package rd;  
 import rd.concurent.Consumer;  
 import rd.concurent.Producer;  
 import rd.domain.Message;  
 import java.util.concurrent.ArrayBlockingQueue;  
 import java.util.concurrent.BlockingQueue;  
 public class Main {  
   public static void main(String[] args) {  
     BlockingQueue<Message> blockingQueue = new ArrayBlockingQueue<>(10);  
     Producer producer = new Producer(blockingQueue);  
     Consumer consumer = new Consumer(blockingQueue);  
     new Thread(consumer).start();  
     new Thread(producer).start();  
     System.out.println("Started.................");  
   }  
 }  

Hence we had  solved the producer consumer problem.

Does Clean Code matters ?

Does Clean Code matters ?

YES,
And the other question is WHAT FOR ? That's what we talk about here..

OK lets go for it..
What is Clean code ? Actually we don't have the exact definition for this. But there are some definitions that create by geniuses. Below are some of these...


Bjarne Stroustrup, Inventor C++ and author of The C++ Programming Language
 I like my code to be elegant and efficient. The logic should be straightforward to make it hard  
 for bugs to hide, the dependencies minimal to ease maintenance, error handling complete  
 according to an articulated strategy, and performance close to optimal so as not to tempt  
 people to make the code messy with unprincipled optimizations. Clean code does one thing  
 well.  

Dave Thomas, founder of OTI, godfather of the Eclipse strategy,
 Clean code can be read, and enhanced by a developer other than its original author. It has  
 unit and acceptance tests. It has meaningful names. It provides one way rather than many  
 ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since depending on the language, not all necessary information can be expressed clearly in code alone.  

Michael Feathers, author of working Effectively with Legacy Code,
 I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads to all of them. Clean code always looks like it was written by someone who cares. There is nothing obvious that you can do to make it better. All of those things were thought  
 about by the code’s author, and if you try to imagine improvements, you’re led back to  
 where you are, sitting in appreciation of the code someone left for you—code left by someone  
 who cares deeply about the craft.  

  WHY WE NEED CLEAN CODE ?
If your a programmer with more than one year you know what I mean..

First take a look at bad code or a mess code.
 

Suppose you get a new requirement and you need to add the new functionality to your existing code which is really bad and messy. Before adding any new thing to your code first you need to understand the exiting code. That means you need to READABILITY your existing code. Since its a bad code you will go through really pain full time to understand what the code says. For example imaging a code with no comments, no meaning full variables, no meaning full function definitions with hell of big line of codes that does too many things within single code block , this will drive you crazy.
So this will take more than time than your expecting(Or your PM) to read and understand only others code. Every programmer knows about the damn dead lines and our heads will bump up.
If it is a clean code it must full fill the READABILITY.

That's one of the basic thing that each and every code must have. But there are more. Formatting, Unit testing, avoid writing bad comments, Error handling  are some of. So will talk about all of these things in near future.

Android Applications[Windows]

To Develop Android Applications[Windows] Setup required

In this tutorial I will show you the required steps how to setup your computer to develop Android applications in windows.

Android is a software platform and operating system for mobile devices. It based on linux kernal and it written on Java language but not use JVM to execute the applications.

Here are the requirements to setup Android in your machine.

  • Any Operating system
  • Android SDK
  • Java Development Kit
  • Eclipse IDE (Optional, but has lot of advantages)
Here are the steps that you want to follow.

STEP 1 
First you have to download the appropriate Android SDK from http://developer.android.com/sdk/index.html here.

Its free and anyone can download it. After downloading unzip it and install it. 
Now you will get the Android SDK Manager.\
To get the full benefit go to Available packages and install the Third party add-ons . It includes the Google APIs.
 
STEP 2
In order to run your apps you want to set the Android Path in Environment variable.
For an Example:
suppose we have Java path and Android path. So PATH variable look like this 
PATH = C:\Program Files\Java\jdk1.6.0_19\bin;C:\Program Files\Android\android-sdk-windows\tools

STEP 3

Now Every thing is set. But to develop applications you need Eclipse IDE. You can use any IDEs that support Java. But using eclipse you can get the lots of benefits. 

Download Eclipse in here ---> http://www.eclipse.org/downloads/

Goto Help --> Install New Software 
Click Add 

Enter 'ADT Plugin' for the Name and enter 'https://dl-ssl.google.com/android/eclipse/' for the URL and Select OK.

Click Next in the windows and click Finish. After that restart the Eclipse to apply the changes.
 
To configure Eclipse follow these steps.
1.Windows --> Preferences
2.Select Android
3.Give the SDK location and click Apply.
 
Hence you all can enjoy Android Programming.