简体   繁体   中英

Java- how do I save data that is being read into my program by a public static void method into a String?

I am writing a Java program that will listen for PDUs that are being sent across a network, and present information about those PDUs (such as entity state & position within the simulation) to the user.

I currently have one class which is being used to listen for the PDUs being sent over the network (EspduReceiver.java), and another class that I am using to create the GUI that the user will use to interact with the program (Gui.java).

The EspduReceiver.java class currently works as intended- when I run it, it listens for any PDUs that are being sent over the network, and displays information about each individual PDU it receives in the console. The information that it displays includes the entity ID, and its position within the simulation.

However, this method literally just listens for a PDU message, and prints the relevant information about it to the console when it 'hears' one- so the information about the Entity State PDU that is being printed in the console, is not actually stored anywhere in the program at present.

Now, what I would like to do with my Gui.java class, is to create a user interface that will display what I am seeing being printed in the console to the user. I have written the foundation of a class that will do this, but I am having trouble retrieving the data that is being read into my program from the EspduReceiver.java class...

The method that I am using to retrieve the PDU information, and display it in the console currently looks like this:

public static void receivePdu(){

    try{
        /*Specify the socket to receive the data */
        socket = new MulticastSocket(EspduSender.PORT);
        address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
        socket.joinGroup(address); 

        /*Loop infinitely, receiving datagrams */
        while(true){
            byte buffer[] = new byte[MAX_PDU_SIZE];
            packet = new DatagramPacket(buffer, buffer.length);

            socket.receive(packet);

            Pdu pdu = pduFactory.createPdu(packet.getData()); /*    Commented on 15/04/2014 @ 09:15 */

            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.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                    System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
                } else if(!(pdu instanceof EntityStatePdu)){
                    System.out.println("There are no PDUs currently being received.");
                }
                System.out.println();
            }
        } /*end while */
    } /*end try */
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        System.out.println("This is where the error is being generated");
        /*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.  */
    }
}

Then, in my Gui.java class, I have a public void initGui(){...} method, which I am trying to use to call the receivePdu() method from EspduReceiver.java. I have done this by declaring a new instance of my EspduReceiver class, and creating a new String variable to hold the data generated by the receivePdu() method:

EspduReceiver receiver = new EspduReceiver();
String data = receiver.receivePdu();

However, I get an error on the String data = receiver.receivePdu(); line that says: "Type mismatch: cannot convert from void to String", and suggests that I change return type of receivePdu() to String...

I understand the reason that I get the error- because my receivePdu() method has a return type of "void", and here I am trying to state that it is String- I had originally tried to write the receivePdu method with a return type of String, but kept getting errors with that, and couldn't fix it other than by changing it to void.

Is there a way that I can access the data retrieved by the receivePdu() method, so that I can display it to the user? How would I do this? Could I possibly save the data that it retrieves to another variable, and use that to display it to the user?

You can't usefully call the receivePdu() method from your GUI, because receivePdu() never returns - it loops forever - and doesn't return anything.

Creating a new instance of EspduReceiver won't help, since the receivePdu method is static ie shared by all instances.

You could get receivePdu() to store data in a variable at the top level of EspduReceiver, visible to the GUI, then notify the GUI that it should collect the data and display it. Or you could push the data to the GUI from within receivePdu , though that tangles all your code up somewhat.

Either way, you will need to notify the GUI from the "Event Dispatching Thread" - see the tutorial here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM