import drcl.comp.*; /** * Template for writing a component. * This component responds with data that was received last time, * emulating the behavior of "unit-delay". */ public class ComponentTemplate extends drcl.comp.Component implements drcl.comp.ActiveComponent { // The fields here are just for demonstration of usages // of the methods present in this template. int x; Object lock = new Object(); /** * Constructor. */ public ComponentTemplate() { super(); } /** * Constructor. */ public ComponentTemplate(String id_) { super(id_); } /** * Invoked when data_ arrives this component at the inPort_ port. */ protected void process(Object data_, drcl.comp.Port inPort_) { // Put codes here for handling the incoming data. synchronized (lock) { x++; notify(lock); // Notifies the waiting thread. wait(lock); // Delay sending data until someone notifies. inPort_.doSending(data_); } } /** * Resets this component to the initial state for use anew. * Must call super.reset() in the beginning. */ public void reset() { super.reset(); // Let super class reset its fields. x = 0; // Reset the fields defined in this class. } /** * Copies the content from the source_ to this component. * Must call super.duplicate() in the beginning. */ public void duplicate(Object source_) { if (!(source_ instanceof ComponentTemplate)) return; super.duplicate(source_); // Let super class copy its fields. ComponentTemplate that_ = (ComponentTemplate)source_; x = that_.x; // Duplicate the fields defined in this class. // Suppose we don't have to copy "lock". } /** * Invoked when the component is run()ed. */ public void start() { debug(this + " is starting!"); } /** * Script interface which reveals the internal states of the component. * It is for debugging and demonstration purpose. */ public String info() { return "x = " + x + "\n"; } /** * Script interface which increments the counter by +1. */ public void increment() { x++; } }