/** * @author Yoon Kyung Koo * Yoon Kyung Koo (contact via mailto:yoonforh@moon.daewoo.co.kr) * @version 1.1z * Version History * Version 1.0 97/12/29 * revised to support JDK 1.1 ko locale * Version 1.1 98/02/14 * Version 1.1z 99/03/16 * renamed to Gossip to keep pace * with the newly designed server named GossipServer * Copy initial GUI skeleton from Kyle's Client.java * Kyle Palmer * Client.java resides at */ import java.awt.*; import java.applet.Applet; import java.net.*; import java.io.*; public class Gossip extends Applet { ChatSession session; Button connect; Label name_box; TextField name; List user_list; public void init() { connect = new Button("Login"); name = new TextField(15); user_list = new List(5, false); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints gridbagc = new GridBagConstraints(); setLayout(gridbag); gridbagc.insets=new Insets(0, 1, 0, 1); gridbagc.fill=GridBagConstraints.HORIZONTAL; gridbagc.gridwidth = GridBagConstraints.REMAINDER; gridbagc.weightx=1.0; gridbagc.gridheight=1; gridbag.setConstraints(name, gridbagc); add(name); gridbag.setConstraints(connect, gridbagc); add(connect); gridbagc.fill=GridBagConstraints.BOTH; gridbag.setConstraints(user_list, gridbagc); add(user_list); name.setBackground(new Color(100, 255, 255)); try { name.setText(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) {} connect.setBackground(new Color(255, 255, 150)); user_list.setBackground(new Color(100, 255, 255)); setBackground(Color.darkGray); layout(); validate(); if ((ChatSession.SERVER_ADDR=getParameter("SERVER"))==null) ChatSession.SERVER_ADDR="127.0.0.1"; if (getParameter("PORT")!=null) ChatSession.SERVER_PORT=Integer.parseInt(getParameter("PORT")); else ChatSession.SERVER_PORT=8888; } public void start() { session = new ChatSession(this); session.start(); // initialize polling thread String text=name.getText(); if (text.length()!=0) { session.doConnect(text); } else session.doConnect(); } public boolean handleEvent(Event e) { if (e.target instanceof Button) { if ((connect == (Button)e.target)&& ( e.id == Event.ACTION_EVENT )) { String text=name.getText(); if (text.length()!=0) { session.doConnect(text); } else session.doConnect(); session.show(); return true; } } return false; } public void stop() { session.stop(); session = null; } } // class Gossip class ChatSession extends Frame implements Runnable{ // the working window static String SERVER_ADDR=null; static int SERVER_PORT=8888; static final int MAXUSER = 25; final int MAXCOL = 62; Gossip applet = null; // Chat class Fields boolean is_connected = false; // awt elements Label users_name = null; TextField textField = null; TextArea textArea = null; TextField name = null; Socket to_server = null; BufferedReader is = null; PrintWriter os = null; Thread thread = null; Label name_box = null; Label input_box = null; Label users = null; List user_list = null; Checkbox only_cb = null; Button quit = null; Button connect = null; Button close = null; Button clear = null; // Chat class Methods public ChatSession(Gossip applet){ // constructor super("Chat Window"); // call Frame's constructor this.applet=applet; is_connected=false; textField = new TextField(80); //all of the UI components textArea = new TextArea(12, MAXCOL); user_list = new List(MAXUSER, false); name = new TextField(15); textArea.setEditable(false); quit = new Button("Disconnect"); //The quit button connect = new Button ("Connect"); //the connect button clear = new Button ("Clear"); close = new Button ("Close"); users_name = new Label("Current User"); name_box = new Label("Name: "); only_cb = new Checkbox("Private"); only_cb.setFont(new Font("Helvetica", Font.PLAIN, 10)); setFont(new Font("Helvetica", Font.PLAIN, 12)); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints gridbagc = new GridBagConstraints(); setLayout(gridbag); Panel topP = new Panel(); topP.setLayout(new FlowLayout(0)); topP.add(name_box); topP.add(name); name.setText("À̸§¾øÀ½"); topP.add(connect); topP.add(quit); topP.add(clear); topP.add(close); gridbagc.fill=GridBagConstraints.BOTH; gridbagc.gridwidth = GridBagConstraints.REMAINDER; gridbagc.weightx=1.0; gridbag.setConstraints(topP, gridbagc); add(topP); gridbagc.weighty=1.0; Panel disp_panel = new Panel(); GridBagLayout pgridbag = new GridBagLayout(); disp_panel.setLayout(pgridbag); textArea.setEditable(false); pgridbag.setConstraints(textArea, gridbagc); disp_panel.add(textArea); gridbagc.gridwidth = GridBagConstraints.RELATIVE; gridbag.setConstraints(disp_panel, gridbagc); add(disp_panel); Panel lst_panel = new Panel(); BorderLayout lpbl = new BorderLayout(0,0); lst_panel.setLayout(lpbl); lst_panel.add("North", users_name); lst_panel.add("Center", user_list); lst_panel.add("South", only_cb); gridbagc.weighty=0.0; gridbagc.weightx=0.0; gridbagc.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(lst_panel, gridbagc); add(lst_panel); Panel ctrl_panel = new Panel(); ctrl_panel.setLayout(new FlowLayout(FlowLayout.LEFT)); ctrl_panel.add(textField); gridbag.setConstraints(ctrl_panel, gridbagc); add(ctrl_panel); setBackground(new Color(255, 255, 150)); try { setSize(650,350); // Default Window Size } catch (NoSuchMethodError nsme) { resize(650,350); // Default Window Size displayln("ºê¶ó¿ìÀú¸¦ JDK 1.1·Î ¾÷±×·¹À̵åÇϼ¼¿ä.\nPlease, Upgrade to JDK 1.1"); } setResizable(false); } public void start() { thread = new Thread(this,"main"); //create a thread for this window thread.setPriority(Thread.MIN_PRIORITY); //low priority so components work thread.start(); } void doConnect() { if ( is_connected ) { return; } user_list.clear(); //clear the user list (clear() should be replaced with removeAll() in JDK 1.1) applet.user_list.clear(); //clear the user list (clear() should be replaced with removeAll() in JDK 1.1) is_connected = false; //no longer connected only_cb.setState(false); textArea.setText(""); displayln("(¾È³»)¼­¹ö¿¡ ¿¬°áÀ» ½ÃµµÇÏ°í ÀÖ½À´Ï´Ù..."); try{ try{ to_server = new Socket(ChatSession.SERVER_ADDR,ChatSession.SERVER_PORT); }catch(UnknownHostException uhe){ displayln("(¿¡·¯)¼­¹ö¿¡ ¿¬°áÇÒ ¼ö ¾ø½À´Ï´Ù."+uhe.toString()); return ; } try { is = new BufferedReader(new InputStreamReader(to_server.getInputStream(), "EUC_KR")); os = new PrintWriter (new OutputStreamWriter(to_server.getOutputStream(), "EUC_KR"), true /* set autoflush mode */); } catch (UnsupportedEncodingException uee) { is = new BufferedReader(new InputStreamReader(to_server.getInputStream())); os = new PrintWriter (to_server.getOutputStream(), true /* set autoflush mode */); } } catch(IOException ie){ displayln("(¿¡·¯)¼­¹ö¿¡ ¿¬°áÇÒ ¼ö ¾ø½À´Ï´Ù."+ie.toString()); return ; } is_connected = true; //make a connection String text = name.getText(); // text = convert_to_ascii(text); os.println("##name##"+text); //send the name with name command // thread.resume(); } void doConnect(String text) { String curName = name.getText(); if (text.equals(curName)) doConnect(); else { name.setText(text); if (is_connected) os.println("##name##"+text); //send the name with name command else doConnect(); } } void doClose() { displayln("(¾È³»)Á¢¼ÓÀ» ÇØÁ¦ÇÏ°í ÀÖ½À´Ï´Ù."); if (os != null && is != null){ os.println("##quit##"); os.close(); try{ is.close(); } catch(IOException e) { displayln("(¿¡·¯)Á¢¼Ó ¿¡·¯ÀÔ´Ï´Ù."); } if (to_server != null) { try{ to_server.close(); } catch(IOException e) { displayln("(¿¡·¯)Á¢¼Ó ¿¡·¯ÀÔ´Ï´Ù."); } } displayln("(¾È³»)Á¢¼ÓÀ» ÇØÁ¦ÇÏ¿´½À´Ï´Ù."); user_list.clear(); //clear the user list (clear() should be replaced with removeAll() in JDK 1.1) applet.user_list.clear(); //clear the user list (clear() should be replaced with removeAll() in JDK 1.1) only_cb.setState(false); textArea.setText(""); displayln("(¾È³»)'Á¢¼Ó' ¹öÆ°À» ´©¸£¸é Á¢¼ÓÇÕ´Ï´Ù."); } // thread.suspend(); is_connected = false; //no longer connected } public void run(){ String text = null; while (thread==Thread.currentThread()) { // to remove unreliable Thread.stop() method try { thread.sleep(50); } catch(InterruptedException e) { } if (!is_connected || is == null || os == null) continue; try{ if (is.ready()) { try{ text = is.readLine() + '\n'; // text = convert_to_ksc(text); // System.out.println("DEBUG:text="+text); } catch(IOException e) { displayln("(¿¡·¯)"+e.getMessage()); doClose(); } if (text.startsWith("##names##")) //if is a names list write_list(text.substring(9)); //update the list else if (text.startsWith("(¼­¹ö)")) // if server message display(text); //otherwise display the text else { // if server message display(text); //otherwise display the text show(); // show if message arrived } } } catch(IOException e) { // when is.ready() displayln("(¿¡·¯)"+e.getMessage()); doClose(); } } // end of while } // end of run public void write_list(String list) { //displays the user list int i; int next; user_list.clear(); //erase the current list (clear() should be replaced with removeAll() in JDK 1.1) applet.user_list.clear(); //erase the current list (clear() should be replaced with removeAll() in JDK 1.1) for (i=0;i= list.length() || next == -1) break; //done on last element user_list.addItem(list.substring(i,next)); //write this user applet.user_list.addItem(list.substring(i,next)); //write this user // System.out.println("DEBUG:add item>"+list.substring(i,next)); i = next; //look for a new delimiter } } public void display(String s) { //write to the text area displayText(s); } public void displayln(String s) { //write to the text area displayText(s); displayText("\n"); } public void displayText(String s) { try { textArea.append(s); } catch (NoSuchMethodError nsme) { textArea.appendText(s); } try { thread.sleep(100); } catch(InterruptedException e) { } } public boolean handleEvent(Event evt) { if ( evt.id == Event.ACTION_EVENT ) { if ( is_connected) { if (os == null || is == null){ displayln("(¾È³»)Á¢¼ÓµÇ¾î ÀÖÁö ¾Ê½À´Ï´Ù."); return super.handleEvent(evt); } else if (evt.target == textField){ //if want to send the text String text = textField.getText(); textField.setText(""); String receiver=null; if ( only_cb.getState() && ( (receiver=user_list.getSelectedItem()) != null ) ) text = "##only##"+receiver+':'+text; if (text.length()>0) { // text = convert_to_ascii(text); os.println(text); if (os.checkError()) { displayln("(¿¡·¯)¿¬°á¿¡ ¹®Á¦°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù."); doClose(); } } } else if (evt.target == name){ //if want to send the text String text = name.getText(); applet.name.setText(text); // text = convert_to_ascii(text); os.println("##name##"+text); //send the name with name command if (os.checkError()) { displayln("(¿¡·¯)¿¬°á¿¡ ¹®Á¦°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù."); doClose(); } } else if (evt.target == quit){ //user wants to quit doClose(); } // end quit else if (evt.target == close) { //if request to close the window // doClose(); // keep the connection dispose(); } else if (evt.target == clear) textArea.setText(""); else if (evt.target == connect) displayln("(¾È³»)ÀÌ¹Ì ¿¬°áµÇ¾î ÀÖ½À´Ï´Ù."); else if (evt.target == user_list) { // System.out.println("DEBUG> Event Occurred"+user_list.getSelectedIndex()); } } // end is_connected else { if (evt.target == connect) doConnect(); else if (evt.target == clear) textArea.setText(""); else if (evt.target == close) { //if request to close the window if (os != null && is != null){ os.println("##quit##"); try{ os.close(); is.close(); if (to_server != null) to_server.close(); } catch(IOException e) { } } dispose(); } else displayln("(¾È³»)'Á¢¼Ó' ¹öÆ°À» ´©¸£¸é Á¢¼ÓÇÕ´Ï´Ù."); } // end else ( not connected ) } // end if ACTION_EVENT else if (evt.id==Event.WINDOW_DESTROY) { dispose(); } return super.handleEvent(evt); } public void stop() { thread=null; doClose(); dispose(); } public static String convert_to_ksc(String str) { String result=null; try { byte[] rawBytes=str.getBytes("8859_1"); result=new String(rawBytes, "EUC_KR"); } catch (java.io.UnsupportedEncodingException e) { System.err.println(e.toString()); return str; } return result; } public static String convert_to_ascii(String str) { String result=null; try { byte[] kscBytes=str.getBytes("EUC_KR"); result=new String(kscBytes, "8859_1"); } catch (java.io.UnsupportedEncodingException e) { System.err.println(e.toString()); return str; } return result; } }