From 245bd25cf23cd6d7d48fcfdcd52b25c282485fb7 Mon Sep 17 00:00:00 2001 From: toniebe Date: Wed, 17 May 2017 20:27:40 +0700 Subject: [PATCH] 1301154217_AhmadFathoni --- .gitignore | 1 + nbproject/build-impl.xml | 21 ++-- nbproject/genfiles.properties | 4 +- nbproject/private/private.properties | 2 +- nbproject/project.properties | 2 +- src/chatGUI/ChatController.java | 65 +++++++++++++ src/chatGUI/ChatView.form | 69 +++++++++++++ src/chatGUI/ChatView.java | 130 +++++++++++++++++++++++++ src/consoleApp/ConnectionThread.java | 52 ++++++++++ src/consoleApp/ConsoleApplication.java | 71 ++++++++++++++ src/driver/DriverClientConsole.java | 19 ++++ src/driver/DriverClientGui.java | 23 +++++ src/driver/DriverServer.java | 30 ++++++ 13 files changed, 475 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 src/chatGUI/ChatController.java create mode 100644 src/chatGUI/ChatView.form create mode 100644 src/chatGUI/ChatView.java create mode 100644 src/consoleApp/ConnectionThread.java create mode 100644 src/consoleApp/ConsoleApplication.java create mode 100644 src/driver/DriverClientConsole.java create mode 100644 src/driver/DriverClientGui.java create mode 100644 src/driver/DriverServer.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d6549 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ \ No newline at end of file diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index 4abce43..f071bc4 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -46,8 +46,8 @@ is divided into following sections: - - + + @@ -76,7 +76,7 @@ is divided into following sections: - + @@ -156,6 +156,7 @@ is divided into following sections: + @@ -840,7 +841,7 @@ is divided into following sections: - + @@ -852,7 +853,7 @@ is divided into following sections: - + @@ -975,15 +976,15 @@ is divided into following sections: - + - + - + @@ -991,7 +992,7 @@ is divided into following sections: - + @@ -1186,7 +1187,7 @@ is divided into following sections: Must select one file in the IDE or set run.class - + Must select one file in the IDE or set applet.url diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index cb2d623..a1e02d5 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -4,5 +4,5 @@ build.xml.stylesheet.CRC32=8064a381@1.75.1.48 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=6d39ee20 -nbproject/build-impl.xml.script.CRC32=681df6e3 -nbproject/build-impl.xml.stylesheet.CRC32=05530350@1.79.1.48 +nbproject/build-impl.xml.script.CRC32=5c486f47 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index 1b6c36a..f9a8466 100644 --- a/nbproject/private/private.properties +++ b/nbproject/private/private.properties @@ -1,2 +1,2 @@ compile.on.save=true -user.properties.file=C:\\Users\\ANDITYAARIFIANTO\\AppData\\Roaming\\NetBeans\\8.1\\build.properties +user.properties.file=C:\\Users\\toniebe\\AppData\\Roaming\\NetBeans\\8.2\\build.properties diff --git a/nbproject/project.properties b/nbproject/project.properties index de16b77..d23ca2f 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -53,7 +53,7 @@ javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= -main.class=task.Task +main.class=driver.DriverClientConsole manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/src/chatGUI/ChatController.java b/src/chatGUI/ChatController.java new file mode 100644 index 0000000..0cdc3ec --- /dev/null +++ b/src/chatGUI/ChatController.java @@ -0,0 +1,65 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package chatGUI; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import javaChat.ClientConnection; +import javax.swing.text.View; + +/** + * + * @author toniebe + */ +public class ChatController implements ActionListener { + + private ChatView view; + private ClientConnection client; + + public ChatController() { + view = new ChatView(); + view.setVisible(true); + view.addListener(this); + client = null; + } + + public class WriteOutput extends Thread { + + public void run() { + String inputan; + try { + while ((inputan = client.readStream()) != null) { + view.setTxAreaChat(inputan); + } + } catch (IOException ex) { + System.out.println("Error !"); + } + } + } + + public void actionPerformed(ActionEvent ae) { + Object source = ae.getSource(); + + if (source.equals(view.getTxFieldChat())) { + if (client == null) { + try { + client = new ClientConnection(); + String ip = view.getStringChat(); + client.connect(ip); + WriteOutput w = new WriteOutput(); + w.start(); + } catch (IOException ex) { + System.out.println("Error !"); + } + } else { + String input = view.getStringChat(); + client.writeStream(input); + view.setTxFieldChat(""); + } + } + } +} diff --git a/src/chatGUI/ChatView.form b/src/chatGUI/ChatView.form new file mode 100644 index 0000000..89d12c7 --- /dev/null +++ b/src/chatGUI/ChatView.form @@ -0,0 +1,69 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/chatGUI/ChatView.java b/src/chatGUI/ChatView.java new file mode 100644 index 0000000..07454cf --- /dev/null +++ b/src/chatGUI/ChatView.java @@ -0,0 +1,130 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package chatGUI; + +import javax.swing.JTextArea; +import javax.swing.JTextField; +import java.awt.event.ActionListener; + +/** + * + * @author toniebe + */ +public class ChatView extends javax.swing.JFrame { + + /** + * Creates new form ChatView + */ + public ChatView() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + txAreaChat = new javax.swing.JTextArea(); + txFieldChat = new javax.swing.JTextField(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + txAreaChat.setEditable(false); + txAreaChat.setColumns(20); + txAreaChat.setRows(5); + txAreaChat.setText("Input Server IP Address : "); + jScrollPane1.setViewportView(txAreaChat); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE) + .addComponent(txFieldChat)) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(txFieldChat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new ChatView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JTextArea txAreaChat; + private javax.swing.JTextField txFieldChat; + // End of variables declaration//GEN-END:variables + + public void setTxAreaChat(String message) { + txAreaChat.append(message + "\n"); + } + + public Object getTxFieldChat() { + return txFieldChat; + } + + public String getStringChat() { + return txFieldChat.getText(); + } + + public void setTxFieldChat(String message) { + this.txFieldChat.setText(message); + } + + public void addListener(ActionListener e) { + txFieldChat.addActionListener(e); + } +} diff --git a/src/consoleApp/ConnectionThread.java b/src/consoleApp/ConnectionThread.java new file mode 100644 index 0000000..c4ddff2 --- /dev/null +++ b/src/consoleApp/ConnectionThread.java @@ -0,0 +1,52 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package consoleApp; + +import java.io.IOException; +import java.net.Socket; +import javaChat.Connection; + +/** + * + * @author toniebe + */ +public class ConnectionThread { + private Socket client; + private Connection connection; + + public ConnectionThread(Socket newClient) throws IOException { + this.client = newClient; + connection = new Connection(client); + } + + + + public void run() { + try { + connection.startChat("Start chat"); + System.out.println("-----------------------"); + System.out.println("new client connected"); + System.out.println("client information"); + System.out.println(connection.getClientInformation()); + + String inputan; + String message; + while ((inputan = connection.readStream()) != null && !inputan.equals("quit")) { + message = "Client" + connection.getIpClient() + " said : " + inputan; + System.out.println(message); + connection.sendToAll(message); + } + + message = "Client from IP : " + connection.getIpClient() + "Quit the chat room"; + System.out.println(message); + connection.sendToAll(message); + connection.disconnect(); + + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } +} diff --git a/src/consoleApp/ConsoleApplication.java b/src/consoleApp/ConsoleApplication.java new file mode 100644 index 0000000..3f92946 --- /dev/null +++ b/src/consoleApp/ConsoleApplication.java @@ -0,0 +1,71 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package consoleApp; + +import javaChat.ClientConnection; + +/** + * + * @author toniebe + */ +public class ConsoleApplication { + private ClientConnection client; + + public void startChat() { + try { + client = new ClientConnection(); + System.out.println(">> input server IP : "); + String ip = client.inputString(); + client.connect(ip); + + ReadInput in = new ReadInput(); + WriteOutput out = new WriteOutput(); + in.start(); + out.start(); + + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + + + } + + public class ReadInput extends Thread{ + + @Override + public void run() { + try { + String inputKey; + do { + System.out.println(">> "); + inputKey = client.inputString(); + client.writeStream(inputKey); + } while (!inputKey.equals("Quit")); + client.disconnect(); + + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } + } + + public class WriteOutput extends Thread{ + + @Override + public void run() { + try { + String input; + while ((input = client.readStream()) != null) { + System.out.println(input); + System.out.println(">> "); + } + + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } + } +} diff --git a/src/driver/DriverClientConsole.java b/src/driver/DriverClientConsole.java new file mode 100644 index 0000000..4a7822d --- /dev/null +++ b/src/driver/DriverClientConsole.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package driver; + +import consoleApp.ConsoleApplication; + +/** + * + * @author toniebe + */ +public class DriverClientConsole { + public static void main(String[] args) { + ConsoleApplication ca = new ConsoleApplication(); + ca.startChat(); + } +} diff --git a/src/driver/DriverClientGui.java b/src/driver/DriverClientGui.java new file mode 100644 index 0000000..ad4d6be --- /dev/null +++ b/src/driver/DriverClientGui.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package driver; + +import chatGUI.ChatController; + + + + + +/** + * + * @author toniebe + */ +public class DriverClientGui { + public static void main(String[] args) { + ChatController cc = new ChatController(); + + } +} diff --git a/src/driver/DriverServer.java b/src/driver/DriverServer.java new file mode 100644 index 0000000..2d4c17e --- /dev/null +++ b/src/driver/DriverServer.java @@ -0,0 +1,30 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package driver; + +import consoleApp.ConnectionThread; +import java.io.IOException; +import javaChat.ServerConnection; + +/** + * + * @author toniebe + */ +public class DriverServer { + public static void main(String[] args) throws IOException { + try { + ServerConnection server = new ServerConnection(); + System.out.println("Server Information"); + System.out.println(server.getServerInformation()); + while (true) { + ConnectionThread connection = new ConnectionThread(server.getClient()); + connection.run(); + } + } catch (IOException ex) { + System.out.println("error !"); + } + } +}