From b3d5c94f617607ec43f0af54d2fac002fec9c40f Mon Sep 17 00:00:00 2001 From: ValentinTishenko Date: Thu, 29 Oct 2015 12:07:25 +0200 Subject: [PATCH 1/5] Create MYCHAT --- MYCHAT | 1 + 1 file changed, 1 insertion(+) create mode 100644 MYCHAT diff --git a/MYCHAT b/MYCHAT new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/MYCHAT @@ -0,0 +1 @@ + From 28ee59770d4ac16c9bd8614879d4ed51f7cb37e3 Mon Sep 17 00:00:00 2001 From: NikitNikit Date: Wed, 9 Dec 2015 20:16:38 +0200 Subject: [PATCH 2/5] i --- chatapp.iml | 11 +++ src/CallListener.java | 99 +++++++++++++++++++++ src/CallListenerThread.java | 88 +++++++++++++++++++ src/Caller.java | 104 ++++++++++++++++++++++ src/ChatForm.form | 122 ++++++++++++++++++++++++++ src/ChatForm.java | 168 ++++++++++++++++++++++++++++++++++++ src/Command.java | 33 +++++++ src/Connection.java | 78 +++++++++++++++++ src/Main.java | 8 ++ 9 files changed, 711 insertions(+) create mode 100644 chatapp.iml create mode 100644 src/CallListener.java create mode 100644 src/CallListenerThread.java create mode 100644 src/Caller.java create mode 100644 src/ChatForm.form create mode 100644 src/ChatForm.java create mode 100644 src/Command.java create mode 100644 src/Connection.java create mode 100644 src/Main.java diff --git a/chatapp.iml b/chatapp.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/chatapp.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/CallListener.java b/src/CallListener.java new file mode 100644 index 0000000..6aaf6e6 --- /dev/null +++ b/src/CallListener.java @@ -0,0 +1,99 @@ +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; + +public class CallListener { + public static final int port = 28411; + public String localNick; + public String localAddress; + private boolean busy; + private SocketAddress listenAddress,remoteAdress; + private ServerSocket serverSocket; + public String remoteNick; + public Socket socket; + public CallListener() throws IOException { + this("without name"); + } + + public CallListener(String localNick) { + this.localNick = localNick; + this.serverSocket = null; + + } + + public CallListener(String localNick, String localAdress)throws IOException{ + try { + serverSocket = new ServerSocket(28411); + } catch (IOException e) { + e.printStackTrace(); + } + if (localAdress != null) + serverSocket.bind(new InetSocketAddress(localAdress, 28411)); + this.localNick=localNick; + this.listenAddress =new InetSocketAddress(localAdress,port); + } + + public Connection getConnection() throws IOException { + socket = serverSocket.accept(); + return new Connection(socket, localNick); + } + + public SocketAddress getListenAddress() { + return listenAddress; + } + + public void setListenAddress(SocketAddress listenAddress) { + this.listenAddress = listenAddress; + } + + public String getLocalNick() { + return localNick; + } + + public void setLocalNick(String localNick) { + this.localNick = localNick; + } + + public String getLocalAddress() { + return localAddress; + } + + public void setLocalAddress(String localAddress) { + this.localAddress = localAddress; + } + + public SocketAddress getRemoteAdress() { + return remoteAdress; + } + + public void setRemoteAdress(SocketAddress remoteAdress) { + this.remoteAdress = remoteAdress; + } + + public ServerSocket getServerSocket() { + return serverSocket; + } + + public void setServerSocket(ServerSocket serverSocket) { + this.serverSocket = serverSocket; + } + + public String getRemoteNick() { + return remoteNick; + } + + public void setRemoteNick(String remoteNick) { + this.remoteNick = remoteNick; + } + + public boolean isBusy() { + return busy; + } + + public void setBusy(boolean busy) { + this.busy = busy; + } +} + diff --git a/src/CallListenerThread.java b/src/CallListenerThread.java new file mode 100644 index 0000000..89448e9 --- /dev/null +++ b/src/CallListenerThread.java @@ -0,0 +1,88 @@ +import java.io.IOException; +import java.net.SocketAddress; +import java.util.Observable; +import java.util.concurrent.TimeUnit; + +public class CallListenerThread extends Observable implements Runnable { + private CallListener callListener; + private Caller.CallStatus callStatus; + private Connection connection; + private volatile boolean isOpen; + // TODO: Add lastEvent; + + public CallListenerThread() throws IOException { + callListener = new CallListener(); + } + + public CallListenerThread(String localNick) throws IOException { + callListener = new CallListener(localNick); + } + + public CallListenerThread(String localNick, String localIp) throws IOException { + callListener = new CallListener(localNick, localIp); + } + + public Caller.CallStatus getCallStatus() { + return callStatus; + } + + public SocketAddress getListenAddress() throws IOException { + return callListener.getListenAddress(); + } + + public String getLocalNick() { + return callListener.getLocalNick(); + } + + public SocketAddress getRemoteAddress() throws IOException { + return callListener.getRemoteAdress(); + } + + public Connection getConnection() { + return connection; + } + + + public boolean isBusy() { + return callListener.isBusy(); + } + + @Override + public void run() { + while (true) { + try { + connection = callListener.getConnection(); + if (connection == null) { + callStatus = Caller.CallStatus.valueOf("BUSY"); + } else { + callStatus = Caller.CallStatus.valueOf("OK"); + } + } catch (IOException e) { + System.out.println("SmthWrong"); + } + setChanged(); + notifyObservers(); + } + + } + + public void setBusy(boolean busy) { + callListener.setBusy(busy); + } + + + public void setLocalNick(String localNick) { + callListener.setLocalNick(localNick); + } + + public void start() { + this.isOpen = true; + Thread t = new Thread(this); + t.start(); + }; + + public void stop() { + this.isOpen = false; + } + +} \ No newline at end of file diff --git a/src/Caller.java b/src/Caller.java new file mode 100644 index 0000000..9225065 --- /dev/null +++ b/src/Caller.java @@ -0,0 +1,104 @@ +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; + +public class Caller { + + private String nick; + private int localport; + private InetAddress ip; + private String NickCaller; + public static enum CallStatus{OK, NOT_ACCESSIBLE, BUSY, REJECTED, NO_SERVICE}; + private CallStatus status; + private SocketAddress address; + + public Socket getSocket() { + return socket; + } + + public void setSocket(Socket socket) { + this.socket = socket; + } + + private Socket socket; + public Caller(){ + this("unnamed", new InetSocketAddress("127.0.0.1", 465)); + } + + public Caller(String localNick){ + this(localNick, new InetSocketAddress("127.0.0.1",465)); + } + + public Caller(String localNick, SocketAddress remoteAddress){ + this.nick=localNick; + this.address=remoteAddress; + } + + public Caller(String localNick, String ip){ + this(localNick, new InetSocketAddress(ip, 465)); + } + + public String getNick() { + return nick; + } + + public void setNick(String nick) { + this.nick = nick; + } + + public InetAddress getIp() { + return ip; + } + + public void setIp(InetAddress ip) { + this.ip = ip; + } + + public int getLocalport() { + return localport; + } + + public void setLocalport(int localport) { + this.localport = localport; + } + + public String getNickCaller() { + return NickCaller; + } + + public void setNickCaller(String nickCaller) { + NickCaller = nickCaller; + } + + public CallStatus getStatus() { + return status; + } + + public void setStatus(CallStatus status) { + this.status = status; + } + + public SocketAddress getAddress() { + return address; + } + + public void setAddress(SocketAddress address) { + this.address = address; + } + + public Connection call() throws InterruptedException { + String ip = address.toString(); // "/ip:port" + try { + Socket s = new Socket(); + s.connect(address, 1000); + return new Connection(s, nick); + } catch (IOException e) { + System.out.println("Not connected"); + return null; + } + } + + +} \ No newline at end of file diff --git a/src/ChatForm.form b/src/ChatForm.form new file mode 100644 index 0000000..7e93ce2 --- /dev/null +++ b/src/ChatForm.form @@ -0,0 +1,122 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/src/ChatForm.java b/src/ChatForm.java new file mode 100644 index 0000000..d1c29a3 --- /dev/null +++ b/src/ChatForm.java @@ -0,0 +1,168 @@ +import javax.swing.*; + +import javax.swing.table.DefaultTableModel; +import java.awt.event.*; +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.Vector; + + +public class ChatForm extends JFrame { + private JPanel rootPanel; + private JButton SendButton; + private JButton ButtonChangeLocalNick; + public static String LocalNick; + private JButton Disconnect; + private JButton Connect; + private JTextField textFieldIp; + private JTextField textFieldNick; + private JTextField textFieldLocalNick; + private JTextArea MessageStory; + private JTextArea MyText; + Connection connection = null; + + + Caller caller = null; + CallListener callListener = null; + CommandListenerThread commandListenerServer = null; + CommandListenerThread commandListenerClient = null; + + + + + + + public ChatForm() { + + super(); + setContentPane(rootPanel); + setSize(700, 500); + try { + Connect.setEnabled(false); + Disconnect.setEnabled(false); + textFieldIp.setEnabled(false); + textFieldNick.setEnabled(false); + + MyText.setEnabled(false); + SendButton.setEnabled(false); + }catch (NullPointerException e){ + + } + + + + try{ + Connect.addActionListener (e -> { + MyText.setEnabled(true); + if (connection != null) + try { + connection.SendNickHello( LocalNick); + } catch (IOException e1) { + e1.printStackTrace(); + } + else { + new Thread(() -> { + caller = new Caller(LocalNick, textFieldIp.getText()); + try { + try { + connection = caller.call(); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } catch (NullPointerException e1) { + e1.printStackTrace(); + } + commandListenerClient = new CommandListenerThread(connection); + try { + try { + connection.SendNickHello( LocalNick); + } catch (NullPointerException e1) { + e1.printStackTrace(); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + commandListenerClient.start(); + }).start(); + } + });}catch (NullPointerException e){ + + } + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + + + + + + //Change local nick and activate next field + ButtonChangeLocalNick.addActionListener(e -> { + if (!textFieldLocalNick.getText().isEmpty()) { + LocalNick = textFieldLocalNick.getText(); + Connect.setEnabled(true); + textFieldIp.setEnabled(true); + textFieldNick.setEnabled(true); + + + textFieldLocalNick.setEnabled(false); + ButtonChangeLocalNick.setEnabled(false); + + + Runnable runnable = new Runnable() { + @Override + public void run() { + + + try { + callListener = new CallListener(); + callListener.setLocalNick(LocalNick); + commandListenerServer = new CommandListenerThread(callListener.getConnection()); + + commandListenerServer.start(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + }; + new Thread(runnable).start(); + } + }); + + + SendButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String messenger = MyText.getText(); + + long curTime = System.currentTimeMillis(); + String time = new SimpleDateFormat("HH:mm:ss").format(curTime); + + MessageStory.append("\n" + " " + LocalNick + " " + time + ":" + "\n" + " " + messenger + "\n"); + try { + //callThread.getConnection().sendMessage(messenger); + if(caller!=null){ + callListener.getConnection().sendMessage(messenger);} + else{ + callListener.getConnection().sendMessage(messenger); + } + } catch (IOException e1) { + + e1.printStackTrace(); + } + } + }); + textFieldLocalNick.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyChar() == '\n') + ButtonChangeLocalNick.doClick(); + super.keyPressed(e); + } + }); + } + + + + +} \ No newline at end of file diff --git a/src/Command.java b/src/Command.java new file mode 100644 index 0000000..58325b1 --- /dev/null +++ b/src/Command.java @@ -0,0 +1,33 @@ +public class Command { + protected Command.CommandType type; + + Command(CommandType type) { + this.type = type; + } + + public enum CommandType { + ACCEPT, DISCONNECT, MESSAGE, NICK, REJECT + } + + public CommandType getType() { + + return type; + } + + static Command createCommand(String s) { + String text = s.toUpperCase(); + if (text.contains("ACCEPTED\n")) + return new Command(CommandType.ACCEPT); + else if (text.contains("REJECTED\n")) + return new Command(CommandType.REJECT); + else if (text.contains("DISCONNECT\n")) + return new Command(CommandType.DISCONNECT); + else if (text.contains("NICK\n")) + return new Command(CommandType.NICK); + else if (text.contains("MESSAGE\n")) + return new Command(CommandType.MESSAGE); + return null; + } + + +} \ No newline at end of file diff --git a/src/Connection.java b/src/Connection.java new file mode 100644 index 0000000..8d7d26e --- /dev/null +++ b/src/Connection.java @@ -0,0 +1,78 @@ +import java.io.*; +import java.net.Socket; +import java.util.Scanner; + + +public class Connection { + private Socket s; + private Scanner in; + private PrintWriter out; + public static final int inetPort = 465; + String nick; + + + public Connection(Socket socket, String localNick) throws IOException { + this.nick = localNick; + this.s = socket; + in = new Scanner(this.s.getInputStream(),"UTF-8"); + out = new PrintWriter(this.s.getOutputStream(), true); + } + + public boolean isOpen() { + return s != null; + } + + public boolean isConnected() { + return s.isConnected(); + } + + public void SendNickHello(String Nickname) throws IOException { + out.write(("ChatApp 2015 user " + Nickname + '\n')); + out.flush(); + } + + public void SendNickBusy(String Nickname) throws IOException { + out.write(("ChatApp 2015 user " + Nickname + " busy" + '\n')); + } + + public void sendMessage(String messege) throws IOException { + out.write((messege + '\n')); + out.flush(); + } + + public void accept() throws IOException { + out.write(("Accepted")); + out.write('\n'); + out.flush(); + } + + public void reject() throws IOException{ + out.write(("Rejected\n")); + out.flush(); + } + + public void closeSocket()throws IOException{ + s.close(); + } + + public void disconnect() throws IOException{ + out.write(("Disconnected\n")); + out.flush(); + + } + + public Command receive() throws IOException{ + return Command.createCommand(in.nextLine()); + } + + public String waitMessage(){ + StringBuilder stringBuilder =new StringBuilder(); + while (in.hasNextLine()) + stringBuilder.append(in.nextLine()); + return stringBuilder.toString(); + } + + + + +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..9114599 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,8 @@ +public class Main{ + + public static ChatForm form= new ChatForm(); + + public static void main(String[] args) { + form.setVisible(true); + } +} \ No newline at end of file From 405e56e29c246267bbe8f95f09df10c33376d1d7 Mon Sep 17 00:00:00 2001 From: NikitNikit Date: Wed, 9 Dec 2015 20:18:02 +0200 Subject: [PATCH 3/5] d --- src/CommandListenerThread.java | 49 ++++++++++++++++++++++++++++++++++ src/Server.java | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/CommandListenerThread.java create mode 100644 src/Server.java diff --git a/src/CommandListenerThread.java b/src/CommandListenerThread.java new file mode 100644 index 0000000..515cbdb --- /dev/null +++ b/src/CommandListenerThread.java @@ -0,0 +1,49 @@ +import java.io.IOException; +import java.util.Observable; + +/** + * Created by Никита on 19.11.2015. + */ +public class CommandListenerThread extends Observable implements Runnable { + + Command lastCommand; + Connection connection; + boolean stopped; +public CommandListenerThread(){ + +} + public CommandListenerThread(Connection con) { + connection = con; + } + + public Command getLastCommand() { + return lastCommand; + } + + public boolean isDisconnected() { + return stopped; + } + + public void start() { + run(); + } + + public void stop() { + stopped = true; + } + + public void run() { + do { + try { + Command checked = connection.receive(); + System.out.println(connection); + if (checked != null) { + lastCommand = checked; + notifyObservers(); + } + } catch (IOException ex) { + stopped = true; + } + } while (!stopped); + } +} \ No newline at end of file diff --git a/src/Server.java b/src/Server.java new file mode 100644 index 0000000..8c4d039 --- /dev/null +++ b/src/Server.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; + +class Server { + public static void main (String[] args) throws IOException { + System.out.println("Welcome to Server side"); + BufferedReader in = null; + ServerSocket servers = null; + Socket fromclient = null; + // create server socket + + try { + servers = new ServerSocket(28411); + } catch (IOException e) { + System.out.println("Couldn't listen to port 4444"); + System.exit(-1); + } + + try { + System.out.print("Waiting for a client..."); + fromclient= servers.accept(); + System.out.println("Client connected"); + } catch (IOException e) { + System.out.println("Can't accept"); + System.exit(-1); + } + InputStreamReader isp = new InputStreamReader(fromclient.getInputStream()); + in = new BufferedReader(isp); + + System.out.println("Wait for messages"); + String str = ""; + + while ((str = in.readLine()) != null) { + if (str.equals("exit")) break; + System.out.println(str); + System.out.println("port = " +fromclient.getPort()); + System.out.println("ip = " +fromclient.getInetAddress()); + } + in.close(); + fromclient.close(); + servers.close(); + } +} + + From e7a1a283d55b3c0d49d6af26e860f05f1b2d976b Mon Sep 17 00:00:00 2001 From: NikitNikit Date: Wed, 9 Dec 2015 20:20:29 +0200 Subject: [PATCH 4/5] vvv --- src/Server.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server.java b/src/Server.java index 8c4d039..73cd424 100644 --- a/src/Server.java +++ b/src/Server.java @@ -10,7 +10,7 @@ public static void main (String[] args) throws IOException { BufferedReader in = null; ServerSocket servers = null; Socket fromclient = null; - // create server socket + try { servers = new ServerSocket(28411); From e802a77b4453e80ff7c19949d3eba756d87d86ce Mon Sep 17 00:00:00 2001 From: NikitNikit Date: Wed, 9 Dec 2015 20:22:41 +0200 Subject: [PATCH 5/5] jj --- src/Server.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Server.java b/src/Server.java index 73cd424..e2b73be 100644 --- a/src/Server.java +++ b/src/Server.java @@ -6,7 +6,7 @@ class Server { public static void main (String[] args) throws IOException { - System.out.println("Welcome to Server side"); + BufferedReader in = null; ServerSocket servers = null; Socket fromclient = null;