Problems with network connection

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Problems with network connection

Postby Philipp15b » Fri May 18, 2012 3:47 pm

Hello,
I decided to write my first network application with Java and LeJOS: I tried to implement kind of packets that are transmitted by a "NetworkManager" in a seperate thread. Essentially, those packets just write their id as a byte in the input stream and then their data (for example a String). It worked at the beginning, but now the NXT doesnt receive any data... :o

Main code (on PC):
Code: Select all
            DisplayMessagePacket packet = new DisplayMessagePacket();
            packet.message = "TEST";
            while (true) {
               network.addToSendQueue(packet);
               try {
                  Thread.sleep(20);
               } catch (InterruptedException e) {
               }
            }


NetworkManager (on PC and NXT, shortened):
Code: Select all
public class NetworkManager {

   private class NetworkReaderThread extends Thread {
      public NetworkReaderThread() {
         super("NetworkManager Reader Thread");
      }

      @Override
      public void run() {
         while (isRunning()) {
            try {
               try {
                  System.out.println("reading!");
                  readPacket();
               } catch (IOException e) {
                  if (in == null)
                     return;
                  e.printStackTrace();
                  shutdown();
                  return;
               }

               Thread.sleep(readFrequency);
            } catch (InterruptedException e) {
            }
         }
      }
   }

   private class NetworkWriterThread extends Thread {
      public NetworkWriterThread() {
         super("NetworkManager Writer Thread");
      }

      @Override
      public void run() {
         while (isRunning()) {
            try {
               while (writePacket())
                  ;
            } catch (IOException e1) {
               e1.printStackTrace();
               shutdown();
               return;
            }

            try {
               Thread.sleep(sendFrequency);
            } catch (InterruptedException e) {
            }
         }
      }
   }

   public short readFrequency = 500;
   public short sendFrequency = 500;
   private NetHandler handler;
   private DataInputStream in;
   private DataOutputStream out;
   private List<Packet> packetSendQueue = new LinkedList<Packet>();
   private NetworkReaderThread reader;
   private NetworkWriterThread writer;

   public NetworkManager(DataInputStream in, DataOutputStream out,
         NetHandler handler) {
      this.in = in;
      this.out = out;
      this.handler = handler;
      handler.setNetworkManager(this);

      writer = new NetworkWriterThread();
      reader = new NetworkReaderThread();
      reader.start();
      writer.start();
   }

   /**
    * Adds a packet to a queue of packets to be sent.
    *
    * @param packet
    */
   public void addToSendQueue(Packet packet) {
      synchronized (packetSendQueue) {
         packetSendQueue.add(packet);
      }
   }

   /**
    * Reads a single packet from the {@link DataInputStream} of this object.
    * This method is blocking; it waits until a full packet has been read.
    *
    * @throws IOException
    */
   private void readPacket() throws IOException {
      Packet packet = Packet.readPacket(in);
      if (packet != null) {
         packet.processPacket(handler);
      } else {
         shutdown();
      }
   }

   /**
    * Writes a single packet to the {@link DataOutputStream} of this object.
    *
    * @return If there are any more packets left in the send queue.
    * @throws IOException
    */
   private boolean writePacket() throws IOException {
      synchronized (packetSendQueue) {
         if (packetSendQueue.isEmpty())
            return false;

         Packet packet = packetSendQueue.remove(0);

         Packet.writePacket(packet, out);

         if (packetSendQueue.isEmpty())
            return false;
         else
            return true;
      }
   }

}


While it works fine on the PC (it receives data sent from the NXT, it also shows "reading!"), the NXT only shows "reading!" one time and then receives nothing... Can somebody help?
Philipp15b
New User
 
Posts: 6
Joined: Mon Nov 28, 2011 6:12 pm

Re: Problems with network connection

Postby kirkpthompson » Fri May 18, 2012 5:06 pm

Leg Godt!
User avatar
kirkpthompson
leJOS Team Member
 
Posts: 304
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: Problems with network connection

Postby Philipp15b » Fri May 18, 2012 5:51 pm

So I have to use Bluetooth to use my program, right? Because still with Bluetooth it does not work...
Philipp15b
New User
 
Posts: 6
Joined: Mon Nov 28, 2011 6:12 pm

Re: Problems with network connection

Postby gloomyandy » Fri May 18, 2012 9:50 pm

We probably need to see the actual I/O code and you need to provide more details of what exactly you are reading and writing when things go wrong. To answer your other question, yes you need to use Bluetooth if yo want to have a read and write operation happening at the same time.

Have you tested your packet reading and writing code without the additional complication of the threads and queues. If this is new code it is best to start of simple and build things up from working code...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4239
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Problems with network connection

Postby Xfel » Sun May 20, 2012 1:49 pm

I ran into a similar problem a while ago. There seems to be a race condition in the system that lock IO operations if they occur in a specific timed pattern. After I added some debug printouts to trace the problem, it did work. I replaced them with RConsole calls, it worked while running the console via USB. Then I ran it without the console code, and it didn't work again...
It seems to me that you have the same problem here. I wasn't able to track it down myself, as all changes fo better debugging made the error simply disappear. Maybe we get another chance now.
Xfel
New User
 
Posts: 11
Joined: Mon Jan 30, 2012 3:29 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff