-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOurServer.java
More file actions
441 lines (424 loc) · 17.3 KB
/
OurServer.java
File metadata and controls
441 lines (424 loc) · 17.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.Date;
public class OurServer extends Thread
{
public int portNumber;
public static ServerSocket server;
public static BufferedReader in;
public static BufferedWriter out;
public static boolean flag;
public static Socket socket;
OurServer(int portNumber)
{
this.portNumber = portNumber;
if(!createServer())
System.out.println("Cannot start the server");
else
System.out.println("Server is running at port: " + portNumber);
}
public boolean createServer()
{
try
{
server = new ServerSocket(portNumber,3);
}
catch (IOException error)
{
error.getMessage();
System.exit(-1);
}
return true;
}
public OurServer(Socket soc)
{
socket = soc;
start();
}
public static void main (String [] args)throws IOException, InterruptedException
{
OurServer serv = new OurServer(4002);
while(true)
{
new OurServer(serv.server.accept()); //blocks waiting for a client to establish a connection
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println("Client connected");
}
}
public void run()
{
try
{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String result = VerifyLogin();
String[] resultStrings = result.split("\\s+");
while(true)
{
if(resultStrings[0].equals("success"))
{
out.write(resultStrings[0] + "\n");
out.flush();
String todo = in.readLine();
if(todo.equals("record"))
{
insertTrans(resultStrings[1]);
}
else if(todo.equals("forward"))
{
forwardTrans(resultStrings[1]);
}
else if(todo.equals("check"))
{
checkRequests(resultStrings[1]);
String k = checkRequests(resultStrings[1]);
serviceRequest(resultStrings[1], k);
insertTrans(resultStrings[1]);
}
else if(todo.equals("status"))
{
out.write(statusForward(resultStrings[1]) + "\n");
out.flush();
}
}
else
{
socket.close();
in.close();
out.close();
}
}
}
catch(IOException io)
{
System.err.println("Problem with server");
System.exit(1);
}
}
public static String statusForward(String user)
{
Statement stat = getMyStatement();
String status = null;
try
{
ResultSet se = stat.executeQuery("select status from exchange where request_from ='"+user+"'");
while(se.next())
{
status = se.getString(1);
}
}
catch(SQLException h){ h.getMessage(); }
return status;
}
public static void serviceRequest(String user, String id)
{
Statement stat = getMyStatement();
try
{
stat.executeUpdate("update exchange set status='served' where ID='"+id+"'");
}
catch(SQLException sq){ sq.getMessage(); }
}
public static String checkRequests(String user)throws IOException
{
Statement stat = getMyStatement();
String field = null;
try
{
ResultSet re = stat.executeQuery("select * from exchange where request_to='"+user+"' and status='pending' limit 1");
while(re.next())
{
String sendBack = re.getString(4) +" "+re.getString(5);
out.write(sendBack + "\n");
out.flush();
field = re.getString(1);
}
}
catch(SQLException sq){ sq.getMessage(); }
return field;
}
public static void forwardTrans(String user)throws IOException
{
Statement stat = getMyStatement();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String cmd = in.readLine();
String[] cmdArgs = cmd.split("\\s+");
System.out.println("Forwaded Transaction Recieved: " + cmd);
try
{
ResultSet t = stat.executeQuery("select agentName from kiosk where name='"+cmdArgs[3]+"'");
while(t.next())
{
String na = t.getString(1);
stat.execute("insert into exchange set request_from='"+user+"',customer_Num='"+cmdArgs[1]+"', amount='"+cmdArgs[2]+"',request_to='"+na+"',status='pending'");
}
}
catch(SQLException ss){ss.getMessage();}
}
public static void checkFloat()throws IOException
{
Statement stat = getMyStatement();
String checkCMD = in.readLine();
String[] cmds = checkCMD.split("\\s+");
if(cmds[0].equals("check"))
{
String compact = null;
if(cmds[1].equals("mtn"))
{
try
{
ResultSet resd = stat.executeQuery("select name,mtn_Float from kiosk");
System.out.println("Kiosk name\tmtn_Float");
while(resd.next())
{
compact = resd.getString(1) +" " + resd.getString(2);
System.out.println(compact);
}
}
catch(SQLException hh){hh.getMessage();}
}
else
{
try
{
ResultSet resd = stat.executeQuery("select name,warid_Float from kiosk");
System.out.println("Kiosk name\twarid_Float");
while(resd.next())
{
System.out.println(resd.getString(1)+"\t\t"+resd.getString(2));
}
}
catch(SQLException hh){hh.getMessage();}
}
}
}
public static void insertTrans(String user)throws IOException
{
String[] arr = new String[3];
for(int i = 0; i<3;i++)
{
arr[i] = in.readLine();
String[] cmdArgs = arr[i].split("\\s+");
System.out.println("Recieved: " + arr[i]);
String theTime = new Date().toString();
Statement stat = getMyStatement();
if(cmdArgs[0].equals("commit"))
{
if((cmdArgs[2].startsWith("075") && Integer.parseInt(cmdArgs[3])<5000000)||(cmdArgs[2].startsWith("078") && Integer.parseInt(cmdArgs[3])<4000000))
{
try
{
ResultSet ress = stat.executeQuery("select mtn_Num,warid_Num,mtn_Float,mtn_Cash,warid_Float,warid_Cash from kiosk where agentName='"+user+"'");
if(cmdArgs[2].startsWith("078"))
{
int com = 0;
if(cmdArgs[1].equals("deposit"))
{
com=getMtnCommission_dep(Integer.parseInt(cmdArgs[3]));
while(ress.next())
{
String no = ress.getString(1);
int floa = ress.getInt(3)-Integer.parseInt(cmdArgs[3]);
int cash = ress.getInt(4)+Integer.parseInt(cmdArgs[3]);
stat.execute("insert into transaction set type='"+cmdArgs[1]+"',customer_num='"+cmdArgs[2]+"',amount='"+cmdArgs[3]+"',commission='"+com+"',agent_name='"+user+"',status='served',date_time='"+theTime+"',kiosk_fonNum='"+no+"'");
stat.execute("update kiosk set mtn_Float="+floa+", mtn_Cash="+cash+" where agentName='"+user+"'");
}
}
else
{
com=getMtnCommission_with(Integer.parseInt(cmdArgs[3]));
while(ress.next())
{
String no = ress.getString(1);
int floa = Integer.parseInt(ress.getString(3))+Integer.parseInt(cmdArgs[3]);
int cash = Integer.parseInt(ress.getString(4))-Integer.parseInt(cmdArgs[3]);
stat.execute("insert into transaction set type='"+cmdArgs[1]+"',customer_num='"+cmdArgs[2]+"',amount='"+cmdArgs[3]+"',commission='"+com+"',agent_name='"+user+"',status='served',date_time='"+theTime+"',kiosk_fonNum='"+no+"'");
stat.execute("update kiosk set mtn_Float="+floa+", mtn_Cash="+cash+" where agentName='"+user+"'");
}
}
}
else
{
int com = 0;
if(cmdArgs[1].equals("deposit"))
{
com=getAirtelCommission_dep(Integer.parseInt(cmdArgs[3]));
while(ress.next())
{
String no = ress.getString(2);
int floa = ress.getInt(5)-Integer.parseInt(cmdArgs[3]);
int cash = ress.getInt(6)+Integer.parseInt(cmdArgs[3]);//kiosk_fonNum='"+ress.getString(1)+"'
stat.execute("insert into transaction set type='"+cmdArgs[1]+"',customer_num='"+cmdArgs[2]+"',amount='"+cmdArgs[3]+"',commission='"+com+"',agent_name='"+user+"',status='served',date_time='"+theTime+"',kiosk_fonNum='"+no+"'");
stat.execute("update kiosk set warid_Float="+floa+", warid_Cash="+cash+" where agentName='"+user+"'");
}
}
else
{
com=getAirtelCommission_with(Integer.parseInt(cmdArgs[3]));
while(ress.next())
{
String no = ress.getString(2);
int floa = Integer.parseInt(ress.getString(5))+Integer.parseInt(cmdArgs[3]);
int cash = Integer.parseInt(ress.getString(6))-Integer.parseInt(cmdArgs[3]);
stat.execute("insert into transaction set type='"+cmdArgs[1]+"',customer_num='"+cmdArgs[2]+"',amount='"+cmdArgs[3]+"',commission='"+com+"',agent_name='"+user+"',status='served',date_time='"+theTime+"',kiosk_fonNum='"+no+"'");
stat.execute("update kiosk set warid_Float="+floa+", warid_Cash="+cash+" where agentName='"+user+"'");
}
}
}
}
catch(SQLException sq){sq.getMessage();}
}
}
else if(cmdArgs[0].equals("forward"))
{
//forwardTrans();
}
}
}
public static int getMtnCommission_dep(int amount)
{
int commission = 0;
if(amount>=500 && amount<=5000)
commission = 0;
else if(amount>5000 && amount<=60000)
commission = 285;
else if(amount>60000 && amount<=125000)
commission = 440;
else if(amount>125000 && amount<=250000)
commission = 520;
else if(amount>250000 && amount<=500000)
commission = 850;
else if(amount>500000 && amount<=1000000)
commission = 2500;
else if(amount>1000000 && amount<=2000000)
commission = 4500;
else if(amount>2000000 && amount<=4000000)
commission = 8000;
return commission;
}
public static int getMtnCommission_with(int amount)
{
int commission = 0;
if(amount>=500 && amount<=2500)
commission = 100;
else if(amount>2500 && amount<=5000)
commission = 125;
else if(amount>5000 && amount<=15000)
commission = 450;
else if(amount>15000 && amount<=30000)
commission = 500;
else if(amount>30000 && amount<=45000)
commission = 525;
else if(amount>45000 && amount<=60000)
commission = 575;
else if(amount>60000 && amount<=125000)
commission = 700;
else if(amount>125000 && amount<=250000)
commission = 1300;
else if(amount>250000 && amount<=500000)
commission = 2600;
else if(amount>500000 && amount<=1000000)
commission = 5000;
else if(amount>1000000 && amount<=2000000)
commission = 7500;
else if(amount>2000000 && amount<=4000000)
commission = 12500;
return commission;
}
public static int getAirtelCommission_dep(int amount)
{
int commission = 0;
if(amount>=500 && amount<=5000)
commission = 150;
else if(amount>5000 && amount<=60000)
commission = 285;
else if(amount>60000 && amount<=125000)
commission = 440;
else if(amount>125000 && amount<=250000)
commission = 520;
else if(amount>250000 && amount<=500000)
commission = 850;
else if(amount>500000 && amount<=1000000)
commission = 2500;
else if(amount>1000000 && amount<=2000000)
commission = 4500;
else if(amount>2000000 && amount<=4000000)
commission = 8000;
else if(amount>4000000 && amount<=5000000)
commission = 9000;
return commission;
}
public static int getAirtelCommission_with(int amount)
{
int commission = 0;
if(amount>=500 && amount<=2500)
commission = 100;
else if(amount>2500 && amount<=5000)
commission = 125;
else if(amount>5000 && amount<=15000)
commission = 450;
else if(amount>15000 && amount<=30000)
commission = 500;
else if(amount>30000 && amount<=45000)
commission = 525;
else if(amount>45000 && amount<=60000)
commission = 575;
else if(amount>60000 && amount<=125000)
commission = 700;
else if(amount>125000 && amount<=250000)
commission = 1300;
else if(amount>250000 && amount<=500000)
commission = 2600;
else if(amount>500000 && amount<=1000000)
commission = 5000;
else if(amount>1000000 && amount<=2000000)
commission = 7500;
else if(amount>2000000 && amount<=4000000)
commission = 12500;
else if(amount>4000000 && amount<=5000000)
commission = 15000;
return commission;
}
public static String VerifyLogin()throws IOException
{
Statement stat_2 = getMyStatement();
String name = in.readLine();
System.out.println("Recieved username: " + name);
try
{
int count = 0;
ResultSet user = stat_2.executeQuery("select * from user where username='"+name+"' and status='agent'");
while(user.next())
{
++count;
}
if(count==1)
return "success "+name+"";
else
{
return "failure";
}
}
catch(SQLException sq){sq.getMessage();}
return "failure";
}
public static Statement getMyStatement()
{
Statement theState = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/mobile";
Connection conn = DriverManager.getConnection(url,"root","");
theState = conn.createStatement();
}
catch(ClassNotFoundException | SQLException var)
{
System.out.println("ERROR LOADING DRIVER " + var.getMessage());
}
return theState;
}
}