am currently workin on RFID Technology
and am stuck with the socket connectivity...
there is a client which is an EXE file called RFID.exe
now we need to create a server program to connect the client n read the msgs sent by the client application. in the server program v need to store the msgs using the HASH table n filter the msgs with no duplication and store it in the database..
these are the following codes..
but cant understand any..
and the client prog is missing...
RFID data processor
package com.satyam.rfid.processor;
import java.util.Hashtable;
import org.apache.log4j.Logger;
import com.satyam.impl.bo.DCPickConfirmation;
import com.satyam.rfid.util.RFIDUtil;
import com.satyam.rfid.beans.RFIDBean;
import com.satyam.rfid.beans.ReaderBean;
import com.satyam.rfid.dao.CommonDao;
import com.satyam.rfid.exceptions.InvalidMsg;
import com.satyam.rfid.util.RFIDInfo;
import com.satyam.rfid.util.RFIDLogger;
/**
* @author admin
*/
public class RFIDDataProcessor {
private RFIDBean rfidBean;
private CommonDao dao;
private ReaderBean readerBean;
private Logger log;
//private Hashtable htMsg = null;
private Hashtable htReader = new Hashtable();
private Hashtable htLogicalLocation= new Hashtable();
/** Detault constructor
*
*/
public RFIDDataProcessor(){
super();
log = RFIDLogger.getLogger(this);
}
/**
*/
public void doProcess(Hashtable htMsg){
//log.info("Start of doProcess()");
try{
String msgType=(String)htMsg.get(RFIDInfo.RFID_MSG_TYPE);
if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_CONFIG)){
String readerIP = (String)htMsg.get(RFIDInfo.RFID_MSG_REARDER_IP);
String readerPort = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_PORT);
String readerName = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_NAME);
int cycleTime = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_CYCLE_TIME));
int totalAntenna = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT));
int totalAntGroup = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP));
Hashtable htAntennaGroupID=new Hashtable();
Hashtable htAntennaGroupName = new Hashtable();
for (int count=0;count<totalAntGroup;count++){
//log.info((String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count));
//log.info(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count);
htAntennaGroupName.put(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count,(String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count));
htAntennaGroupID.put(RFIDInfo.RFID_MSG_READER_ANT_GRP_ID+count,(String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_ID+count));
}
if(htReader.containsValue(readerName)){
log.error("Reader" + readerName + "is Already Connected");
}
else{
// Setting the RFID Reader Bean with the above values
log.info("Setting the RFID Reader Bean");
ReaderBean readerBean = new ReaderBean();
readerBean.setReaderIP(readerIP);
readerBean.setReaderName(readerName);
readerBean.setReaderPort(readerPort);
readerBean.setTotalAntenna(totalAntenna);
readerBean.setReadCycleTime(cycleTime);
readerBean.setTotalGrouping(totalAntGroup);
readerBean.setAntennaGroupID(htAntennaGroupID);
readerBean.setAntennaGroupName(htAntennaGroupName);
//Creating DAO object
CommonDao dao = new CommonDao();
boolean readerExist=false;
readerExist=dao.readerExtsit(readerBean);
log.info("After DB Insertion");
if(readerExist){
// Calling DAO for checking readerdetails
dao.deleteAntennaDetails(readerBean.getReaderName());
dao.deleteReaderDetails(readerBean);
// Calling DAO for inserting Reader data into database
dao.insertReaderDetails(readerBean);
}else {
// Calling DAO for inserting Reader data into database
dao.insertReaderDetails(readerBean);
}
//Creating New Reader object
RFIDReader rfidDataPrcr = new RFIDReader(readerBean,htAntennaGroupID,htAntennaGroupName);
//Adding new reader to the reader hashtable
htReader.put(rfidDataPrcr,readerName);
//Creating New Logical Location
for(int count=0;count<readerBean.getTotalGroup();count++){
String logicalGName = (String)htAntennaGroupName.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count);
String logicalGAntID =(String)htAntennaGroupID.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_ID+count);
dao.insertAntennaDetails(readerName,logicalGAntID,logicalGName);
RFIDLogicalLocation logicalLoc = new RFIDLogicalLocation(readerName,(String)htAntennaGroupID.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_ID+count),(String)htAntennaGroupName.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME+count),cycleTime);
this.htLogicalLocation.put(logicalGName,logicalLoc);
}
}
}else if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_DATA)){
String readerName = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_NAME);
int antennaID = Integer.parseInt((String)htMsg.get(RFIDInfo.RFID_MSG_ANTENNA_ID));
String tagID = (String)htMsg.get(RFIDInfo.RFID_MSG_TAG_ID);
String logicalLocName = (String)htMsg.get(RFIDInfo.RFID_MSG_READER_ANT_GRP_NAME);
//System.out.println(logicalLocName);
RFIDLogicalLocation Loc = (RFIDLogicalLocation)this.htLogicalLocation.get(logicalLocName);
// Setting the RFID Bean with the above values
log.info("Setting the RFID bean");
RFIDBean rfidBean = new RFIDBean();
rfidBean.setReaderName(readerName);
rfidBean.setAntennaID(antennaID);
rfidBean.setRFIDTagId(tagID);
Loc.doProcess(tagID,Loc,this.htLogicalLocation);
// Instantiating the processor
log.info("Calling life cycle methods of RFIDDataProcessor");
}else if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_HHT)){
log.info("Received Message from Handheld...");
String picklist = (String)htMsg.get(RFIDInfo.RFID_MSG_PICKLIST_NO);
log.info((String)htMsg.get(RFIDInfo.RFID_MSG_PICKLIST_NO));
DCPickConfirmation pickConfirm = new DCPickConfirmation();
pickConfirm.doProcess(picklist);
}
else{
throw new InvalidMsg("Invalid Message");
}
}catch(InvalidMsg ill){
log.error(RFIDUtil.getPropertyValue("rfid.invalidLocation.error"));
}catch(Exception e){
e.printStackTrace();
log.error(e.getMessage());
}
}
/** This method is called finally to kill all the unused instances
*/
public void destroy(){
log.info("Start of Destroy");
rfidBean = null;
dao = null;
log.info("End of Destroy");
}
}
RFID message parser
/**
*
*/
package com.satyam.rfid.processor;
import java.util.Hashtable;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import com.satyam.rfid.util.RFIDLogger;
/**
* @author Badmabooshan
* @version 1.0
*/
public class RFIDMessageParser {
private Hashtable msgMap = new Hashtable(100);
private Logger log;
//Default constructor
public RFIDMessageParser(){
log = RFIDLogger.getLogger(this);
}
public Hashtable getMessageDetails(String recMsg){
StringTokenizer messageContent;
int count;
String msgSegment;
StringTokenizer msg;
messageContent = new StringTokenizer(recMsg,"$");
if(messageContent.countTokens()>=1)
{
int messageSize=messageContent.countTokens();
for (count =0; count<messageSize; count++)
{
msg=null;
msgSegment = messageContent.nextToken();
msg = new StringTokenizer(msgSegment,":");
String key;
String value;
key = msg.nextToken();
value = msg.nextToken();
msgMap.put(key,value);
//log.info("Key"+key);
//log.info("value"+value);
}
//System.out.println(msgMap.size());
}
return msgMap;
}
}
RFID socket Reader:
package com.satyam.rfid.processor;
import com.satyam.rfid.dao.CommonDao;
import com.satyam.rfid.exceptions.InvalidMsg;
import com.satyam.rfid.util.RFIDInfo;
import com.satyam.rfid.util.RFIDLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.Hashtable;
import org.apache.log4j.Logger;
/**
* @author admin
*/
public class RFIDSocketReader implements Runnable{
/**
* @uml.property name="socket"
*/
private Socket socket;
private Logger log;
private String receviedMsg=null;
private Hashtable ht=null;
private InputStream is;
private BufferedReader brOut;
private RFIDDataProcessor rfidDataPrcr= new RFIDDataProcessor();
//initializes the logger
public RFIDSocketReader() {
log = RFIDLogger.getLogger(this);
}
/**This method processes the InputStream from the socket and parses the
* character array. It initialises the RFIDDataProcessor and invoke the life
* cycle methods for further processing.
* Finally it calls the close method to close the socket connection.
*/
public void run() {
try {
//Instantiating CommonDao
CommonDao dao = new CommonDao();
log.info("Connection accepted from " + socket.getRemoteSocketAddress());
while(true){
if(this.brOut!=null){
receviedMsg = this.brOut.readLine();
}
else{
receviedMsg="Disconnected";
}
if(receviedMsg.equals("Disconnected")){
throw new InvalidMsg(receviedMsg);
}else if(receviedMsg==null){
throw new InvalidMsg(receviedMsg);
}
else{
log.info("Received Message :" + receviedMsg);
// Parsing the RFID message from the above string
RFIDMessageParser rmp = new RFIDMessageParser();
ht = rmp.getMessageDetails(receviedMsg);
//Getting RFID Details from the parser results
String msgType=(String)ht.get(RFIDInfo.RFID_MSG_TYPE);
//Diconnect the Client
if(msgType.equalsIgnoreCase(RFIDInfo.RFID_MSG_CDISCONNECT)){ // Diconnecting the Reader
close();
}
else{
log.info("Starting Life Cycle Process");
rfidDataPrcr.doProcess(ht);
//rfidDataPrcr.destroy();
//rfidDataPrcr = null;
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* @return Returns the socket.
* @uml.property name="socket"
*/
public Socket getSocket() {
return socket;
}
/**
* @param socket The socket to set.
* @uml.property name="socket"
*/
public void setSocket(Socket socket) {
try {
this.socket = socket;
//Getting the input stream from the socket
is = socket.getInputStream();
//Creating the BufferedReader from the input stream
brOut = new BufferedReader(new InputStreamReader(is));
} catch (Exception e) {
e.printStackTrace();
}
}
/**This method closes the socket connection
*/
private void close(){
try{
this.socket.close();
this.is = null;
this.brOut=null;
Thread t = Thread.currentThread();
log.info("Reader Disconnected...End the thread:" + t.getName());
if(t.isDaemon()){
t.destroy();
}
}catch(SocketException se){
log.error(se.getMessage());
}catch(IOException ioe){
log.error(ioe.getMessage());
}
}
}
TCPIP:
package com.satyam.rfid.processor;
/**
* @author bb27629
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
public class TCPIP implements Runnable{
BufferedReader bufferedReaderOut = null;
Thread thread = null;
Socket socketClient = null;
byte[] byteArray = new byte[255];
int numBytes=0;
boolean toRun = false;
public TCPIP()
{
}
public TCPIP(Socket socketClient)
{
toRun = true;
this.socketClient = socketClient;
try{
bufferedReaderOut = new BufferedReader( new InputStreamReader(socketClient.getInputStream()));
}catch(IOException ioe){
System.out.println("Error : " + ioe.getMessage());
ioe.printStackTrace();
}
thread = new Thread(this);
thread.start();
}
public void stopThread()
{
try
{
if(!(this.socketClient==null))
{
this.socketClient.close();
this.thread.stop();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
boolean toRun = true;
byte[] byteArray = new byte[255];
int intByteCount = 0,i=0;
char[] chrArray;
while(true)
{
try{
String strData = bufferedReaderOut.readLine();
System.out.println("Received Data : " + strData);
byteArray = strData.getBytes();
System.out.println("Length : " + byteArray.length);
System.out.println("Reading....");
}catch(Exception e)
{
System.out.println("Error :" + e.getMessage());
e.printStackTrace();
}
}
}
public static TCPIP ipConfig(String strIpAddress)
{
TCPIP tcpip =null;
boolean blnConnected = true;
while (blnConnected)
{
try{
Connection connection = null;
//System.out.println("IPCONFIG");
Socket socketClient= new Socket( InetAddress.getByName(strIpAddress),23);
//System.out.println("IPCONFIG");
tcpip = new TCPIP(socketClient);
blnConnected=false;
}catch(Exception e)
{
System.out.println("********* Error : " + e.getMessage());
e.printStackTrace();
blnConnected=true;
}
}
return tcpip;
}
public static void main(String[] args)
{
TCPIP tcpIpConfig = new TCPIP();
tcpIpConfig.ipConfig("172.18.33.164");
//TCPIP.ipConfig("172.18.33.157");
}
}