Java applet example

The following example is a Java applet that runs in a web browser to connect to the TeamOnCall Subscription service.

Save the code to a file named TeamClientApplet.java. Compile it with the Java Development Kit and save TeamClientApplet.class to the TeamRecord or TeamOnCall server. The Subscription service port is read as a parameter from the HTML page that contains the applet. See bold below.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class TeamClientApplet extends Applet implements ActionListener, Runnable
{
    Label label_;
    TextField textField_;
    Button button_;
    TextArea textArea_;
    Label statusBar_;
    Socket sock_;
    Thread thread_;

    public void init()
    {
        setLayout(null);

        label_ = new Label("Phone: ");
        textField_ = new TextField(20);

        button_ = new Button("Subscribe"); 
        button_.addActionListener(this);

        textArea_ = new TextArea();
        textArea_.setEditable(false);

        statusBar_ = new Label("Ready");

        add(label_);
        add(textField_);
        add(button_);
        add(textArea_);
        add(statusBar_);

        Insets insets = getInsets();
        int x = insets.left + 20;
        int y = insets.top + 20;
        Dimension labelSize = label_.getPreferredSize();
        label_.setBounds(x, y, labelSize.width, labelSize.height);

        Dimension textFieldSize = textField_.getPreferredSize();
        x += labelSize.width + 10;
        textField_.setBounds(x, y, textFieldSize.width, textFieldSize.height);

        x += textFieldSize.width + 10;
        Dimension buttonSize = button_.getPreferredSize();
        button_.setBounds(x, y, buttonSize.width, textFieldSize.height);

        x = insets.left + 20;
        y += buttonSize.height + 20;
        Dimension textAreaSize = textArea_.getPreferredSize();
        textArea_.setBounds(x, y, 800, textAreaSize.height);
        
        y += textAreaSize.height + 10;
        Dimension statusBarSize = statusBar_.getPreferredSize();
        statusBar_.setBounds(x, y, 800, statusBarSize.height);
    }

    public void destroy() 
    {
        try
        {
            if (sock_ != null) 
                sock_.close();
        }
        catch (Exception e)
        {
        }  
    }

    public void start()
    {            
        thread_ = new Thread(this);
        thread_.start();
    }

    public void run()
    {
        connect();

        if (sock_ == null)
            return;

        setStatusBar("Connected");

        send("SUBSCRIBE");
        try
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(sock_.getInputStream()));
    
            for (;;)
            {
                String line;
                line = in.readLine();
                if (line.startsWith("EVENT"))
                    handleTeamRecordEvent(line);
            }
        }
        catch (Exception e)
        {   
            setStatusBar(e.getMessage());
            e.printStackTrace();
        }
    }

    private void connect()
    {
        String host = getCodeBase().getHost();
        int port = Integer.parseInt(getParameter("port"));

        if (host.equals("")) // testing in AppletViewer
            host = "localhost";

        try
        {          
            sock_ = new Socket(host, port);
        }
        catch (Exception e)
        {   
            setStatusBar(host + ":" + port + " " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent e) 
    {
        send("DELETE-PHONE");
        send("ADD-PHONE " + textField_.getText().trim());
    }

    private void handleTeamRecordEvent(String line)
    {
        StringTokenizer token = new StringTokenizer(line);

        token.nextToken(); // skip EVENT

        // 2nd token is number of secs since Jan. 1, 1970 GMT
        Date d = new Date(Long.parseLong(token.nextToken()) * 1000);

        // 3rd token is event type
        String type = token.nextToken();
        String s, local = "", remote = "", remoteName = "", file = "";
        
        // process rest of tokens
        while (token.hasMoreTokens())
        {
            s = token.nextToken();
            if (s.startsWith("local="))
                local = s.substring(6);
            else if (s.startsWith("remote="))
                remote = s.substring(7);
            else if (s.startsWith("remoteName="))
                remoteName = s.substring(11);
            else if (s.startsWith("file="))
                file = s.substring(5);
        }

        if (type.equals("CALL-DIAL"))
        {
            textArea_.setText(d + ": Outgoing dial: local: " + local + " remote: " + remote + "\n");
        }
        else if (type.equals("CALL-IN"))
        {
            textArea_.setText(d + ": Incoming call: local: " + local + " remote: " + remote + " " + remoteName + "\n");
        }
        else if (type.equals("CALL-WAITING"))
        {
            textArea_.append(d + ": Call waiting: local: " + local + " remote: " + remote + " " + remoteName + "\n");
        }
        else if (type.equals("CALL-OUT"))
        {
            textArea_.append(d + ": Outgoing call: local: " + local + " remote: " + remote + "\n");
        }
        else if (type.equals("CALL-START"))
        {
            textArea_.append(d + ": Call started: local: " + local + " remote: " + remote + " " + remoteName + "\n");
        }
        else if (type.equals("CALL-RECORD"))
        {
            textArea_.append(d + ": Recording call: local: " + local + " remote: " + remote + " " + remoteName + "\n");
        }
        else if (type.equals("CALL-END"))
        {
            textArea_.append(d + ": Call ended: local: " + local + " remote: " + remote + " " + remoteName + "\n");
        }
        else if (type.equals("CALL-FILE"))
        {
            textArea_.append(d + ": File ready: local: " + local + " remote: " + remote + " " + remoteName + " file: " + file + "\n");
        }
    }

    private void send(String msg)
    {
        if (sock_ == null)
        {
            setStatusBar("Not connected to server");
            return;
        }

        try
        {
            msg = msg + "\r\n";
            sock_.getOutputStream().write(msg.getBytes());
        }
        catch (Exception e)
        {
            setStatusBar(e.getMessage());
        }
    }

    private void setStatusBar(String msg)
    {
        statusBar_.setText(msg);
    }
}