A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Saturday, November 6, 2010

GPS with JGPS - Setup Testing

3:12:00 PM Posted by Satish , , No comments
I am playing around the GPS module since quite some time and got a new JGPS java api to interact with. I have a serial out put GPS module. Check out my GPS setup. This particular API has got a very good interface for USB, Serial and bluetooth GPS devides.This has native API for both Unix and windows platform. In unix plat form this supports both linux and solaris. This API has got a very meaning full instruction to setup in unix environment. As I have my java comm API already setup to windows and I have the USB - Serial driver for windows, I tested in windows rather than ubuntu.

This api works on the top of java comm api. I was controlling my GSM/ GPRS modem through  the IO channels using java comm api. In the older versions of jGPS, the io interaction to the GPS module was through Java comm api and the jGPS was only the formatter. But this new release integrated the Java comm api with it again with some additional interface like bluetooth.

So general architecture is as follows.

GPS Module - Serial/ USB/ Bluetooth Interface - Java Comm API (JAVA+Native API[OS specific]) - jGPS API (JAVA)

Following is a simple program to register to one of the event and get notified for the lat/lng.

Source


package org.satish.practice;



import com.uf.gps.Descriptor;
import com.uf.gps.GPSListener; 
import com.uf.gps.GPSEvent; 
import com.uf.gps.Position;
import com.uf.gps.protocols.NMEA.NMEAHandler; 
import com.uf.gps.Connection; 
import com.uf.gps.ConnectException; 
import javax.comm.SerialPort;

public class myApp implements GPSListener { 
public myApp () { 

NMEAHandler nh = null; 
/* Initialize a connection handler on the RS232 interface */ 
try { 
nh = new NMEAHandler(1,"myApp","COM6",9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
} catch (ConnectException x) { 
// Something is going wrong

/* Enable reception of GPSEvents */ 
nh.registerListener( this ); 
}

public static void main(String[] args) throws ConnectException { 
myApp my = new myApp(); 
}

public void actionPerformed(GPSEvent ev) { 
Position p = null; 
/*
* public GPSEvent (Connection src, int transactiontype, int datatype, Object
* data)
*/ 
/* src: connection handler, maybe there's more than one defined */
/* transactiontype: is one of BEGIN_TRANS, END_TRANS, _TRANS */ 
/* datatype: one of the _CLASS constants, which classifies a data type */ 
/* data: the GPSObject */ 

if (ev.getTransType() == GPSEvent.POS_TRANS) { 
p = (Position) ev.getData(); 

/* Every high level class has lots of properties */
Descriptor wa = p.getProperty("LONGITUDE"); 
Descriptor wb = p.getProperty("LATITUDE"); 

/* wa.getValue of class Double */ 
String lon = wa == null ? "" : (String) wa.getValue().toString(); 

/* wb.getValue of class Double */ 
String lat = wb == null ? "" : (String) wb.getValue().toString(); System.out.println( "Latitude/Longitude = [" + lat + "][" + lon + "]" ); 
} else if (ev.getTransType() == GPSEvent.WPT_TRANS) { 
// .................................................. 
}
}
}


Output

Latitude/Longitude = [12.957963999999999][77.67056266666667]

Latitude/Longitude = [12.957963999999999][77.67056266666667]
Latitude/Longitude = [12.957967][77.67056366666667]
Latitude/Longitude = [12.957967][77.67056366666667]
Latitude/Longitude = [12.957972][77.67056166666667]
Latitude/Longitude = [12.957972][77.67056166666667]
Latitude/Longitude = [12.95797][77.67056366666667]
Latitude/Longitude = [12.95797][77.67056366666667]
Latitude/Longitude = [12.957965][77.67056366666667]
Latitude/Longitude = [12.957965][77.67056366666667]
Latitude/Longitude = [12.957963][77.67056366666667]
Latitude/Longitude = [12.957963][77.67056366666667]
Latitude/Longitude = [12.957961999999998][77.67056466666668]
Latitude/Longitude = [12.957961999999998][77.67056466666668]
Latitude/Longitude = [12.957961999999998][77.67056466666668]
Latitude/Longitude = [12.957961999999998][77.67056466666668]
Latitude/Longitude = [12.95796][77.67056666666667]
Latitude/Longitude = [12.95796][77.67056666666667]
Latitude/Longitude = [12.957958999999999][77.67056866666667]
Latitude/Longitude = [12.957958999999999][77.67056866666667]
Latitude/Longitude = [12.957958999999999][77.67056966666667]
Latitude/Longitude = [12.957958999999999][77.67056966666667]
Latitude/Longitude = [12.957961999999998][77.67057066666668]
Latitude/Longitude = [12.957961999999998][77.67057066666668]
Latitude/Longitude = [12.957958999999999][77.67057266666667]
Latitude/Longitude = [12.957958999999999][77.67057266666667]


I will still be playing around the API and keep posting too.

Thursday, October 28, 2010

NMEA GPS Reads

8:58:00 PM Posted by Satish , , No comments
GPS modules typically put out a series of standard strings of information, under something called the National Marine Electronics Association (NMEA) protocol. More information on NMEA standard data strings can be found at this site.
GPS receiver gives the reading starting with the following strings and each string appended data has its own significant.
  • $GPGGA: Global Positioning System Fix Data
  • $GPGSV: GPS satellites in view
  • $GPGSA: GPS DOP and active satellites
  • $GPRMC: Recommended minimum specific GPS/Transit data
Each of these sentences contains a wealth of data. For example, here are a few instances of the $GPRMC string.
eg1. $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62 eg2. $GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E*68
225446 Time of fix 22:54:46 UTC A Navigation receiver warning A = Valid position, V = Warning 4916.45,N Latitude 49 deg. 16.45 min. North 12311.12,W Longitude 123 deg. 11.12 min. West 000.5 Speed over ground, Knots 054.7 Course Made Good, degrees true 191194 UTC Date of fix, 19 November 1994 020.3,E Magnetic variation, 20.3 deg. East *68 mandatory checksum
eg3. $GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70
1 2 3 4 5 6 7 8 9 10 11 12
 
1 220516 Time Stamp 2 A validity - A-ok, V-invalid 3 5133.82 current Latitude 4 N North/South 5 00042.24 current Longitude 6 W East/West 7 173.8 Speed in knots 8 231.8 True course 9 130694 Date Stamp 10 004.2 Variation 11 W East/West 12 *70 checksum
eg4. for NMEA 0183 version 3.00 active the Mode indicator field is added
$GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a,m*hh
Field #
1 = UTC time of fix 2 = Data status (A=Valid position, V=navigation receiver warning) 3 = Latitude of fix 4 = N or S of longitude 5 = Longitude of fix 6 = E or W of longitude 7 = Speed over ground in knots 8 = Track made good in degrees True 9 = UTC date of fix 10= Magnetic variation degrees (Easterly var. subtracts from true course) 11= E or W of magnetic variation 12= Mode indicator, (A=Autonomous, D=Differential, E=Estimated, N=Data not valid) 13 = Checksum
The full documentation you can get here.

Sunday, October 24, 2010

GPS Receiver Module - iW-GPS-01

6:06:00 PM Posted by Satish , , 2 comments
Got iW-GPS-01(with Active Antenna) and iWave's GPS Platforms. The GPS board is mounted on a GPS platform with serial out and has a external antenna. The board has got a serial port and a power input.The board operates on 5 - 35 V input.

Full specification attached here.

Reads I got from Hiper Terminal.

$GPGSA,A,3,14,06,24,03,30,31,21,22,,,,,2.1,1.0,1.8*3D
$GPGSV,3,1,12,31,72,201,17,22,47,056,15,24,38,314,26,14,37,004,24*72
$GPGSV,3,2,12,18,26,093,,30,26,106,24,06,17,219,25,25,16,084,19*73
$GPGSV,3,3,12,19,14,265,,21,13,150,22,03,13,232,27,16,06,191,*7C
$GPRMC,181611.000,A,1257.7947,N,07740.3910,E,0.16,57.21,231010,,,A*54
$GPVTG,57.21,T,,M,0.16,N,0.3,K,A*38
$GPGGA,181612.000,1257.7948,N,07740.3910,E,1,08,1.0,916.6,M,-88.4,M,,0000*75
$GPGSA,A,3,14,06,24,03,30,31,21,22,,,,,2.1,1.0,1.8*3D
$GPRMC,181612.000,A,1257.7948,N,07740.3910,E,0.40,131.00,231010,,,A*69
$GPVTG,131.00,T,,M,0.40,N,0.7,K,A*0D
$GPGGA,181613.000,1257.7946,N,07740.3911,E,1,08,1.0,917.4,M,-88.4,M,,0000*78
$GPGSA,A,3,14,06,24,03,30,31,21,22,,,,,2.1,1.0,1.8*3D
$GPGSV,3,1,12,31,72,201,17,22,47,056,14,24,38,314,26,14,37,004,24*73
$GPGSV,3,2,12,18,26,093,,30,26,106,24,06,17,219,25,25,16,084,19*73
$GPGSV,3,3,12,19,14,265,,21,13,150,22,03,13,232,27,16,06,191,*7C
$GPRMC,181613.000,A,1257.7946,N,07740.3911,E,0.74,149.98,231010,,,A*6E
$GPVTG,149.98,T,,M,0.74,N,1.4,K,A*06


Some pictures of my setup.





Let play around it. 

Tuesday, September 28, 2010

EASY JSF WITH ECLIPSE - BASICS

8:26:00 PM Posted by Satish , , , , No comments

What is JSF

 JavaServer Faces (JSF) is a UI component based Java Web application framework. JSF is serverbased, e.g. the JSF UI components and their state are represented on the server with a defined life-cycle of the UI components. JSF is part of the Java EE standard.


A JSF application run in a standard web container, for example Tomcat or Jetty . 

A JSF application

A JSF application consists of web pages with JSF UI components. A JSF application requires also some configuration files ("faces-config.xml" and "web.xml").

The faces-config.xml defines:

  1. Managed Bean - the data elements of the JSF application (managed beans and backing beans) Represents a Java class which will be created dynamically during runtime of the JSF application. It can be defined for which scope the bean is valid (Session, Request, Application or none).
  2. the navigation between web pages
  3. data validators - Used to check the validity of UI input
  4. data converters -Used to translate between UI and model
Managed beans are simple Java objects (POJO's) which are declared in "faces-config.xml" and can be used in an JSF application. For example you can define a Java object "Person". Once you define the object in faces-config.xml you can use the attributes of Person in your JSF UI components, e.g. by binding the value "firstName" of this object to an JSF input field.

JSF uses the Unified Expression Language (EL) to bind UI components to object attributes or methods. 

Value and Method Binding

In JSF you can access the values of a managed bean via value binding. For value binding the universal Expression Language (EL) is used (to access bean and / or methods). In JSF you do not need to specify the get() or set() method but just the variable name.

Method binding can be used to bind a JSF component, e.g. a button to an method of a Java class.

  • Expression Language statements either start with "${" or with "#{" and end with "}". JSP EL expressions are using the ${...} syntax. These EL expressions are immediately evaluated. JSF EL expressions are of the type #{...}. These are only evaluated when needed (and otherwise stored as strings).

Prerequisites to use JSF  

To use JSF you need: 

  1. JSF Implementation (in the form of the JSF jars)
  2. The JSTL tags library
  3. A Java runtime environment
  4. A web-container to use JSF in (for example Tomcat)
JSF Main features

JSP has the following main features:

  1. JSP is based on the Model-View-Controller concept
  2. JSP has a stateful UI component model, e.g. each component is aware of its data
  3. JSF separates the functionality of a component from the display of the component. The renderer is responsible of displaying the component for a certain client. This renderer can get exchanged. The standard renderer for JSF components is the HTML renderer.
  4. JSP support listeners on UI components
  5. JSP support data validation, data binding and data conversion between the UI and the model
JSP and JSF 

In this tutorial the JSF application will be build based on JavaServer Pages (JSP's). JSTL tags are used to include JSF UI components into the JSP. This is standard in JSF 1.2. The JSF 2.0 version is using Facelets. 

JSF configuration files

JSF is based on the following configuration files:

  1. web.xml - General web application configuration file
  2. faces-config.xml - Contains the configuration of the JSF application.
web.xml

JSF requires the central configuration list "web.xml" in the directory WEB-INF of the application. This is similar to other web-applications which are based on servlets.

You must specify in "web.xml" that a "FacesServlet" is responsible for handling JSF applications. "FacesServlet" is the central controller for the JSF application. "FacesServlet" receives all requests for the JSF application and initializes the JSF components before the JSP is displayed.

faces-config.xml

"faces-config.xml" allows to configure the application, managed beans, convertors, validators, and navigation.


Sunday, September 26, 2010

EASY JSF WITH ECLIPSE - CONFIGURATION

8:33:00 PM Posted by Satish , , , , No comments


This tutorial was developed with Java 1.6, JavaServerFaces 1.2, the Apache MyFaces JSF implementation, Tomcat 6.0 and Eclipse 3.5.

Eclipse & Tomcat

For JSP development you need the Eclipse and an installed Tomcat.
JSF library

A JSF library is required. We will later use Eclipse to download and install the Apache MyFaces JSF implementation during project creation.

JSLT library

Download the JSLT library from https://jstl.dev.java.net/.

JSF Project

Create JSF Project

Create a new Dynamic Web Project "JSF". 
Under "Configuration" select "JavaServer Faces v1.2".


Press next until you see the following screen. The JSF and JSTL is showing in the next screen, because I have already configured to my eclipse. For the first time nothing will be there. 


The first time you create a JSF project you need to install / download a JSF implementation. Press the "Download library..." button and select the Apache Library and install it.




Press Manage libraries and create a library for JSTL.



Press new now.


Add the two downloaded JSTL jars and press OK.


Check JSTL and click Finish.



Review the "web.xml" file. It has an entry for the Faces Servlet and for the servlet mapping. Also the file "faces-config.xml" has been created.

Right click on the JSF project and click on Properties. Go to Java Build path -> Libraries and expand the JSF and JSTL library. You can see all the jars associated to the library and the "Publish/ export dependency" shows where the jars has to be placed, when you deploy to the server.


Next step is to add tomcat 6 to eclipse.

Monday, August 30, 2010

INCOMPLETE ROAD MAP TO Thread.run()

8:40:00 PM Posted by Satish , , 1 comment

When it comes to java thread, I always try to dig how the Thread.run() method get executed when, the Thread.start() method get called. Recently, I downloaded the JDK 5 source code and started looking into the implementation.

There is two way we can create user defined threads and in both the scenarios, we used to call the Thread constructor and that internally calls the Thread.init(). init() has the following signature. Basically this method initialize the Thread class members like group, daemon, priority, name etc.

private void init(ThreadGroup paramThreadGroup, Runnable paramRunnable, String paramString, long paramLong);

Now the actual game begins, when you call the start() on a thread object. start() method adds the thread to a thread group as per the initializing parameter. And here the start() calls a native method start0() after that. To find out further, I started looking at the Thread's native implementation.Starting with \JDK 1.5 Source\j2se\src\share\native\java\lang\Thread.c, I found the start0() method is mapped to (void *)&JVM_StartThread (refer line no 25). In \JDK 1.5 Source\j2se\src\share\javavm\export\jvm.h from line no 206 to 256 JNI configuration is there for Thread class native implementation.

Now it is time to jump to the implementation of (void *)&JVM_StartThread method. \JDK 1.5 Source\hotspot\src\share\vm\prims\jvm.cpp contains the implementation of above method. This method basically create a native Java thread and starts the same (refer from line no. 2257 to 2314).

To see the native thread start method, I went to \JDK 1.5 Source\hotspot\src\share\vm\runtime\thread.cpp. In the Thread::start(Thread* thread) method, the particular thread is initialized to RUNNABLE state followed by passing the thread reference to os.

In \JDK 1.5 Source\hotspot\src\share\vm\runtime\os.cpp, the os::start_thread(Thread* thread) method creates a OS thread.

As I have limited exposure in C/C++, I could not move further. But I discussed this in some of the Java forums and found that there will be different thread implementation for different OS and once the OS thread is created, the OS calls and manages the Thread.run() method according to it's own mechanism.

To move further in this, I was needing a C/C++ developer and could probably have looked in to the OS level. I started looking in to Ubuntu, but got confused. So finally left that there.  

Tuesday, August 10, 2010

JAVA EVENT DELEGATION MODEL

10:09:00 PM Posted by Satish , , , , 4 comments
Event broadcasting sytem in java  called as EVENT DELEGATION MODEL. This model is based on the Event Source and Event Listeners. Event Listener is an object that receives the message/ events. The Event Source is any object which creates the message/ event. The Event Delegation Model is based on the following things.
  • Event Source - This class which broadcasts the events.
  • Event Listeners - The classes which receive notifications of events. The Event Listeners are implemented to Adapter classes for the default action for a particular event. The API user is free to override the Adapter version of the method. The event listeners should extend the java.util.EventListener interface.
  • Event Object - The object which describes the event. The event object class should extend java.util.EventObject class.
  • Event Manager - This is the class which manages the relationship between the event and listeners. This class will register and de-register the listeners for a particular event.
The following picture explains how the event model works.


Now time to look at the programming implementation.

Event Class:

1
2
3
4
5
6
7
8
import java.util.EventObject;

//Declare the event. It must extend EventObject.
public class MyEvent extends EventObject {
    public MyEvent(Object source) {
        super(source);
    }
}

Event Listener:

1
2
3
4
5
6
7
8
9
package org.satish.event;

import java.util.EventListener;

//Declare the listener class. It must extend EventListener.
//A class must implement this interface to get MyEvents.
public interface MyEventListener extends EventListener {
    public void myEventOccurred(MyEvent evt);
}

Event Manager:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package org.satish.event;

import java.util.ArrayList;
import java.util.List;

//Add the event registration and notification code to a class. 
public class EventManager {
    // Create the listener list
    protected List listenerList = new ArrayList();

    // This methods allows classes to register for MyEvents
    public void addMyEventListener(MyEventListener listener) {
        listenerList.add(listener);
    }

    // This methods allows classes to unregister for MyEvents
    public void removeMyEventListener(MyEventListener listener) {
        listenerList.remove(listener);
    }

    // This method is used to fire MyEvents
    void fireMyEvent(MyEvent evt) {
        System.out.println("Got the event." + listenerList.size());
        // Iterate the Listener list to fire the implemented method of each
        // listener.
        for (MyEventListener myEventListener : listenerList) {
            System.out
                    .println("Firing listener method for each of the listener.");
            myEventListener.myEventOccurred(evt);
        }
    }
}

Event Source:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package org.satish.event;

public class EventGenerator implements Runnable{
    
    Thread t;
    EventManager c;
    public EventGenerator(EventManager c) {
        // TODO Auto-generated constructor stub
        this.c = c;
        t =  new Thread(this, "Event Generator");
        t.start();
    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Event generator in the run method.");
        while(true){
            System.out.println("Generate a event.");
            c.fireMyEvent(new MyEvent(this));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }
}

Demo Starter:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.satish.event;

public class EventDemo {
    public static void main(String[] args) {

        EventManager c = new EventManager();

        // Event Generator is instantiated to generate the events.
        EventGenerator eventGenerator = new EventGenerator(c);
        System.out.println("Event generator started.");

        // Register for MyEvents from c
        // Here the object of MyEventListener is been created, implementing the
        // interface method. Developers are free to user the Adapter class
        // object or use their own implementation class object.
        c.addMyEventListener(new MyEventListener() {
            public void myEventOccurred(MyEvent evt) {
                // MyEvent was fired
                System.out.println("1st Event Fired");
            }
        });
        System.out.println("First event is registered.");

        // Register another event
        c.addMyEventListener(new MyEventListener() {
            public void myEventOccurred(MyEvent evt) {
                // MyEvent was fired
                System.out.println(" 2nd Event Fired");
            }
        });
        System.out.println("Second event is registered.");
    }
}


This is the basic implementation of event delegation model. In a multi threaded environment, where more than one listener are registered to each event and the generation of event is quite fast, then you need to change your design to deal with the requirement.

Source Code:

Complete source code you can fork from GitHub.

Saturday, July 17, 2010

TOMCAT SSL CONFIGURATION IN UBUNTU

11:12:00 PM Posted by Satish , , , , No comments

This post is made considering the the system is having jdk 6 and Tomcat 6 installed and "JAVA_HOME" environment variable is set.

Bellow are the steps to configure tomcat to listen https port (8443)

1. Create a certificate keystore by executing the following command. It will ask few questions. It will also ask one password to enter. You have to remember this password as you need to specify that to configuration file.  You can store the keystore any where.
    keytool -genkey -alias tomcat -keyalg RSA 
    \-keystore /usr/local/tomcat/conf/.keystore

2. Now configure "tomcat_home"\conf\server.xml, add the following code.
    <Connector
    port="8443" 
    protocol="org.apache.coyote.http11.Http11Protocol"   
    maxThreads="200"
    scheme="https" secure="true" SSLEnabled="true"
    keystoreFile="/usr/local/tomcat/conf/.keystore"  
    keystorePass="password"
    clientAuth="false" 
    sslProtocol="TLS"/>  

You are done with your configuration. Save the "server.xml" file. Restart your tomcat. Now type https://localhost:8443

This should show you the same tomcat homepage but in secured format.