Skip to content

Commit 381db33

Browse files
committed
fully implemented chat room that the admin can only send the message and the members can listen to them
1 parent 2151528 commit 381db33

File tree

12 files changed

+106
-22
lines changed

12 files changed

+106
-22
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/AdminClient/AdminClient.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AdminClient {
1111
public static final int BROADCAST_PORT = 3001;
1212
public static final String Section_1 = "1";
1313
public static final String Section_2 = "2";
14-
// public static boolean end = false;
14+
1515

1616
public static void main(String[] args) throws IOException {
1717
System.out.println("Welcome, Which group do you want to broadcast your\n" +
@@ -27,14 +27,19 @@ public static void main(String[] args) throws IOException {
2727
out.println(groupSectionID);
2828
String messageInfo = null;
2929
String messageContent = null;
30-
if ((messageInfo = massageReader.readLine()) != null) {
30+
while ((messageInfo = massageReader.readLine()) != null) {
3131
System.out.println(messageInfo);
3232
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
3333
messageContent = consoleReader.readLine();
34+
if (!"end".equals(messageContent)) {
35+
out.println(messageContent);
36+
37+
}else{
38+
out.println(messageContent);
39+
break;
40+
}
3441
}
35-
while (!"end".equals(messageContent)) {
36-
out.println(messageContent);
37-
}
42+
3843
scanner.close();
3944
massageReader.close();
4045
out.close();

src/MemberClient/MemberClient.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.io.PrintWriter;
7+
import java.net.DatagramPacket;
8+
import java.net.DatagramSocket;
79
import java.net.Socket;
810
import java.util.Scanner;
911

@@ -20,19 +22,35 @@ public static void main(String[] args) throws IOException {
2022
String groupSectionID = (scanner.nextInt()) == 1 ? Section_1 : Section_2;
2123

2224
Socket s = new Socket("localhost", SUBSCRIBE_PORT);
23-
2425
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
2526
Scanner messageScanner = new Scanner(new InputStreamReader(s.getInputStream()));
27+
Scanner ID =null;
2628
out.println(groupSectionID);
2729
String stateMassage = messageScanner.next();
2830

2931
if (stateMassage.equals("SUCCESS")) {
30-
31-
String message = messageScanner.nextLine();
32+
String message = messageScanner.nextLine();
3233
System.out.println(message);
33-
while(true){
34+
if(groupSectionID.equals("1")){
3435

36+
ID = new Scanner(message).useDelimiter("\\s*You are successfully added to the group Section-1 with ID =\\s*");
37+
}else{
38+
ID = new Scanner(message).useDelimiter("\\s*You are successfully added to the group Section-2 with ID =\\s*");
39+
}
40+
int clientID = ID.nextInt();
41+
DatagramSocket serverSocket = new DatagramSocket(clientID);
42+
43+
44+
while (true){
45+
byte[] receiveData = new byte[1024];
46+
// prepare packet container to receive the reply from the server
47+
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
48+
// wait to receive the reply
49+
serverSocket.receive(receivePacket);
50+
String messageContent = new String(receivePacket.getData());
51+
System.out.println("FROM Admin:" + messageContent);
3552
}
53+
// serverSocket.close();
3654

3755
}else{
3856
String message = messageScanner.nextLine();

src/Server/Server.java

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.io.PrintWriter;
7-
import java.net.ServerSocket;
8-
import java.net.Socket;
7+
import java.net.*;
98
import java.util.ArrayList;
109

1110

@@ -19,11 +18,20 @@ public class Server {
1918

2019
public static void main(String[] args){
2120
System.out.println("The server started .. ");
21+
/**
22+
* in this server chat we assume that we have only two groups section-1 and section-2 and each section can handel
23+
* a maximum member of 3 and here we initialize the group array list.the member client allow only to choose the
24+
* one group and listen to what will the admin send to this groups
25+
*/
2226
ArrayList<Members> groupSection_1 = new ArrayList<>();
2327
ArrayList<Members> groupSection_2 = new ArrayList<>();
2428
Groups Section_1 = new Groups("Section_1",groupSection_1),Section_2=new Groups("Section_2",groupSection_2) ;
2529
groupsArrayList.add(Section_1);
2630
groupsArrayList.add(Section_2);
31+
/**
32+
* we use this thread for subscription to either one of the two sections, and in the constructor we pass
33+
* total client number + 600 , the groupe array list to choose between the 2 sections
34+
*/
2735
new Thread() {
2836
public void run() {
2937
try {
@@ -36,13 +44,16 @@ public void run() {
3644
}
3745
}
3846
}.start();
39-
47+
/**
48+
* we use this thread fro broadcasting the message from the admins member to the group that they will
49+
* choose to send their message to.
50+
*/
4051
new Thread() {
4152
public void run() {
4253
try {
4354
ServerSocket ss = new ServerSocket(BROADCAST_PORT);
4455
while (true) {
45-
new BROADCAST_Service(ss.accept()).start();
56+
new BROADCAST_Service(ss.accept(),groupsArrayList).start();
4657
}
4758
} catch (IOException e) {
4859
e.printStackTrace();
@@ -52,25 +63,68 @@ public void run() {
5263
}
5364
private static class BROADCAST_Service extends Thread{
5465
Socket socket;
55-
56-
public BROADCAST_Service(Socket socket) {
66+
ArrayList<Groups> groupsArrayList = new ArrayList<>();
67+
public BROADCAST_Service(Socket socket,ArrayList<Groups> groupsArrayList) {
5768
this.socket = socket;
69+
this.groupsArrayList=groupsArrayList;
5870
}
5971

6072
public void run() {
6173
try {
6274
PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
6375
BufferedReader groupBR = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
6476
String groupSectionID = groupBR.readLine();
65-
if (groupSectionID.equals("1")) {
77+
DatagramSocket clientSocket = new DatagramSocket();
78+
while (groupSectionID.equals("1")) {
6679
String str = "What is the message to be broadcast?";
6780
out.println(str);
6881
String message = null;
69-
while((message=groupBR.readLine())!= null){
70-
System.out.println(message);
71-
82+
Groups Section1 =groupsArrayList.get(0);
83+
System.out.println("Sending data to all client in this group");
84+
if ((message=groupBR.readLine())!= null && !message.equals("end")){
85+
System.out.println(message);
86+
// convert to byte
87+
byte[] sendData = new byte[1024];
88+
sendData=message.getBytes();
89+
// get the IP Address of the server
90+
InetAddress IPAddress = InetAddress.getByName("localhost");
91+
for (int i=0; i<Section1.membersArrayList.size() ;i++){
92+
int clientID= Section1.membersArrayList.get(i).getID();
93+
//create packet
94+
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientID);
95+
clientSocket.send(sendPacket);
96+
}
97+
}else{
98+
break;
7299
}
73100
}
101+
while (groupSectionID.equals("2")) {
102+
String str = "What is the message to be broadcast?";
103+
out.println(str);
104+
String message = null;
105+
Groups Section2 =groupsArrayList.get(1);
106+
System.out.println("Sending data to all client in this group");
107+
if ((message=groupBR.readLine())!= null && !message.equals("end")){
108+
System.out.println(message);
109+
// convert to byte
110+
byte[] sendData = new byte[1024];
111+
sendData=message.getBytes();
112+
// get the IP Address of the server
113+
InetAddress IPAddress = InetAddress.getByName("localhost");
114+
for (int i=0; i<Section2.membersArrayList.size() ;i++){
115+
int clientID= Section2.membersArrayList.get(i).getID();
116+
//create packet
117+
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientID);
118+
clientSocket.send(sendPacket);
119+
}
120+
}else{
121+
break;
122+
}
123+
}
124+
out.close();
125+
groupBR.close();
126+
socket.close();
127+
clientSocket.close();
74128
} catch (IOException e) {
75129
e.printStackTrace();
76130
}
@@ -86,7 +140,7 @@ public SUBSCRIBE_Service(Socket socket, int clientID, ArrayList<Groups> groupsAr
86140
this.socket = socket;
87141
this.clientID = clientID;
88142
this.groupsArrayList=groupsArrayList;
89-
System.out.println("Connection with Client #" + clientID + "at socket " + socket);
143+
System.out.println("Subscribe Service Connection with Client #" + clientID + "at socket " + socket);
90144
}
91145

92146
public void run() {
@@ -103,6 +157,9 @@ public void run() {
103157
if (State) {
104158
String str = "SUCCESS You are successfully added to the group Section-1 with ID =" + clientID;
105159
out.println(str);
160+
while(true){
161+
162+
}
106163

107164
}else{
108165
String str = "FAILURE Sorry, the group reached its maximum count";
@@ -116,6 +173,9 @@ public void run() {
116173
if (State) {
117174
String str = "SUCCESS You are successfully added to the group Section-2 with ID =" + clientID;
118175
out.println(str);
176+
while(true){
177+
178+
}
119179
}else{
120180
String str = "FAILURE Sorry, the group reached its maximum count";
121181
out.println(str);
@@ -132,7 +192,9 @@ public void run() {
132192
}
133193
}
134194

135-
195+
/**
196+
* the server contain two inner class that it is implemented as required from the PDF
197+
*/
136198
public static final class Members {
137199
int ID;
138200

@@ -157,7 +219,6 @@ public Groups(String name, ArrayList<Members> membersArrayList) {
157219
public boolean addMember(Members M){
158220
if (membersArrayList.size() != MaximumNumber){
159221
membersArrayList.add(M);
160-
System.out.println(membersArrayList.size());
161222
return true;
162223
}
163224
return false;

0 commit comments

Comments
 (0)