3 Replies - 317 Views - Last Post: 07 December 2011 - 07:54 PM Rate Topic: -----

#1 pgfdbug  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-April 08

Project looping problem

Posted 23 November 2011 - 11:48 AM

I can not figure out why my code is looping and running in multiple times for each packet. I thought it had to do with the units so I put them into a hashset to make sure there would only be one time for each capture. I am at a complete loss here. Also please excuse the messy code I am still learning and tinkering. The log capture is attached.

package prealert;

import org.apache.log4j.Logger;
import java.util.HashSet;
import java.util.Set;
import java.util.LinkedHashSet;

public class PrintOut {

	public long timeSec;
	private int sign = 0;
        public HashSet units;
        public LinkedHashSet station1;
        public LinkedHashSet type1;
        public LinkedHashSet location1;

	public String data = "";
        private static String betaString1;
	public static String betaString;
	public static String colorString;

	public static final int ENGINEBOX = 1;
    public static final int streetalarm = 2;
	public static final int FIRSTDUE = 3;
	public static final int engine = 4;
	public static final int ambo = 5;
	public static final int medic = 6;
	public static String station = "";
	public static String location = "";
	public static String type = "";
	public String ID = "";
	public String map = "";

	private Logger log = Logger.getRootLogger();

	public PrintOut(String header) {
		ID = Reciever.getId(header);
		this.data += header;
	}

	public void addPart(String dataIn) {

		log.info("Adding part to id: " + ID);
		this.data += dataIn;
                
		if (data.contains("Station:")) {
			int temp = data.indexOf("Station:");
			station = new String(data.substring(temp + 8,(temp + 8) + 2));
			station += ".";
                        log.info("Station :" + station);
		}
		if (data.contains("Final Type:")) {
			int temp = data.indexOf("Final Type:");
			type = new String(data.substring(temp + 11,(temp +11) + 6));
			type += ".";
                           log.info("Type :" + type);
		}
		if (data.contains("Location:")) {
			int temp = data.indexOf("Location:");
			location = new String(data.substring(temp + 9,(temp +9) + 25));
			location += ".";
                       
			log.info("Location :" + location);
		}
           
            
			
		// String soundString = "";
		// String betaString = "";
				
		if ((type.contains("APTF")) || (type.contains("HOUSEF"))
				|| (type.contains("BUILDF")) || (type.contains("APTF"))
				|| (type.contains("BUILDT")) || (type.contains("APTT"))
				|| (type.contains("APTT")) || (type.contains("BUILDT"))
				|| (type.contains("HOUSET")) || (type.contains("HOUSET"))
				|| (type.contains("HOUSEF")) || (type.contains("BUILDF"))
				|| (type.contains("TOWNHF")) || (type.contains("TOWNHF"))
				|| (type.contains("TOWNHT")) || (type.contains("TOWNHT")) && (station.contains("46")) && (units.contains("PE846"))){ 
                                        colorString = "yellow";  
                                        final String soundString = "FIRSTDUE";
					final String betaString1 = "FIRTS DUE";
                                        new AudioPlayer("sounds/"+ soundString +".wav").start();
						sign = FIRSTDUE;
                                                betaString = betaString1;
						log.info("First Due");
			}
	    
                else if ((type.contains("APTF")) || (type.contains("HOUSEF"))
						|| (type.contains("BUILDF")) || (type.contains("APTF"))
						|| (type.contains("BUILDT")) || (type.contains("APTT"))
						|| (type.contains("APTT")) || (type.contains("BUILDT"))
						|| (type.contains("HOUSET")) || (type.contains("HOUSET"))
						|| (type.contains("HOUSEF")) || (type.contains("BUILDF"))
						|| (type.contains("TOWNHF")) || (type.contains("TOWNHF"))
						|| (type.contains("TOWNHT")) || (type.contains("TOWNHT")) && (station.contains("!=46")) && (units.contains("PE846"))){
						colorString = "green";
                                                final String soundString = "ENGINEBOX";
						final String betaString1 = "Box Alarm";
                                                new AudioPlayer("sounds/"+ soundString +".wav").start();
						sign = ENGINEBOX;
                                                betaString = betaString1;
						log.info("Box Alarm");
			}
					
	    else if ((type.contains("APTG")) || (type.contains("HOUSEG"))
							|| (type.contains("BUILDG")) || (type.contains("COLAPS"))
							|| (type.contains("CONFSP")) || (type.contains("PLANE"))
							|| (type.contains("RTASK")) || (type.contains("STREET"))
							|| (type.contains("TOWNHG")) && (units.contains("PE846"))){
						colorString = "orange";
                                                final String soundString = "streetalarm";
						final String betaString1 = "Street Alarm";
                                                new AudioPlayer("sounds/"+ soundString +".wav").start();
						sign = streetalarm;
                                                betaString = betaString1;
						log.info("Street Alarm");
			}
            else if ((units.contains("PE846")) && (units.contains("A846")) && (units.contains("MD846"))){
			colorString = "red";
                        final String soundString = "engineambomedic";
			final String betaString1 = "Engine : Ambo : Medic ";
                        new AudioPlayer("sounds/"+ soundString +".wav").start();
			sign = engine;
                        betaString = betaString1;
                        log.info("Engine Ambo Medic");
			}
            else if ((units.contains("PE846")) && (units.contains("A846"))){
                        colorString = "red";
			final String soundString = "engineambo";
			final String betaString1 = "Engine : Ambo ";
                        new AudioPlayer("sounds/"+ soundString +".wav").start();
			sign = engine;
                        betaString = betaString1;
                        log.info("Engine Ambo");
			}
            else if ((units.contains("PE846")) && (units.contains("MD846"))){
                        colorString = "red";
			final String soundString = "enginemedic";
			final String betaString1 = "Engine : Medic ";
                        new AudioPlayer("sounds/"+ soundString +".wav").start();
			sign = engine;
                        betaString = betaString1;
                        log.info("Engine Medic");
			}
            else if ((units.contains("A846")) && (units.contains("MD846"))){
                        colorString = "red";
			final String soundString = "ambomedic";
			final String betaString1 = "Ambo : Medic ";
                        new AudioPlayer("sounds/"+ soundString +".wav").start();
			sign = ambo;
                        betaString = betaString1;
                        log.info("Ambo Medic");
			}
            else if (units.contains("PE846")){
                        colorString = "red";
			final String soundString = "engine";
			final String betaString1 = "Engine";
                        new AudioPlayer("sounds/"+ soundString +".wav").start();
			sign = engine;
                             betaString = betaString1;
                        log.info("Engine");
			}  
            else if (units.contains("A846")){
                                colorString = "red";
				final String soundString = "ambo";
				final String betaString1 = "Ambo";
                                new AudioPlayer("sounds/"+ soundString +".wav").start();
				sign = ambo;
                                betaString = betaString1;
                                log.info("Ambo");
			}
            else if (units.contains("MD846")){
                            colorString = "red";
				final String soundString = "medic";
				final String betaString1 = "Medic";
                                new AudioPlayer("sounds/"+ soundString +".wav").start();
				sign = medic;
                                betaString = betaString1;
                                log.info("Medic");
			}

				try {
					log.info("New SetBoard, tempSign: " + tempSign + ", ID: ");
					new SetBoard(tempSign).start();
				} catch (Exception e) {
					log.error("Exception while setboard: ", e);
				}
	}
}
	


	
	public String toString() {
		return (ID + location + station);
	}

	public int hashCode() {
		return toString().hashCode();
	}
}



and the other part
package prealert;

import jpcap.JpcapCaptor;
import jpcap.PacketReceiver;
import jpcap.packet.*;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;

//import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;

public class Reciever extends Thread implements PacketReceiver {

	public JpcapCaptor jpcap;
	public enum PacketPart {
		ID, DATA, OTHER
	};

	private Map<String, PrintOut> calls = new HashMap<String, PrintOut>();
	private PrintOut recentPrintOut;
        public HashSet units;

	private Logger log = Logger.getRootLogger();

	public void receivePacket(Packet packet) {

		if ((((IPPacket) packet).dst_ip.toString().equals("/xxx.xx.xx.xxx")) || (((IPPacket) packet).src_ip.toString().equals("/xxx.xx.xx.xxx"))) {

			String data3 = new String(packet.data, Charset.forName("UTF-16"));
			data3 = data3.replaceAll ("[^A-Za-z0-9#://>/s() ]", "");
			String data1 = new String(packet.data, Charset.forName("UTF-16BE"));
			data1 = data1.replaceAll ("[^A-Za-z0-9#://>/s() ]", "");
			String data2 = new String(packet.data, Charset.forName("UTF-16LE"));
			data2 = data2.replaceAll ("[^A-Za-z0-9#://>/s() ]", "");
			
                        String data = data3 + data1 + data2;
                        

            Set<String> units = new HashSet<String>();
            if (data.contains("PE846")){
                units.add("PE846");
            }
            if (data.contains("A846")){
                units.add("A846");
            }
            if (data.contains("MD846")){
                units.add("MD846");
            }
			}					
		}
	}
	private static PacketPart whatPart(String data) {

		if (data.contains("iCall #F")) {
			return PacketPart.ID;
		}
		if (data.contains("Darea:") || (data.contains("PrimeUnit:")) || (data.contains("Location:")) || (data.contains("Station:"))
				|| (data.contains("CREATE")) || (data.contains("Final Type")) || (data.contains("ENTRY"))
				|| (data.contains("Dispatch")) || (data.contains(". 2.")) || (data.contains(". 6."))
				|| (data.contains("DISP")) || (data.contains("846"))) {
			return PacketPart.DATA;
		}

		return PacketPart.OTHER;
	}

	public static String getId(String data) {
		int index = data.indexOf("iCall #");
		return data.substring(index + 8, index + 17);
	}
	
	public void run() {
		try {
			this.jpcap.setFilter("tcp", true);
		} catch (Exception e) {
			log.error("IOException: ", e);
		}
		this.jpcap.loopPacket(-1, new Reciever());
	}
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Project looping problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Project looping problem

Posted 23 November 2011 - 09:50 PM

Where is it looping ?

And honestly this

       if ((type.contains("APTF")) || (type.contains("HOUSEF"))
                || (type.contains("BUILDF")) || (type.contains("APTF"))
                || (type.contains("BUILDT")) || (type.contains("APTT"))
                || (type.contains("APTT")) || (type.contains("BUILDT"))
                || (type.contains("HOUSET")) || (type.contains("HOUSET"))
                || (type.contains("HOUSEF")) || (type.contains("BUILDF"))

                || (type.contains("TOWNHF")) || (type.contains("TOWNHF"))

                || (type.contains("TOWNHT")) || (type.contains("TOWNHT")) && (station.contains("46")) && (units.contains("PE846"))){ 


This is horrible !!! Write a method boolean doesContains(String xxx)
and compare it with an array
And why checking "APPT" and "HOUSET" and "BUILDT" more than once ? You are not sure that type.contains() will work the first time ? :)
Was This Post Helpful? 2
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: Project looping problem

Posted 23 November 2011 - 10:21 PM

Holy mother of spaghetti code, you need to move to a more OO-oriented packet scheme that will be easier to understand/maintain.

This post has been edited by Ghlavac: 23 November 2011 - 10:21 PM

Was This Post Helpful? 1
  • +
  • -

#4 pgfdbug  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-April 08

Re: Project looping problem

Posted 07 December 2011 - 07:54 PM

Thats the problem Im not sure where the loop is. I know one packet has to be read through 2 different character sets (UTF16-BE & UTF-16LE) to retrieve all the info I need. I am not sure why the booleans are being returned 3 times? I have been trying to find a way so once a boolean is returned true the program goes back to listening.

The multiple HOUSET and BUILDT were just typos I forgot to remove. At one point it had to look at "Type" and "Type2".

This post has been edited by pgfdbug: 07 December 2011 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1