Skip to content

Code clean up #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 47 additions & 58 deletions src/ChatClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;


// CLASS: ChatClient
Expand All @@ -20,13 +30,15 @@ public class ChatClient
String username;
String[] ps;


// FUNCTION: main
public static void main(String[] args)
{
ChatClient client = new ChatClient();
client.go();
}


// FUNCTION: go
public void go()
{
Expand All @@ -35,15 +47,15 @@ public void go()
JPanel mainPanel = new JPanel();

JLabel l1, l2;
l1 = new JLabel("Message Box");
l2 = new JLabel("User List");
l1 = new JLabel(Constants.MESSAGE_BOX);
l2 = new JLabel(Constants.USER_LIST);


incoming = new JTextArea(15, 50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);


JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
Expand All @@ -52,46 +64,44 @@ public void go()
userList.setLineWrap(true);
userList.setWrapStyleWord(true);
userList.setEditable(false);



JScrollPane uScroller = new JScrollPane(userList);
uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");

JButton sendButton = new JButton(Constants.SEND);
sendButton.addActionListener(new SendButtonListener());

//mainPanel.add(l1);
//mainPanel.add(l2);
mainPanel.add(qScroller);
mainPanel.add(uScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);

setUpNetworking();

username = JOptionPane.showInputDialog("Welcome ! Kindly say who you are ?");
username = JOptionPane.showInputDialog(Constants.WELCOME_MESSAGE);

frame.setTitle(username.toUpperCase());

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
//frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}


// FUNCTION: setUpNetworking
private void setUpNetworking()
{

try
{
sock = new Socket("127.0.0.1", 6666);
sock = new Socket(Constants.HOST, Constants.PORT);

InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());

Expand Down Expand Up @@ -124,63 +134,42 @@ public void actionPerformed(ActionEvent ev)
{
ex.printStackTrace();
}

outgoing.setText("");
outgoing.requestFocus();
}
}

public class IncomingReader implements Runnable {

// CLASS: IncomingReader
public class IncomingReader implements Runnable
{

// FUNCTION: run
public void run()
{
public void run() {
String message;

try
{
while((message = reader.readLine()) != null)
{
if(!messageOrList(message))
{
//System.out.println("read" + message);
incoming.append( message + "\n");
}
else
{
try {

while ((message = reader.readLine()) != null) {
if (!messageOrList(message)) {
incoming.append(message + "\n");
} else {
userList.setText("");
for(int i = 1; i < ps.length; i++)
{
//System.out.println(ps[i]);
for (int i = 1; i < ps.length; i++) {

userList.append(ps[i] + "\n");
}

}

}
}
catch(Exception ex)
{
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

// FUNCTION: messageOrList
public boolean messageOrList(String ms)
{
ps = ms.split("\\,");

if(ps[0].equals("000.."))
{
return true;
}
else
{
return false;
}

public boolean messageOrList(String ms) {
ps = ms.split(Constants.REG_EX_ESC_PATTERN);
return Constants.SHOW_MSG_VAL_CON_VAL.equals(ps[0]);
}
}



}
86 changes: 38 additions & 48 deletions src/ChatServer.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// CLASS: ChatServer
public class ChatServer
{
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;


public class ChatServer {

JTextArea clientsMessage;
JTextArea userList;
Expand All @@ -21,35 +28,25 @@ public class ChatServer
ArrayList<String> uname = new ArrayList<String>();
ArrayList<Socket> port = new ArrayList<Socket>();


// CLASS: ClientHandler
public class ClientHandler implements Runnable
{
public class ClientHandler implements Runnable {

BufferedReader reader;
Socket sock;
//JTextArea clientsMessage; get nullPointerException for this line wasted one day

// FUNCTION: ClientHandler
public ClientHandler(Socket clientSocket)
{
public ClientHandler(Socket clientSocket) {

try
{
try {
sock = clientSocket;

InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
}
catch(Exception ex)
{
ex.printStackTrace();

} catch (IOException e) {
e.printStackTrace();
}
}

// FUNCTION: run
public void run()
{

public void run() {

String message;
String[] parts = null;
Expand All @@ -68,7 +65,6 @@ public void run()
uname.add(parts[0]);
once = true;
}

//System.out.println("read" + message);
clientsMessage.append(message + "\n");
tellEveryone(message);
Expand Down Expand Up @@ -106,14 +102,10 @@ public void actionPerformed(ActionEvent arg0)

}

// FUNCTION: showingClients
public void showingClients()
{
String st = "000..,";
String t = "";

for(String s : uname)
{
public void showingClients() {
String st = Constants.SHOW_MSG_VAL_CON_VAL;
String t = Constants.EMPTY;
for (String s : uname) {
st += s + ",";
t += s + "\n";
}
Expand Down Expand Up @@ -163,6 +155,7 @@ public void go()
}
catch(Exception ex)
{

ex.printStackTrace();
}

Expand Down Expand Up @@ -200,7 +193,7 @@ public void addAction()
clientsMessage.setLineWrap(true);
clientsMessage.setWrapStyleWord(true);
clientsMessage.setEditable(false);

JScrollPane qScroller = new JScrollPane(clientsMessage);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
Expand All @@ -214,7 +207,7 @@ public void addAction()
uScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

smsToAll = new JTextField(20);

sendButton = new JButton("Send To All");
sendButton.addActionListener(new SendButtonListener());

Expand All @@ -227,18 +220,15 @@ public void addAction()
window.setSize(800, 400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);

clientsMessage.setText("");
clientsMessage.setText(Constants.EMPTY);

}

// FUNCTION: main
public static void main(String[] args)
{

public static void main(String[] args) {
ChatServer cs = new ChatServer();

cs.addAction();
cs.go();
}


}
12 changes: 12 additions & 0 deletions src/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Constants {
public static final String CLIENT = "Client";
public static final String SEND = "Send";
public static final String MESSAGE_BOX = "Message Box";
public static final String USER_LIST = "User List";
public static final String WELCOME_MESSAGE = "Welcome ! Kindly say who you are ?";
public static final String HOST = "127.0.0.1";
public static final int PORT = 6666;
public static final String SHOW_MSG_VAL_CON_VAL = "000..";
public static final String REG_EX_ESC_PATTERN = "\\,";
public static final String EMPTY = "";
}
Loading