Developing Peer-to-Peer Applications for the Internet
by Louis Thomas, Sean Suchter, Adam Rifkin


Listing One
Class SharedVar

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class SharedInt extends SharedVar {
  int _data;
  SharedInt() {
    super();
    _data = 0;
  }
  public SharedInt(SharedContainer parent) {
    super(parent);
    _data=0;
  }
public synchronized void readData(DataInputStream dis) throws IOException {
    _data=dis.readInt();
  }
  public synchronized void writeData(DataOutputStream dos) throws IOException {
    dos.writeInt(_data);
  public synchronized void set(int data) {
    if (!locked()) throw new FieldNotLockedException("SharedInt not locked.");
    if (_data != data) {
      _data=data;
      setModified();
    }
  public synchronized int get() {
    return _data;
  }
}


Listing Two
Locking fields
public class MyProgram {
  private MyNDO ndo;
  void doSomething() throws FieldDeletedException {
    ndo.startLock();
    ndo.field(0).lock();
    ndo.completeLock(); //This call will throw the FieldDeletedException if 
                        // field 0 of the NDO no longer exists
    ((SharedInt)ndo.field(0)).set(10);
    //SharedInt.set() calls setModified()
    ndo.field(0).unlock();
  }
} 


Listing Three
boolean bRetry;
do {
  bRetry=false;

  // Lock what we need
  int nLine=0;
  SharedString sszLine=null;
  while (true) {
    nLine=sscMyCursor.getLine();
    sszLine=m_seNDO.getLineObject(nLine);
    m_seNDO.startLock();
    scCursorList.lockAll();
    sszLine.lock();
    try {
      m_seNDO.completeLock();
      break;
    } catch (FieldDeletedException e) {
    // That's OK, try again.
    }
  }
  // make sure we get the line that the cursor is ACTUALLY on.
    bRetry=true;
  } else {
    // Do whatever you want to with the line and the cursors
  }
  scCursorList.unlockAll();
  sszLine.unlock();
} while (true==bRetry);


Listing Four
//-----------------------------------------------
// Clean up ASAP - inform parent that we've been dismissed
private void quit() {
    hide();
    dispose();
    getParent().postEvent(
        new Event(this, Event.ACTION_EVENT, null));
}

Listing Five
// update as soon as possible
Graphics g=getGraphics();
if (null==g) {
  repaint();
} else {
  update(g);
}


Listing Six
//------------------------------------------------------------------
// (override) When this is called, we can finally create the buffer.
public void addNotify() {
  super.addNotify();
  if (null==m_imOffscreen) {
    createOffScreenBuffer();
  }
}
//------------------------------------------------------------------
// We can't create an image until peer has been created
private void createOffScreenBuffer() {
  Assert.isNull(m_imOffscreen);
  // Create the image
  m_imOffscreen=createImage(m_dCanv.width, m_dCanv.height);
  if (m_imOffscreen==null) {
    throw new RuntimeException(
      "TextGrid can't create offscreen image.");
  }
  // ... initialize, etc.
}

1


