diff --git a/src/chatGUI/ChatController.java b/src/chatGUI/ChatController.java new file mode 100644 index 0000000..7851b78 --- /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 javaChat.ClientConnection; + +/** + * + * @author cleanrun + */ +public class ChatController implements ActionListener { + + private ChatView view; + private ClientConnection client = null; + + public ChatController() { + view = new ChatView(); + view.setVisible(true); + view.addListener(this); + client = null; + } + + public class WriteOutput extends Thread { + + public void run() { + try { + String inputan; + + while ((inputan = client.readStream()) != null) { + view.setTxAreaChat(inputan); + } + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } + } + + + public void actionPerformed(ActionEvent ae) { + Object source = ae.getSource(); + if (source == view.getTxFieldChat()) { + if (client == null) { + try { + client = new ClientConnection(); + String ip = view.getStringChat(); + client.connect(ip); + + WriteOutput w = new WriteOutput(); + w.start(); + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } 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..b21c6a6 --- /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..c7f57a8 --- /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 java.awt.event.ActionListener; +import javax.swing.JTextArea; + +/** + * + * @author cleanrun + */ +public class ChatView extends javax.swing.JFrame { + + /** + * Creates new form ChatView + */ + public ChatView() { + initComponents(); + } + + 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); + } + + /** + * 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, 380, 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, 243, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(txFieldChat, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE) + .addGap(6, 6, 6)) + ); + + 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 +} diff --git a/src/consoleApp/ConsoleApplication.java b/src/consoleApp/ConsoleApplication.java new file mode 100644 index 0000000..6b1c1b0 --- /dev/null +++ b/src/consoleApp/ConsoleApplication.java @@ -0,0 +1,58 @@ +package consoleApp; + +import javaChat.ClientConnection; + +public class ConsoleApplication extends Thread { + + private ClientConnection client; + + public class ReadInput extends Thread { + + public void run() { + try { + String inputKeyboard; + do { + System.out.println(">> "); + inputKeyboard = client.inputString(); + client.writeStream(inputKeyboard); + } while (!inputKeyboard.equals("quit")); + client.disconnect(); + } catch (Exception e) { + System.out.println(" Error :" + e.getMessage()); + } + + } + } + + public class WriteInput extends Thread { + + public void run() { + try { + String inputan; + while ((inputan = client.readStream()) != null) { + System.out.println(inputan); + System.out.println(">> "); + } + } catch (Exception e) { + System.out.println(" Error :" + e.getMessage()); + } + } + } + + public void startChat() { + try { + client = new ClientConnection(); + System.out.println("input server IP : "); + String ip = client.inputString(); + client.connect(ip); + + ReadInput in = new ReadInput(); + WriteInput out = new WriteInput(); + in.start(); + out.start(); + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } + +} diff --git a/src/consoleApp/connectionthread.java b/src/consoleApp/connectionthread.java new file mode 100644 index 0000000..f00c169 --- /dev/null +++ b/src/consoleApp/connectionthread.java @@ -0,0 +1,41 @@ +package consoleApp; + +import java.io.IOException; +import java.net.Socket; +import javaChat.Connection; + +public class connectionthread extends Thread{ + private Socket client; + private Connection connection; + + public connectionthread(Socket newClient) throws IOException{ + this.client = newClient; + connection = new Connection(client); + } + + public void run(){ + try{ + 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 = "Clien from IP : " + connection.getIpClient() + + "Quit the chat room"; + System.out.println(message); + connection.sendToAll(message); + connection.disconnect(); + } catch (IOException ex){ + System.out.println("Error : " + ex);} + } + } + + diff --git a/src/driver/DriverClientConsole.java b/src/driver/DriverClientConsole.java new file mode 100644 index 0000000..ad23375 --- /dev/null +++ b/src/driver/DriverClientConsole.java @@ -0,0 +1,11 @@ +package driver; + +import consoleApp.ConsoleApplication; + +public class DriverClientConsole { + public static void main(String[] args) { + ConsoleApplication cpp = new ConsoleApplication(); + cpp.startChat(); + } + +} diff --git a/src/driver/DriverClientGui.java b/src/driver/DriverClientGui.java new file mode 100644 index 0000000..2748825 --- /dev/null +++ b/src/driver/DriverClientGui.java @@ -0,0 +1,9 @@ +package driver; + +import chatGUI.ChatController; + +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..8b77309 --- /dev/null +++ b/src/driver/DriverServer.java @@ -0,0 +1,22 @@ +package driver; + +import consoleApp.connectionthread; +import javaChat.ServerConnection; + +public class DriverServer { + + public static void main(String[] args) { + try { + ServerConnection server = new ServerConnection(); + System.out.println("Server Information"); + System.out.println(server.getServerInformation()); + while(true){ + connectionthread connection = new connectionthread(server.getClient()); + connection.start(); + } + } catch (Exception e) { + System.out.println("Error : " + e.getMessage()); + } + } + +}