程序未捕获

发布于 2024-12-03 18:01:03 字数 6664 浏览 3 评论 0原文

我的站里有一个程序作为警报系统运行。我对 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);
    }

I have a program running as an alerting system in my station. I am brand new to Java and learning as I go. Anyway the program compiles and runs but does not capture any of the packets. Im hoping you all can give me a little input on what I am missing. Thanks in advance.

Listener

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);
        }
    }
}

Receiver

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文