title: Java IPC with Mappedbus - reset file on EOF
body: <p>I am using Mappedbus for IPC between two JVMs (oracle jdk 1.8, linux arm);
  I would like to see the IPC work "forever", but as the memory mapped (or shared memory) file is finite, before or later the writer gets an EOFException. </p>
  
  <p>So I try to reset the writer (and the reader), but after closing and reopening the writer, the reader is not able to read the new records.</p>
  
  <p>So, the question is, <strong>how to make the following test case work i.e. go on forever with both writer and reader working?</strong></p>
  
  <pre><code> // intentionally low, for this test case
   final long FILE_SIZE = 2000L;
  
   private final String fileName = "/dev/shm/ipc-message";
  
   @Test
   public void hello() throws IOException {
  
      MappedBusReader reader;
  
      MappedBusWriter writer;
      writer = new MappedBusWriter(fileName, FILE_SIZE, 128, false);
  
      writer.open();  
  
      reader = new MappedBusReader(fileName, FILE_SIZE, 128);
  
      reader.open();        
  
      int writeCounter = 0;
      int readCounter = 0;
  
      while (true) {
  
          IpcMessage message = new IpcMessage();
  
          message.source = 1; 
  
          message.value = 1;
          message.destination = 2;
  
          message.body = new byte [32];
  
          try {
  
              writer.write(message);       
  
          } catch (EOFException ex) {
              System.out.println("write EXCEPTION");
  
              writer.close();
  
              writer = new MappedBusWriter(fileName, FILE_SIZE, 128, false);
  
              writer.open();  
          }
  
          System.out.println("write: " + writeCounter++);
  
          try {
              if (reader.next()) {
  
                      boolean recovered = reader.hasRecovered();
                      int type = reader.readType();
  
                      System.out.println("read: " + readCounter++);
  
                      reader.readMessage(message);   
  
              }
          }  catch (EOFException ex) {
  
              System.out.println("read EXCEPTION");
  
              reader.close();
  
              reader = new MappedBusReader(fileName, FILE_SIZE, 128);
  
              reader.open(); 
  
          }
  
      }
  
  
  
   }
  </code></pre>
  
  <p>where IpcMessage is:</p>
  
  <pre><code>public class IpcMessage implements MappedBusMessage, Serializable {
  
      public static final int TYPE = 0;
  
      public int messageType;
      public int value;
  
      public int source;
      public int destination;
  
  
      @Override
      public void write(MemoryMappedFile mem, long pos) {
  
          mem.putInt(pos, messageType); 
          mem.putInt(pos + 4, value); 
  
          mem.putInt(pos + 8, source);
          mem.putInt(pos + 12, destination);
      }
  
      @Override
      public void read(MemoryMappedFile mem, long pos) {
  
          messageType = mem.getInt(pos);
          value = mem.getInt(pos + 4);
  
          source = mem.getInt(pos + 8);
          destination = mem.getInt(pos + 12); 
  
      }
  
      @Override
      public int type() {
          return TYPE;
      }
  
  
  
  }
  </code></pre>
  
  <p>The output I get from this test case is:</p>
  
  <pre><code>write: 0
  read: 0
  write: 1
  read: 1
  write: 2
  read: 2
  ...
  write: 8
  read: 8
  write: 9
  read: 9
  write: 10
  read: 10
  write: 11
  read: 11
  write: 12
  read: 12
  write: 13
  read: 13
  write EXCEPTION
  write: 14
  write: 15
  write: 16
  write: 17
  write: 18
  write: 19
  write: 20
  write: 21
  write: 22
  write: 23
  </code></pre>