Tutorial #1: Network Communication
Tutorial #2: Device Driver
Tutorial #3: Intelligent
Agent
import weblab.net.*;
public class PUser
extends Frame
implements MouseListener
{
TextArea text; // content of text window
Button submit_button;
Publisher publisher;
// **********************************************************
// Constructor
// **********************************************************
public PUser(String name)
{
super(name);
// establish a publisher for data
publisher = new Publisher(name, WhiteBoardConf.SERVER_NAME);
publisher.addSubject("warning");
// set up GUI objects
setLayout(new BorderLayout());
add("Center", text = new TextArea());
add("South", submit_button = new Button("Publish"));
submit_button.addMouseListener(this); // this object
listens to mouse
setSize(300,300);
show();
}
// **********************************************************
// MouseListener Interface implementation
// **********************************************************
public void mouseClicked(MouseEvent me) {
publisher.setValue("warning", text.getText());
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
//
// launch this object
//
public static void main(String args[]) {
new PUser("PublisherUser");
}
}
Create a Subscriber object, give it a unique name such a host name, and tell it where is the
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;
import weblab.net.*;
public class SUser
extends Frame
implements SubscriberUser
{
Subscriber subscriber;
TextArea text; // content of text window
InetAddress local_host; // local host network address
String SUBJECT= "warning"; // subject name to subscribe
// **********************************************************
// CONSTRUCTOR
// **********************************************************
public SUser(String name) {
super(name);
// get local host address
try {
local_host = InetAddress.getLocalHost();//
use class static method to get a ref
}
catch (Exception e) {
System.out.println("can't find local
network address.");
System.exit(1);
}
// establish a subscriber helper object
subscriber = new Subscriber(local_host.getHostName(),
WhiteBoardConf.SERVER_NAME,
this);
Object value = subscriber.subscribe(SUBJECT);
// set up GUI objects
setLayout(new BorderLayout());
add("Center", text = new TextArea());
// get latest value ASAP
if (value != null)
update(SUBJECT, value);
setSize(300,300);
show();
}
// **********************************************************
// SubscriberUser Interface implementation
// **********************************************************
public void update(String name, Object s) {
text.setText((String)s);
}
//
// launch this object
//
public static void main(String args[]) {
new SUser("SubscriberUser");
}
}