Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 接收通过网络发送的PDU,并在显示给用户之前对其进行过滤_Java_Multithreading_For Loop_While Loop - Fatal编程技术网

Java 接收通过网络发送的PDU,并在显示给用户之前对其进行过滤

Java 接收通过网络发送的PDU,并在显示给用户之前对其进行过滤,java,multithreading,for-loop,while-loop,Java,Multithreading,For Loop,While Loop,我有以下Java方法,我正试图使用它来过滤通过网络接收的PDU消息,并仅显示符合用户过滤条件的PDU: public static void displayFilteredPdu(){ try{ EspduReceiver.socket = new MulticastSocket(EspduSender.PORT); EspduReceiver.address = InetAddress.getByName(EspduSender.DEFAULT_MULT

我有以下Java方法,我正试图使用它来过滤通过网络接收的PDU消息,并仅显示符合用户过滤条件的PDU:

public static void displayFilteredPdu(){
    try{
        EspduReceiver.socket = new MulticastSocket(EspduSender.PORT);
        EspduReceiver.address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
        EspduReceiver.socket.joinGroup(EspduReceiver.address); 



        while(EspduReceiver.stopCapture == false){
            byte buffer[] = new byte[EspduReceiver.MAX_PDU_SIZE];
            EspduReceiver.packet = new DatagramPacket(buffer, buffer.length);

            EspduReceiver.socket.receive(EspduReceiver.packet);

            Pdu pdu = EspduReceiver.pduFactory.createPdu(EspduReceiver.packet.getData());

            if(pdu != null){
                System.out.print("Got PDU of type: " + pdu.getClass().getName());
                if(pdu instanceof EntityStatePdu){
                    EntityID eid = ((EntityStatePdu)pdu).getEntityID(); 
                    Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation(); 
                    System.out.println(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                    System.out.println("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] ");
                    /*Add PDU to ArrayList of PDUs */
                    EspduReceiver.espdu.add(pdu);
    /*              System.out.println(" PDU added to arrayList. "); 
                    System.out.println(espdu); /*This is printing out the actual DIS messages (i.e. edu.nps.moves.dis.EntityState...),
                    maybe try adding the 'eid.getSite()', etc to an ArrayList instead. Use Associative arrays/ map/ hashmap */

                //  if(eid.getSite() != 0){
                        Filter.sitesToBeFiltered.add(eid.getSite());
                        System.out.println("Entity Site added to ArrayList. ");
                //  } else if(eid.getApplication() != 0){
                    Filter.applicationsToBeFiltered.add(eid.getApplication());
                    System.out.println("Entity Application added to ArrayList. ");
                    Filter.IDsToBeFiltered.add(eid.getEntity());
                    System.out.println("Entity ID added to ArrayList");
                    Filter.positionsToBeFilteredX.add(position.getX());
                    System.out.println("Entity X position added to ArrayList. ");
                    Filter.positionsToBeFilteredY.add(position.getY());
                    System.out.println("Entity Y position added to ArrayList. ");
                    Filter.positionsToBeFilteredZ.add(position.getZ());
                    System.out.println("Entity Z position added to ArrayList. ");

                    int i;
                    getFilterConditions();
                    for(i = 0; i < sitesToBeFiltered.size(); i++){
                        if(sitesToBeFiltered.get(i) == filter1Value){
                            //public double xPos = new Double();
                            Gui.displayFilteredOutput.append("\n");
                            Gui.displayFilteredOutput.append("EID: [" + sitesToBeFiltered.get(i) + ", " + applicationsToBeFiltered.get(i) + ", " + IDsToBeFiltered.get(i) + "]. ");
                            double filteredX = positionsToBeFilteredX.get(i);
                            double filteredY = positionsToBeFilteredY.get(i);
                            double filteredZ = positionsToBeFilteredZ.get(i);
                            //Vector3Double filteredEntityPosition = 
                            Gui.displayFilteredOutput.append("\n Location in DIS coordinates: [" + filteredX + ":" + filteredY + ":" + filteredZ + "]. ");
                        }
                    }
                }
            }
        }   
    }
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        System.out.println("Error in displayFilteredPdu() method. ");
        /*09/04/2014 @ 17:100
         * If this exception gets called, presumably it either means that pdu is not an instance of EntityStatePdu, or
         * that pdu does not actually hold a packet.  */
    }

}
publicstaticvoiddisplayFilteredPDU(){
试一试{
espdureciver.socket=新的多播套接字(EspduSender.PORT);
EspduReceiver.address=InetAddress.getByName(EspduSender.DEFAULT\u MULTICAST\u GROUP);
espdureciver.socket.joinGroup(espdureciver.address);
while(EspduReceiver.stopCapture==false){
字节缓冲区[]=新字节[espdureciver.MAX_PDU_SIZE];
espdureciver.packet=新数据包(buffer,buffer.length);
espdureciver.socket.receive(espdureciver.packet);
Pdu Pdu=espdureciver.pduFactory.createPdu(espdureciver.packet.getData());
如果(pdu!=null){
System.out.print(“获取类型为:”+PDU.getClass().getName())的PDU);
if(实体状态pdu的pdu实例){
EntityID eid=((EntityStatePdu)pdu).getEntityID();
Vector3Double position=((EntityStatePdu)pdu.getEntityLocation();
System.out.println(“EID:[“+EID.getSite()+”,“+EID.getApplication()+”,“+EID.getEntity()+”);
System.out.println(“DIS坐标中的位置:[“+position.getX()+”,“+position.getY()+”,“+position.getZ()+”]);
/*将PDU添加到PDU的ArrayList*/
EspduReceiver.espdu.add(pdu);
/*System.out.println(“添加到arrayList的PDU”);
System.out.println(espdu);/*这是打印实际DIS消息(即edu.nps.moves.DIS.EntityState…),
可以尝试将“eid.getSite()”等添加到ArrayList中。请使用关联数组/map/hashmap*/
//如果(eid.getSite()!=0){
Filter.sitesToBeFiltered.add(eid.getSite());
System.out.println(“添加到ArrayList的实体站点”);
//}else如果(eid.getApplication()!=0){
Filter.applicationstobefilted.add(eid.getApplication());
System.out.println(“添加到ArrayList的实体应用程序”);
Filter.idstobefilted.add(eid.getEntity());
System.out.println(“添加到ArrayList的实体ID”);
Filter.positionsToBeFilteredX.add(position.getX());
System.out.println(“添加到ArrayList的实体X位置”);
Filter.positionsToBeFilteredY.add(position.getY());
System.out.println(“添加到ArrayList的实体Y位置”);
Filter.positionsToBeFilteredZ.add(position.getZ());
System.out.println(“添加到ArrayList的实体Z位置”);
int i;
getFilterConditions();
对于(i=0;i
此方法当前位于名为Filter.java的类中,当用户单击GUI上的“过滤器”按钮时调用,并将继续调用,直到变量
stopCapture
的值更改为false(当用户单击GUI上的“停止”按钮时会发生这种情况)

然而,它的行为并不像我当时调用时所希望的那样:目前,当用户单击“Filter”按钮时,所有的
System.out.println
语句都显示在控制台中,因此很明显,
while
循环正在被输入,但我没有看到GUI中显示的任何东西,我认为这会表明当
循环未被输入时,的
for
循环在
的末尾…并且GUI停止响应

据我所知,GUI停止响应的原因可能是因为我当前使用的是单线程程序,因此将此
displayFilteredPdu()
方法移动到另一个线程可能会阻止GUI崩溃,然后我可以使用该方法处理其他问题

我尝试将该方法移动到另一个类(称为FilteredPdu.java),该类扩展了Filter,并将按钮
ActionListener()
更改为从新类调用该方法,即
FilteredPdu.displayFilteredPdu();
,但GUI崩溃仍然存在同样的问题


知道这是为什么吗?

我认为在另一个类中移动它仍然会导致程序陷入无限循环。 试着去上课
public class YourThreadClass  extends Thread{
    String fake_input_in_thread = null;

    public YourThreadClass(String fake_input){
        //constructor
        //save the input / whatever you need to pass in from the other class here, and use it later
        fake_input_in_thread = fake_input
    }

    public void run() {
        //your filter code
        try{
            //now you can use the value pass-in from you main class. This should display "testing123" in console.
            System.out.println(fake_input_in_thread);

            EspduReceiver.socket = new MulticastSocket(EspduSender.PORT);
            EspduReceiver.address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
            EspduReceiver.socket.joinGroup(EspduReceiver.address); 
            .............................
    }

    //put the below method into the thread class
    public void endOfFilter(){
        EspduReceiver.stopCapture = false;
    }
}
//initialize 
YourThreadClass thread = null;

public static void displayFilteredPdu(){
    if(thread ==null){
        //This string is to demostrate how you'd pass variables into your custom class, does not have any actual use
        String str = "testing123";

        thread = new YourThreadClass(str);
        thread.start();
    }
}

//in main code
public static void stopFilteredPdu(){
    if(thread != null){
        thread.endOfFilter();
        thread = null;
    }
}