package client;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import commoms.CommandIF;
import commoms.CommandImpl;
import commoms.ProtocolConstants;
public class JClient extends Applet implements ActionListener, KeyListener,
ItemListener, Runnable {
private String message = "";
String mess;
JPanel p1, p2, p3, p4, p5, p6;
String sendername;
String serverip;
int serverport;
String privateMsgTo;
ObjectInputStream din;
ObjectOutputStream dout;
Socket socket;
TextField tf;
TextArea jt;
Button jb;
Choice ch;
// HTMLEditorKit kit;
// RichPane richPane;
URL url;
Container cont;
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
new Thread(this).start();
}
public void run() {
try {
jt.setEditable(true);
while (true) {
CommandIF comm = (CommandIF) din.readObject();
// for reading normal messages
if (comm.getCommand().equals(ProtocolConstants.COMMAND_SEND)) {
boolean show = (!comm.isPrivateMessage())
|| comm.getMsgFrom().equals(sendername);
Iterator itr = comm.getRecipientNames().iterator();
boolean flag = false;
mess = comm.getMessage();
while (itr.hasNext()) {
if (itr.next().toString().equals(sendername)) {
flag = true;
mess = " (private message)\n" + mess;
}
}
show = show || flag;
if (show) {
//mess = comm.getMessage();
jt.append(mess + "\n");
}
}// if
// for updating peer list.
if (comm.getCommand().equals(ProtocolConstants.UPDATE_PEER_LIST)) {
List list = comm.getNewPeerList();
ch.removeAll();
Iterator itr = list.iterator();
ch.addItem(ProtocolConstants.SEND_TO_ALL);
while(itr.hasNext()) {
ch.addItem(itr.next().toString());
}
mess = comm.getMessage();
jt.append(mess + "\n");
}//if
jt.setEditable(false);
}
} catch (Exception gg) {
message = "Failed to connect with server. Try upgrading your JVM";
gg.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("send")) {
sendMessage();
}
}
public void paint(Graphics g) {
showStatus(message);
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
sendMessage();
}
}
private void sendMessage() {
try {
mess = tf.getText();
CommandIF comm = null;
if ((null == privateMsgTo) || ("".equals(privateMsgTo))) {
privateMsgTo = ProtocolConstants.SEND_TO_ALL;
}
if (privateMsgTo.equals(ProtocolConstants.SEND_TO_ALL)) {
comm = makeCommandForSendToAllMessage(sendername, mess);
} else {
comm = makeCommandForPvtMessage(sendername, mess);
}
dout.writeObject(comm);
tf.setText("");
} catch (Exception ie) {
showStatus("Message sending failed");
ie.printStackTrace();
}
System.out.println("message sent");
}
String getUrl() {
return this.url.getHost();
}
CommandIF makeCommandForSendToAllMessage(String senderName, String msg) {
CommandImpl impl = new CommandImpl();
impl.setMessage(senderName + ": " + msg);
impl.setCommand(ProtocolConstants.COMMAND_SEND);
List list = new ArrayList();
list.add(ProtocolConstants.SEND_TO_ALL);
impl.setPrivateMessage(false);
impl.setRecipientNames(list);
impl.setMsgFrom(senderName);
return impl;
}
CommandIF makeCommandForPvtMessage(String senderName, String msg) {
CommandImpl impl = new CommandImpl();
impl.setMessage(senderName + ": " + msg);
impl.setCommand(ProtocolConstants.COMMAND_SEND);
List list = new ArrayList();
list.add(privateMsgTo);
impl.setPrivateMessage(true);
impl.setRecipientNames(list);
impl.setMsgFrom(senderName);
return impl;
}
public void createGUI() {
setSize(448, 360);
setForeground(Color.BLACK);
try {
serverip = getParameter("serverip");
serverport = Integer.parseInt(getParameter("serverport"));
socket = new Socket(serverip, serverport);
din = new ObjectInputStream(socket.getInputStream());
dout = new ObjectOutputStream(socket.getOutputStream());
sendername = getParameter("sender");
message = "@@ mailto: prateekjainaa@gmail.com @@";
url = getDocumentBase();
} catch (Exception e) {
// e.printStackTrace();
message = "Server down contact network administrator";
}
Label jl0 = new Label(" Message will be send to ---> ");
add(jl0);
ch = new Choice();
ch.addItem(ProtocolConstants.SEND_TO_ALL);
ch.addItemListener(this);
add(ch);
Label jl = new Label(" Messages ");
add(jl);
jt = new TextArea();
jt.setRows(13);
jt.setSize(new Dimension(330, 270));
Font displayFont = new Font("Times Roman", Font.BOLD, 12);
jt.setFont(displayFont);
jt
.setText(" ");
jt.setEditable(false);
JScrollPane jsp = new JScrollPane(jt,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setAutoscrolls(true);
add(jsp);
Label l = new Label("Type here");
add(l);
tf = new TextField(60);
tf.setEnabled(true);
tf.addActionListener(this);
tf.addKeyListener(this);
add(tf);
jb = new Button("send");
jb.addActionListener(this);
add(jb);
CommandIF com = makeCommandForSendToAllMessage(sendername,
"Hi");
try {
dout.writeObject(com);
} catch (IOException e) {
e.printStackTrace();
}
}
public void itemStateChanged(ItemEvent e) {
privateMsgTo = ch.getSelectedItem();
}
}// end class