Modules in Webflow use the Module interface.The Module interface consists of four methods:
The webflow module must implement all three methods. An adder module is used as an example below. It consists of three ports (two input and one output). The following are the steps in making an adder module.
import webflow.*;
MetaModule mm = new MetaModule("mm");
All ports are given a certain portID by the ConnectionManager. This portID is returned by the port using the getPortID method. All the port ID's are stored in the MetaModule object as shown below:
port1 = new IntPort();
mm.putPortID(port1.getPortID());
The last step in the initialize method is to return the MetaModule object:
return(mm);
num1=port1.recieve().intValue(); // read value from first port num2=port1.recieve().intValue(); //read value from second port
num3=num1 + num2; //add the two numbers
port2.send(new Integer(num3));//dispatch the value through the output port
port1.destroy();
All ports are derived from Port class . The main methods in the Port class are:
A simple integer port has been used for the adder class . The basic integer port sends and recieves an Integer object since the send and receive functions of the Port class deal with the Object class . When the IntPort object is created, the constructor of the Port class is called which registers the port with the ConnectionManager and gives a unique portID to the port. This id is retrieved using the getPortID() method in the initialize method of the module . This portID is what is stored in the MetaModule and it is what is used for making connections between modules.
The destroy method is used when the module terminates and is used to deregister the port.
The send and recieve methods are implemented in the following way in the IntPort class 1.
The send method as defined by the Port class takes an Object as its parameter:
public void send(Object num);
In the IntPort class this Object is an Integer object which contains the integer data to be transmitted through the port. To get the Socket of the port the getSocket method is called . A new output stream is created through which the data is sent:
os= new DataOutputStream(getSocket().getOutputStream());
os.writeInt(num.intValue());
It returns an object of class Object:
public Object receive();
In the IntPort class this Object is an Integer object which contains integer data recieved . As usual the getSocket method retrieves the Socket and a new input stream is created through which data is read:
is= new DataInputStream(getSocket().getInputStream());
// new input stream created
data= new Integer(is.readInt());// data is an Integer object
return(data);// data object being returned