Trevor Douglas
Faculty of Engineering
Office: ED 474
Phone: 306.585.5269
trevor.douglas@uregina.ca

HOME ENSE 350 ENSE 470 ENSE 471 ENSE 473 ENSE 475 ENSE 481 ENSE 483 ENEL 487 ENEL 489 CS 201

ENSE 483 Lab 7

Objective

The Objective of this lab is to continue the process of creating a Bluetoothe Chat Application. In this lab the students will create the chat program that will allow users to communicate text or chat messages as well as the ability to transfer files back and forth.

Background

Server providing our two Services

We need to setup a server that will allow us to offer two services. One of these services will be based on the RFCOMM protocol and the other will be OBEX. The RFCOMM is used to transfer text messages and the OBEX is used for file transfer. The following code supports these two services.

package edu.ense483.lab6p2;


import javax.swing.*;          
import java.awt.*;
import java.awt.event.*;
import javax.microedition.io.*;

import java.io.*;

import javax.bluetooth.*;
import javax.obex.*;


public class FileServer implements ActionListener{

	JLabel spacerlabel = new JLabel(" ");

	JButton startButton = new JButton("Start Server");
	JTextArea textarea = new JTextArea("",20, 40);	
    public	StreamConnection con = null;
	private static StreamConnectionNotifier streamConnectionNotifier = null;


	public FileServer(){

		//Give it the Java look and feel
		JFrame.setDefaultLookAndFeelDecorated(true);	

		JFrame frame = new JFrame("FileServer ");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JScrollPane scrollPane = new JScrollPane(textarea);			
		textarea.setEditable(false);
	
		Container cp = frame.getContentPane();
		cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));

		startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
		startButton.addActionListener(this);
		cp.add(startButton);		

		spacerlabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		cp.add(spacerlabel);
		
		scrollPane.setAlignmentX(Component.CENTER_ALIGNMENT);
		cp.add(scrollPane);

		frame.pack();
		frame.setVisible(true);

		updateStatus("[server:] FileServer Application started");
		updateStatus("[server:] Press the \"Start Server\" button to await for client devices");


	}



	public void actionPerformed(ActionEvent e) {

		startButton.setEnabled(false);
		startServer();
        		
	}

	public void startServer(){
		Runnable serverOBEXJob = new ServerOBEXThread();
		//Startup the server in a new thread

		Thread ServerOBEXThread = new Thread(serverOBEXJob);
		ServerOBEXThread.start();
		
	}
	
	/*
	 * OBEX Section
	 */
	public class ServerOBEXThread extends ServerRequestHandler implements Runnable{

		public void run(){
			System.out.println("New Server OBEX Thread Running");
			UUID uuid = new UUID("8841", true);
			String url = "btgoep://localhost:" + uuid + ";name=FTP;authenticate=false;master=false;encrypt=false";
			startButton.setEnabled(false);

			System.out.println("Trying to start the OBEX connection");

			try {
				SessionNotifier sn = (SessionNotifier)Connector.open(url);

				System.out.println("Creating Connection");

				sn.acceptAndOpen(this);
				System.out.println("Connection Established");
			} catch (Exception ex){
			}

    	}


		public int onConnect(HeaderSet request, HeaderSet reply) {
			System.out.println("[server:] The client has created an OBEX session");
			return ResponseCodes.OBEX_HTTP_OK;
		}


		public int onPut (Operation op) {

			try {
				java.io.InputStream is = op.openInputStream();

				System.out.println("Got data bytes " + is.available() + " name " + op.getReceivedHeaders().getHeader(HeaderSet.NAME) + " type " + op.getType());

				File f = new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
				FileOutputStream fos = new FileOutputStream (f);
				byte b[] = new byte[1000];
				int len;

				while (is.available() > 0 && (len = is.read(b)) > 0) {
					fos.write (b, 0, len);
				}

				fos.close();
				System.out.println("[server:] Wrote data to " + f.getAbsolutePath());

			} catch (Exception e) {
				e.printStackTrace();
			}

			return ResponseCodes.OBEX_HTTP_OK;
		}


		public void onDisconnect (HeaderSet req, HeaderSet resp) {

			System.out.println("[server:] The client has disconnected the OBEX session");

		}

	}
	
	/*
	 * Update the GUI
	 */
	public void updateStatus(String message){

		textarea.append("\n" + message);

	}
	

	/* This will get the RFCOMM service listed.  It would be nice to put
	 * this into a thread of its own, but for now it works here
	 */
	
	public static void initRF(){
		
		
		UUID uuidRF = new UUID("8821", true);

		String serviceURL = "btspp://localhost:" + uuidRF.toString() + ";name=TD's Server";
		LocalDevice localDevice = null;
		
		try{

			try{

				localDevice = LocalDevice.getLocalDevice();
				streamConnectionNotifier = (StreamConnectionNotifier)Connector.open(serviceURL);
//
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {
		initRF();
		new FileServer();

	}

}

	

Objective

The objective of this lab is to modify the given code to allow for the following requirements.
  • Create the GUI for the chat program. The GUI should allow for the ability to send and receive messages as well as send and receive files.
  • One server and one client at a minimum must be used.
  • Any other features that popular chat clients use may be included.