/** * A networked version of AlphaSmarts. Uses cs101.util.Wire to * communicate with something else (theoretically another * AlphaSmarts) in a typical reader/writer fashion: * read (and block until the read returns) a new object and use it. * simultaneously, write your object whenever your object changes. * This is network contract; all other contracts are as AlphaSmarts. */ public class WiredAlphaSmarts extends AlphaSmarts implements Runnable { private cs101.util.Wire wire; // The network private Thread spirit; // The animacy /** * Need to tell the AlphaSmarts what Wire to use. */ public WiredAlphaSmarts( cs101.util.Wire w ) { super(); this.wire = w; this.spirit = new Thread( this ); this.spirit.start(); } /** * The Writer. * Overrides AlphaSmarts.record to send data over the Wire, too. * Whenever a new SafeMonogram needs to be recorded, do so by * sending it over the wire as well as what AlphaSmarts does. */ public void record( SafeMonogram m ) { super.record( m ); this.wire.putObject( m ); } /** * The Reader. * Continually poll the Wire to see if a new object has come in. * When it has, record it locally (but DON'T resend it). */ public void run() { while( true ) { Object o = this.wire.getObject(); if ( o instanceof SafeMonogram ) { super.record( (SafeMonogram) o); } } } }