程序没有捕获

时间:2011-09-10 01:56:11

标签: java jpcap

我的程序在我的工作站中作为警报系统运行。我是Java的全新人物,随时随地学习。无论如何,程序编译并运行但不捕获任何数据包。我希望你们都能给我一些关于我所缺少的信息。提前谢谢。

监听

package prealert;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;

public class Listener {

    private NetworkInterface[] devices;
    private NetworkInterface deviceName;
    private Reciever Reciever;
    private JpcapCaptor jpcap;
    public static Logger log;


    public Listener() {
        PropertyConfigurator.configure("log4j.properties");
        log = Logger.getRootLogger();
        log.debug("Log4j has been initialized");
        devices = JpcapCaptor.getDeviceList();
        for (int i = 0; i < devices.length; i++) {
            log.info(devices[i].description);
        }
        deviceName = devices[1];
        Reciever = new Reciever();
        try {
            jpcap = JpcapCaptor.openDevice(deviceName, 2000, true, 100);
        } catch (Exception e) {
            log.error("Error with JPcapCreation", e);
        }
        Reciever.jpcap = jpcap;
        Reciever.start();
        new SetBoard(SetBoard.DEFAULT).start();
    }

    public static void main(String args[]) {
        try {
            Listener listener = new Listener();
        } catch (Exception e) {
            log.error("ERROR IN THE MAIN METHOD OF LISTENER!", e);
        }
    }
}

接收机

package prealert;

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

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

public class Reciever extends Thread implements PacketReceiver {

    public JpcapCaptor jpcap;
    private int sign = 0;
    private long lastHeaderTime;

    private enum PacketPart {ID, DATA, OTHER};

    public static final int MEDIC = 1;
    public static final int AMBO = 2;
    public static final int ENGINE = 3;
    public static final int BoxAlarm = 4;
    public static final int StreetAlarm = 5;
    public static final int FIRSTDUE = 6;
    public static final int HAZMAT = 7;

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

    public void receivePacket(Packet packet) {

        if ((((IPPacket) packet).dst_ip.toString().equals("172.20.XX.XXX")) || (((IPPacket) packet).src_ip.toString().equals("/172.20.XX.XXX"))) {

            String data = new String(packet.data, Charset.forName("US-ASCII"));

            PacketPart part = whatPart(data);

            if (part == PacketPart.ID) {

                prealert.Listener.log.info(part + ": " + data);

                String id = getId(data);
                lastHeaderTime = packet.sec;

                if (calls.containsKey(id)) {
                    prealert.Listener.log.info("The ID: [" + id + "] was already in the calls, adding a new part");
                    recentPrintOut = calls.get(id);
                    recentPrintOut.addPart(data);
                } else {
                    //prealert.Listener.log.info("The ID: [" + id + "] is new, making a new printout");
                    recentPrintOut = new PrintOut(data);
                    calls.put(id, recentPrintOut);
                }
            }

            if ((part == PacketPart.DATA) && ((packet.sec - lastHeaderTime) < ((long) 3))) {

                prealert.Listener.log.info(part + ": " + data);

                recentPrintOut.addPart(data);

                if(data.contains("PrimeUnit:")){
                    /*
                    int tempd=data.indexOf("PrimeUnit:");
                    data=data.substring(0,tempd)+data.substring(tempd+13);
                    */
                    return;
                }


                if (recentPrintOut.alertFirstDue() == true) {
                    new AudioPlayer("house.wav").start();
                    sign = FIRSTDUE;
                }
                                if (recentPrintOut.alertboxalarm() == true) {
                    new AudioPlayer("enginebox.wav").start();
                    sign = BoxAlarm;
                }
                                if (recentPrintOut.alertstreet() == true) {
                    new AudioPlayer("enginestreet.wav").start();
                    sign = StreetAlarm;
                }
                if (recentPrintOut.alertFirstDue() != true) {
                    if (recentPrintOut.alertPE830() == true) {
                        new AudioPlayer("engine.wav").start();
                        sign = ENGINE;
                    }
                    if (recentPrintOut.alertMD830() == true) {
                        new AudioPlayer("medics.wav").start();
                        sign = MEDIC;
                    }
                    if (recentPrintOut.alertA830() == true) {
                        new AudioPlayer("ambo.wav").start();
                        sign = AMBO;
                    }
                    }
                if (sign != 0) {
                    int tempSign = sign;
                    try {
                        new SetBoard(recentPrintOut, tempSign).start();
                    } catch (Exception e) {
                        prealert.Listener.log.error("Exception while setboard: ", e);
                    }
                                        sign = 0;
                }
            }
        }
    }

    private static PacketPart whatPart(String data) {

        if (data.contains("Call #F")) {
            return PacketPart.ID;
        }
        if (data.contains("Darea:") || (data.contains("PrimeUnit:")) || (data.contains("Location:")) || (data.contains("Station:"))
                || (data.contains("CREATE")) || (data.contains("GeoLong:")) || (data.contains("ALIGEO")) || (data.contains("PROQA"))
                || (data.contains("Statement:")) || (data.contains("ENTRY")) || (data.contains("RSPMOD")) || (data.contains("Classify:"))
                || (data.contains("Dispatch")) || (data.contains(". 2.")) || (data.contains(". 6.")) || (data.contains("TIBNUM")) || (data.contains("Text:"))
                || (data.contains("DISP")) || (data.contains("NOMORE")) || (data.contains("INFO")) || (data.contains("PRIU")) || (data.contains("Name:"))
                || (data.contains("830")) || (data.contains("Sig4:")) || (data.contains("TypeDesc:")) || (data.contains("E911"))) {
            return PacketPart.DATA;
        }

        return PacketPart.OTHER;
    }

    public static String getId(String data) {
        int index = data.indexOf("Call #");
        return data.substring(index + 7, index + 16);
    }

0 个答案:

没有答案