I forgot the attach the source in my previous mail. here it is
import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import org.apache.mina.common.*;
import org.apache.mina.io.socket.*;
import org.apache.mina.registry.*;
import org.apache.mina.io.*;

abstract class ChatFrame {
	JTextField textField;
	JTextArea textArea;
	
	ChatFrame(String title) {
		JFrame frame = new JFrame(title);
		textField = new JTextField();
		textArea = new JTextArea();
		frame.add(textField, BorderLayout.NORTH);
		frame.add(textArea, BorderLayout.CENTER);
		
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendMessage(textField.getText());
			}
		});
		
		frame.setSize(new Dimension(300,300));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	abstract void sendMessage(String msg);
}

class ChatHandler implements IoHandler {
	JTextArea textArea;
	
	ChatHandler(JTextArea textArea) {
		this.textArea = textArea;
	}
	
	public void dataRead(IoSession session, ByteBuffer buf) {
		System.out.println("dataRead " + buf.remaining() + " bytes");
		
		byte[] bytes = new byte[buf.remaining()];
		buf.get(bytes);
		String msg = new String(bytes);
		textArea.append(msg + "\r\n");
	}
	
	public void dataWritten(IoSession session, Object marker) {
		System.out.println("dataWritten");
	}
	
	public void exceptionCaught(IoSession session, Throwable cause) {
		System.out.println("exceptionCaught " + cause);
	}
	
	public void sessionClosed(IoSession session) {
		System.out.println("sessionClosed");
	}
	
	public void sessionCreated(IoSession session) {
		System.out.println("sessionCreated");
	}
	
	public void sessionIdle(IoSession session, IdleStatus status) {
		System.out.println("sessionIdle " + status);
	}
	
	public void sessionOpened(IoSession session) {
		System.out.println("sessionOpened");
	}
}

class Client extends ChatFrame {
	IoSession session;
	
	Client() throws Exception {
		super("client");
		
		SocketConnector socket = new SocketConnector();
		session = socket.connect(
			new InetSocketAddress(InetAddress.getLocalHost(), 8888),
			new ChatHandler(textArea));
	}
	
	public void sendMessage(String msg) {
		ByteBuffer buf = ByteBuffer.allocate(msg.length());
		buf.put(msg.getBytes());
		buf.flip();
		
		session.write(buf, this);
	}
	
	public static void main(String[] args) throws Exception {
		new Client();
	}
}

class Server extends ChatFrame {
	ClientHandler clientHandler = new ClientHandler(textArea);
	
	Server() throws Exception {
		super("server");
		
		SimpleServiceRegistry registry = new SimpleServiceRegistry();
		registry.bind(new Service("server", TransportType.SOCKET, 8888), clientHandler);
		System.out.println("server bound");
	}
	
	public void sendMessage(String msg) {
		ByteBuffer buf = ByteBuffer.allocate(msg.length());
		buf.put(msg.getBytes());
		buf.flip();
		
		clientHandler.sendToAll(buf);
		buf.release();
	}
	
	public static void main(String[] args) throws Exception {
		new Server();
	}
	
	class ClientHandler extends ChatHandler {
		LinkedList<IoSession> list = new LinkedList<IoSession>();
		
		ClientHandler(JTextArea textArea) {
			super(textArea);
		}
		
		public void sendToAll(ByteBuffer buf) {
			Iterator<IoSession> it = list.iterator();
			while(it.hasNext()) {
				buf.acquire();
				buf.flip();
				IoSession session = it.next();
				
				session.write(buf, this);
			}
		}
		
		public void dataRead(IoSession session, ByteBuffer buf) {
			super.dataRead(session, buf);
			sendToAll(buf);
		}
		
		public void sessionClosed(IoSession session) {
			super.sessionClosed(session);
			list.remove(session);
		}
		
		public void sessionCreated(IoSession session) {
			super.sessionCreated(session);
			list.add(session);
		}
	}
}

Reply via email to