gpt4 book ai didi

java - 接收通过网络发送的 PDU,并在显示给用户之前按接收到的方式过滤它们

转载 作者:行者123 更新时间:2023-11-30 11:18:53 34 4
gpt4 key购买 nike

我有以下 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. */
}

}

这个方法目前在一个名为 Filter.java 的类中,当用户单击 GUI 上的“过滤器”按钮时调用,并将继续调用直到变量 stopCapture 更改为 false(当用户单击 GUI 上的“停止”按钮时会发生这种情况)。

但是,调用时它的表现并不像我希望的那样:目前,当用户单击“过滤器”按钮时,将显示所有 System.out.println 语句在控制台中,显然正在输入 while 循环,但我没有在 GUI 中看到任何显示,我认为这表明 for 循环位于未进入 while 循环的结尾...而且 GUI 停止响应...

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

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

知道这是为什么吗?

最佳答案

我认为将它移到另一个类中仍然会导致您的程序陷入无限循环。尝试创建一个扩展线程类,将显示过滤器 pdu 方法设置为:

这是新的线程类:

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

当您的 GUI 需要触发该方法时,这就是您的主类的样子

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

关于java - 接收通过网络发送的 PDU,并在显示给用户之前按接收到的方式过滤它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23562293/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com