Application Development using Traditional Programming

Traditional Programming means writing codes and calling API directly. It offers the highest degree of flexibility for custom application development.

Tutorial #1: Network Communication
Tutorial #2: Device Driver
Tutorial #3: Intelligent Agent
 


Tutorial #1: Network Communication This tutorial describes how to use the World Wide Lab TM  communication package, weblab.net.  Example codes are in test_net directory. In general, the process involves:

Write the code for Publisher process

To implement a process that performs the Publisher role, we need to : The following is the source code for PUser.java which uses a TextArea to collect input.
When the "submit" botton is pressed, it publishes the data to WhiteBoard.


import java.io.*;
import java.awt.*;
import java.awt.event.*;

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");
  }

}
 
 


Write the code for Subscriber process

To implement a process that performs the Subscriber role, we need to :

Create a Subscriber object, give it a unique name such a host name, and tell it where is the

The following is the source code for SUser.java which uses a TextArea to display the latest
message on the Subject from its Publisher.

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");
  }

}
 
 


Tutorial #2: Device Driver under construction
 


Tutorial #3: Intelligent Agent under construction
 



[ Home | Index ]