Complete the Main message function of this client/server peer to peer chat progr
ID: 3672265 • Letter: C
Question
Complete the Main message function of this client/server peer to peer chat program with case statements for leave,put,reply,and request. Client and server should communicate.
import java.net.*;
import java.io.*;
import java.util.*;
/*****************************//**
* rief It implements a distributed chat.
* It creates a ring and delivers messages
* using flooding
**********************************/
public class Chat {
public enum MSG {
REQUEST,
REPLY,
LEAVE,
PUT
};
// General Message
public class MainMessage implements Serializable {
MSG messageID; // Id of the message REQUEST = 0,
//REPLY = 1, LEAVE =2, PUT = 3
int myPort;
String myIP;
String nextIP;
int nextPort;
String idSource;
String idDest;
String text;
/*
LEAVE : myIP, myPort, nextIP, nextPort
REQUEST : nextIP, nextPort
REPLY : myPort, nextIP
PUT : idSender, idDest, payload
*/
public MainMessage() {
}
}
/*****************************//**
* class Server class "chat.java"
* rief It implements the server
**********************************/
private class Server implements Runnable
{
int port;
public Server(int p)
{
port = p;
}
/*****************************//**
* rief It allows the system to interact with the participants.
**********************************/
public void run() {
ServerSocket servSock = new ServerSocket(port);
while (true)
{
Socket clntSock = servSock.accept(); // Get client connections
ObjectInputStream ois = new
ObjectInputStream(clntSock.getInputStream());
ObjectOutputStream oos = new
ObjectOutputStream(clntSock.getOutputStream());
MainMessage m = (MainMessage)ois.readObject();
// Handle Messages
clntSock.close();
}
}
}
/*****************************//**
* class Client class "chat.java"
* rief It implements the client
**********************************/
private class Client implements Runnable
{
String ip;
int port;
MainMessage m;
public Client(String ipAddress, int p)
{
port = p;
ip = ipAddress;
}
/*****************************//**
* rief It allows the user to interact with the system.
**********************************/
public void run()
{
while (true)
{
// Read commands form the keyboard
//Prepare message m
Socket socket = new Socket(ip, port);
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new
ObjectInputStream(socket.getInputStream());
oos.writeObject(m);
ois.read();
socket.close();
}
}
}
/*****************************//**
* Starts the threads with the client and server:
* param Id unique identifier of the process
* param port where the server will listen
**********************************/
public Chat(String Id, int port) {
// Initialization of the peer
Thread server = new Thread(new Server(port));
Thread client = new Thread(new Client(Id, port));
server.start();
client.start();
client.join();
server.join();
}
public static void main(String[] args) {
if (args.length < 2 ) {
throw new IllegalArgumentException("Parameter: ");
}
Chat chat = new Chat(args[0], Integer.parseInt(args[1]));
}
}
Explanation / Answer
Chat.java
/**
* Imports
*/
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* "Main" class Chat
*/
/**
* Performs the main functions of the program
*/
public class Chat {
/**
* Declarations
*/
static Semaphore semaThingy = new Semaphore(1);
String myId;
int myPort;
String myIP;
String nextIP;
int nextPort;
/**
* Class Server
*/
/**
* Handles server-side operations
*/
public class Server implements Runnable {
/**
* run() - Server version
*/
public void run() {
ServerSocket servSock = null;
/**
* Attempt to create new ServerSocket
*/
try {
servSock = new ServerSocket(myPort);
} catch (IOException e) {
e.printStackTrace();
}
/**
* Runs while the overall program is running
*/
while (true) {
MainMessage reply = null;
Socket clntSock;
/**
* Gets the users input
*/
try {
clntSock = servSock.accept();
ObjectInputStream ois = new ObjectInputStream(clntSock.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(clntSock.getOutputStream());
MainMessage m = (MainMessage) ois.readObject();
switch (m.messageID) {
case REQUEST:
System.out.println("'REQUEST' received: " + m.myPort);
{
try {
semaThingy.acquire();
reply = new MainMessage(nextPort, nextIP);
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
{
try {
semaThingy.acquire();
nextIP = m.myIP;
nextPort = m.myPort;
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
oos.writeObject(reply);
break;
case PUT:
System.out.println("'PUT' received from: " + m.idSource);
if ((myId).equals(m.idDest)) {
System.out.println(m.text);
} else if ((myId).equals(m.idSource)) {
System.out.println("Message was not received!");
//reply = new MainMessage(myPort, myIP);
//oos.writeObject(reply);
} else {
Socket socket;
try {
try {
semaThingy.acquire();
socket = new Socket(nextIP, nextPort);
semaThingy.release();
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
oos.writeObject(m);
socket.close();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case LEAVE:
System.out.println("'LEAVE' received: " + m.myPort);
if (myIP.equals(m.myIP) && myPort == m.myPort) {
try {
semaThingy.acquire();
nextIP = myIP;
nextPort = myPort;
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Successfully left chat");
return;
}
Socket socket;
try {
socket = new Socket(nextIP, nextPort);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
oos.writeObject(m);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
if (nextIP.equals(m.myIP) && nextPort == m.myPort) {
try {
semaThingy.acquire();
nextIP = m.nextIP;
nextPort = m.nextPort;
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
break;
default:
break;
}
clntSock.close();
} /**
* 2 catch statements for the potential errors
*/
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
/**
* Class Server
*/
/**
* Handles client-side operations
*/
public class Client implements Runnable {
/**
* run() - Client version
*/
public void run() {
/**
* Uses a scanner to get user input from the command line
*/
Scanner scanner = new Scanner(System.in);
/**
* Resets the variables
*/
MainMessage m = null;
String ip = null;
boolean createSocket = true;
int port = 0;
/**
* Runs while the overall program is running
*/
while (true) {
createSocket = true;
/**
* Get User input
*/
System.out.println("Enter your message (REQUEST, PUT, LEAVE): ");
String msgInput = scanner.next().toUpperCase();
/**
* Responds based on input
*/
switch (msgInput) {
case "REQUEST":
System.out.println("Input IP to connect to:");
ip = scanner.next();
System.out.println("Input port to connect to:");
port = scanner.nextInt();
System.out.println("Connecting on specified port: " + port);
System.out.println("Sending Request...");
m = new MainMessage(myIP, myPort);
break;
case "PUT":
String receiverId;
String msgToSend;
System.out.println("Input the recipient of message: ");
receiverId = scanner.next();
scanner.nextLine();
System.out.println("Input message: ");
msgToSend = scanner.nextLine();
m = new MainMessage(myId, receiverId, msgToSend);
break;
case "LEAVE":
System.out.println("Leaving chat");
m = new MainMessage(myIP, myPort, nextIP, nextPort);
break;
}
/**
* Runs when we want to create a new socket
*/
if (createSocket == true) {
Socket socket;
try {
if (m.messageID != MSG.REQUEST) {
try {
semaThingy.acquire();
ip = nextIP;
port = nextPort;
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
socket = new Socket(ip, port);//ip and port to connect to
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
oos.writeObject(m);
if (m.messageID == MSG.REQUEST) {
try {
System.out.println("Receiving Reply");
MainMessage received = (MainMessage) ois.readObject();
if (received.messageID == MSG.REPLY) {
System.out.println("IP: " + received.nextIP + " " + received.nextPort);
try {
semaThingy.acquire();
nextIP = received.nextIP;
nextPort = received.nextPort;
semaThingy.release();
} catch (InterruptedException ex) {
Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (msgInput == "LEAVE") {
return;
}
}
}
}
/**
*
* @param Id
* @param port
* @param Ip
*/
public Chat(String Id, int port, String Ip) {
myIP = Ip;
myPort = port;
myId = Id;
nextPort = port;
nextIP = Ip;
/**
* Initialises this part of the P2P network
*/
Thread server = new Thread(new Server());
Thread client = new Thread(new Client());
server.start();
client.start();
try {
client.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
server.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Main()
*
* @param args
*/
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("(err1)Parameter: <id> <port>");
return;
}
/**
* Attempts a connection
*/
try {
InetAddress ip = InetAddress.getLocalHost();
Chat chat = new Chat(args[0], Integer.parseInt(args[1]), ip.getHostAddress());
} catch (UnknownHostException p) {
System.out.println("(err2)Parameter: <id> <port>");
}
}
}
MSG.java
public enum MSG {
REQUEST, REPLY, LEAVE, PUT
}
MainMessage.java
import java.io.Serializable;
/**
* Class MainMessage
*/
public class MainMessage implements Serializable {
/**
* Declaration
*/
MSG messageID; // Id of the message REQUEST = 0, REPLY = 1, LEAVE =2, PUT = 3
int myPort;
String myIP;
String nextIP;
int nextPort;
String idSource;
String idDest;
String text;
/**
*
*/
public MainMessage() {
}
/**
* Used when user input is 'LEAVE'
* @param ip
* @param nxtIp
* @param port
* @param nxtPort
*/
public MainMessage(String ip, int port, String nxtIp, int nxtPort) {
messageID = MSG.LEAVE;
myIP = ip;
myPort = port;
nextIP = nxtIp;
nextPort = nxtPort;
}
/**
* Used when user input is 'REQUEST'
* @param ip
* @param port
*/
public MainMessage(String ip, int port) {
messageID = MSG.REQUEST;
myIP = ip;
myPort = port;
}
/**
* Used when user input is 'REPLY'
* @param port
* @param ip
*/
public MainMessage(int port, String ip) {
messageID = MSG.REPLY;
nextPort = port;
nextIP = ip;
}
/**
* Used when user input is 'PUT'
* @param idSrc
* @param idDes
* @param txt
*/
public MainMessage(String idSrc, String idDes, String txt) {
messageID = MSG.PUT;
idSource = idSrc;
idDest = idDes;
text = txt;
}
/**
*
* @param msg
* @param Ip
* @param Port
*/
public MainMessage(MSG msg, String Ip, int Port) {
this.messageID = msg;
myIP = Ip;
myPort = Port;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.