forked from AdvancedProgramming1404/HW-10-Socket-Programming
implemet Server Listener + chat server + part of client session
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
|
import com.university.chat.Common.ChatMessage;
|
||||||
|
import com.university.chat.Common.FileMessage;
|
||||||
|
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
|
|
||||||
public class ServerListener implements Runnable{
|
public class ServerListener implements Runnable{
|
||||||
|
private ObjectInputStream in ;
|
||||||
public ServerListener(ObjectInputStream in) {
|
public ServerListener(ObjectInputStream in) {
|
||||||
|
this.in = in ;
|
||||||
}
|
}
|
||||||
// TODO: store the ObjectInputStream from the user socket
|
// TODO: store the ObjectInputStream from the user socket
|
||||||
// (this should be the same input stream the
|
// (this should be the same input stream the
|
||||||
@@ -12,6 +17,18 @@ public class ServerListener implements Runnable{
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
Object obj =in.readObject() ;
|
||||||
|
if (obj instanceof ChatMessage)
|
||||||
|
{
|
||||||
|
ChatMessage msg = (ChatMessage) obj ;
|
||||||
|
System.out.println("from : " + msg.getSender() + " ->" +msg.getContent() );
|
||||||
|
}
|
||||||
|
else if(obj instanceof FileMessage)
|
||||||
|
{
|
||||||
|
FileMessage fmsg = (FileMessage) obj ;
|
||||||
|
System.out.println("from : " + fmsg.getSender() + "-> File Name : " + fmsg.getFilename());
|
||||||
|
|
||||||
|
}
|
||||||
// TODO: In an infinite loop read objects from the server
|
// TODO: In an infinite loop read objects from the server
|
||||||
// - if it's a ChatMessage -> print "<sender>: <content>"
|
// - if it's a ChatMessage -> print "<sender>: <content>"
|
||||||
// - if it's a FileMessage -> print that a file was received
|
// - if it's a FileMessage -> print that a file was received
|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
package com.university.chat.Server;
|
package com.university.chat.Server;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
public class ChatServer {
|
public class ChatServer {
|
||||||
// TODO: declare a single shared UserManager instance (static final)
|
// TODO: declare a single shared UserManager instance (static final)
|
||||||
|
private static final UserManager usermanager = new UserManager() ;
|
||||||
// This MUST be shared by all ClientSession threads so that
|
// This MUST be shared by all ClientSession threads so that
|
||||||
// broadcasting and private messaging work correctly.
|
// broadcasting and private messaging work correctly.
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws IOException {
|
||||||
// TODO: Create a ServerSocket
|
// TODO: Create a ServerSocket
|
||||||
|
ServerSocket serverSocket = new ServerSocket(8080) ;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Socket socket = serverSocket.accept() ;
|
||||||
|
ClientSession clientSession = new ClientSession(socket , usermanager) ;
|
||||||
|
Thread thread = new Thread(clientSession) ;
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: In an infinite loop:
|
// TODO: In an infinite loop:
|
||||||
// accept an incoming client connection
|
// accept an incoming client connection
|
||||||
|
|||||||
@@ -2,23 +2,51 @@ package com.university.chat.Server;
|
|||||||
|
|
||||||
import com.university.chat.Common.ChatMessage;
|
import com.university.chat.Common.ChatMessage;
|
||||||
import com.university.chat.Common.FileMessage;
|
import com.university.chat.Common.FileMessage;
|
||||||
|
import com.university.chat.Common.MessageType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
public class ClientSession implements Runnable {
|
public class ClientSession implements Runnable {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
private ObjectInputStream in;
|
||||||
|
private ObjectOutputStream out ;
|
||||||
|
private Socket socket ;
|
||||||
|
private UserManager userManager ;
|
||||||
|
|
||||||
public ClientSession(Socket socket, UserManager userManager) {
|
public ClientSession(Socket socket, UserManager userManager) throws IOException {
|
||||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||||
// and an ObjectInputStream from socket.getInputStream().
|
// and an ObjectInputStream from socket.getInputStream().
|
||||||
|
this.socket = socket ;
|
||||||
|
this.userManager = userManager ;
|
||||||
|
this.out = new ObjectOutputStream(socket.getOutputStream()) ;
|
||||||
|
out.flush();
|
||||||
|
this.in = new ObjectInputStream(socket.getInputStream()) ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
Object obj = in.readObject() ;
|
||||||
|
if (obj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN)
|
||||||
|
{
|
||||||
|
this.username = loginmsg.getSender() ;
|
||||||
|
if (userManager.addUser(username , this))
|
||||||
|
{
|
||||||
|
FileManager.createUserFolders(username);
|
||||||
|
out.writeObject(new ChatMessage(MessageType.LOGIN_SUCCESS, "SERVER", username, "Welcome " + username));
|
||||||
|
out.flush();
|
||||||
|
System.out.println("User logged in successfully: " + username);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
// TODO: Welcome the user (login step)
|
||||||
// 1. Read the first object sent by the client.
|
// 1. Read the first object sent by the client.
|
||||||
|
|||||||
Reference in New Issue
Block a user