4
4
import java .io .IOException ;
5
5
import java .io .InputStreamReader ;
6
6
import java .io .PrintWriter ;
7
- import java .net .ServerSocket ;
8
- import java .net .Socket ;
7
+ import java .net .*;
9
8
import java .util .ArrayList ;
10
9
11
10
@@ -19,11 +18,20 @@ public class Server {
19
18
20
19
public static void main (String [] args ){
21
20
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
+ */
22
26
ArrayList <Members > groupSection_1 = new ArrayList <>();
23
27
ArrayList <Members > groupSection_2 = new ArrayList <>();
24
28
Groups Section_1 = new Groups ("Section_1" ,groupSection_1 ),Section_2 =new Groups ("Section_2" ,groupSection_2 ) ;
25
29
groupsArrayList .add (Section_1 );
26
30
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
+ */
27
35
new Thread () {
28
36
public void run () {
29
37
try {
@@ -36,13 +44,16 @@ public void run() {
36
44
}
37
45
}
38
46
}.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
+ */
40
51
new Thread () {
41
52
public void run () {
42
53
try {
43
54
ServerSocket ss = new ServerSocket (BROADCAST_PORT );
44
55
while (true ) {
45
- new BROADCAST_Service (ss .accept ()).start ();
56
+ new BROADCAST_Service (ss .accept (), groupsArrayList ).start ();
46
57
}
47
58
} catch (IOException e ) {
48
59
e .printStackTrace ();
@@ -52,25 +63,68 @@ public void run() {
52
63
}
53
64
private static class BROADCAST_Service extends Thread {
54
65
Socket socket ;
55
-
56
- public BROADCAST_Service (Socket socket ) {
66
+ ArrayList < Groups > groupsArrayList = new ArrayList <>();
67
+ public BROADCAST_Service (Socket socket , ArrayList < Groups > groupsArrayList ) {
57
68
this .socket = socket ;
69
+ this .groupsArrayList =groupsArrayList ;
58
70
}
59
71
60
72
public void run () {
61
73
try {
62
74
PrintWriter out = new PrintWriter (this .socket .getOutputStream (), true );
63
75
BufferedReader groupBR = new BufferedReader (new InputStreamReader (this .socket .getInputStream ()));
64
76
String groupSectionID = groupBR .readLine ();
65
- if (groupSectionID .equals ("1" )) {
77
+ DatagramSocket clientSocket = new DatagramSocket ();
78
+ while (groupSectionID .equals ("1" )) {
66
79
String str = "What is the message to be broadcast?" ;
67
80
out .println (str );
68
81
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 ;
72
99
}
73
100
}
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 ();
74
128
} catch (IOException e ) {
75
129
e .printStackTrace ();
76
130
}
@@ -86,7 +140,7 @@ public SUBSCRIBE_Service(Socket socket, int clientID, ArrayList<Groups> groupsAr
86
140
this .socket = socket ;
87
141
this .clientID = clientID ;
88
142
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 );
90
144
}
91
145
92
146
public void run () {
@@ -103,6 +157,9 @@ public void run() {
103
157
if (State ) {
104
158
String str = "SUCCESS You are successfully added to the group Section-1 with ID =" + clientID ;
105
159
out .println (str );
160
+ while (true ){
161
+
162
+ }
106
163
107
164
}else {
108
165
String str = "FAILURE Sorry, the group reached its maximum count" ;
@@ -116,6 +173,9 @@ public void run() {
116
173
if (State ) {
117
174
String str = "SUCCESS You are successfully added to the group Section-2 with ID =" + clientID ;
118
175
out .println (str );
176
+ while (true ){
177
+
178
+ }
119
179
}else {
120
180
String str = "FAILURE Sorry, the group reached its maximum count" ;
121
181
out .println (str );
@@ -132,7 +192,9 @@ public void run() {
132
192
}
133
193
}
134
194
135
-
195
+ /**
196
+ * the server contain two inner class that it is implemented as required from the PDF
197
+ */
136
198
public static final class Members {
137
199
int ID ;
138
200
@@ -157,7 +219,6 @@ public Groups(String name, ArrayList<Members> membersArrayList) {
157
219
public boolean addMember (Members M ){
158
220
if (membersArrayList .size () != MaximumNumber ){
159
221
membersArrayList .add (M );
160
- System .out .println (membersArrayList .size ());
161
222
return true ;
162
223
}
163
224
return false ;
0 commit comments