-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageServer.java
411 lines (379 loc) · 10.6 KB
/
ImageServer.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
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
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
* @author abhin
* To create a p2p netwrok for sile sharing
* This is the server class that initializes the network
*
*/
public class ImageServer {
private JFrame jf;
private JPanel ji;
private JPanel jb;
private JLabel temp;
private static ArrayList<User> Database = new ArrayList<User>();
private static ArrayList<SubI> imags = new ArrayList<SubI>();
private static ArrayList<Peers> peer = new ArrayList<Peers>();
private BufferedImage imgs[];
private Socket a;
private ServerSocket s;
private int peercount=0;
/**
* @param args string arguments
* @throws IOException
* @throws ParseException
*/
public static void main(String args[]) throws IOException, ParseException {
ImageServer obj = new ImageServer();
obj.execute();
}
private void execute() throws IOException, ParseException {
jf = new JFrame("Image Server");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setim();
jf.add(BorderLayout.NORTH, ji);
setbut();
jf.add(BorderLayout.CENTER, jb);
jf.pack();
jf.setVisible(true);
start();
}
private void start() throws IOException, ParseException {
File file = new File("./User.txt");
try {
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject) parser.parse(new FileReader(file));
JSONArray jar = (JSONArray) o.get("user_array");
Iterator<Object> iterator = jar.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj instanceof JSONObject) {
String u;
String p1;
String na;
String ea;
Long ph;
int w;
String d;
boolean l;
JSONObject json = (JSONObject) obj;
u = json.get("username").toString();
p1 = json.get("hash_password").toString();
na = json.get("Full Name").toString();
ea = json.get("Email").toString();
ph = Long.parseLong(json.get("Phone Number").toString());
w = Integer.parseInt(json.get("Fail Count").toString());
d = json.get("Last Login Date").toString();
l = Boolean.parseBoolean(json.get("Account Locked").toString());
User n = new User(u, p1, na, ea, ph, w, l, d);
Database.add(n);
}
}
go();
} catch (FileNotFoundException e) {
System.out.println("File does not exist");
System.exit(0);
}
}
private void go() {
try {
s = new ServerSocket(9000);
InetAddress ra = InetAddress.getByName("localhost");
int rp = s.getLocalPort();
Peers p = new Peers(ra, rp);
peer.add(p);
while (true) {
a = s.accept();
peercount++;
ra = a.getInetAddress();
rp = a.getPort();
p = new Peers(ra, rp);
peer.add(p);
Thread t = new Thread(new PeerHandler(a));
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author abhin
* Thread to handle each individual peer
*/
public class PeerHandler implements Runnable {
private BufferedReader r;
private Socket so;
private String username;
private String password;
private PrintWriter w;
private ObjectOutputStream oout;
private ObjectOutputStream iout;
/**
* @param peer the socket to which the peer connects to the server on
* Parameterized constructor
*/
public PeerHandler(Socket peer) {
try {
so = peer;
w = new PrintWriter(so.getOutputStream());
oout = new ObjectOutputStream(so.getOutputStream());
iout = new ObjectOutputStream(so.getOutputStream());
InputStreamReader i = new InputStreamReader(so.getInputStream());
r = new BufferedReader(i);
} catch (Exception e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
username = r.readLine();
password = r.readLine();
boolean au = authenti(Finduser(username));
if (!au) {
w.println("false");
w.flush();
} else {
w.println("true");
w.flush();
perform();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @throws IOException
* writes the image block by block to the peer
*/
public void perform() throws IOException {
try {
w.println(Integer.toString(peercount));
w.flush();
oout.writeObject(peer);
oout.flush();
for (int i = 0; i < imags.size(); i++) {
iout.writeObject(imags.get(i));
iout.flush();
TimeUnit.SECONDS.sleep(1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private User Finduser(String u) {
for (int i = 0; i < Database.size(); i++) {
User a = Database.get(i);
if (a.getUsername().equals(u)) {
return a;
}
}
User c = new User("notfound", "", "", "", 0);
return c;
}
private boolean authenti(User a) throws NoSuchAlgorithmException {
int rc = 0;// stores number of incorrect attempts
if (!a.isLocked()) {
String sb = a.hashthis(password);
String pa = a.getHpass();
if (pa.equals(sb)) {// successful
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
a.setLalgin(date);
a.setFailli(0);
return true;
} else {// failed attempt
rc++;
a.setFailli(rc);
if (a.getFailli() >= 3)
a.setLocked(true);
return false;
}
} else if (a.isLocked()) {
return false;
}
return false;
}
}
private String chooseim() {
JFileChooser f = new JFileChooser();
int result = f.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File fi = f.getSelectedFile();
return fi.getAbsolutePath();
} else {
return null;
}
}
private BufferedImage resize(BufferedImage img, int newW, int newH) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}
private void setim() {
File file = new File(chooseim());
FileInputStream fi = null;
try {
fi = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Failed to load image");
System.exit(0);
}
BufferedImage image = null;
try {
image = ImageIO.read(fi);
} catch (IOException e) {
System.out.println("Failed to load image");
System.exit(0);
}
image = resize(image, 700, 700);
splitim(image);
}
private void splitim(BufferedImage image) {
int c = 0;
BufferedImage[] imgs = new BufferedImage[100];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
imgs[c] = new BufferedImage(70, 70, image.getType());
Graphics2D g = imgs[c++].createGraphics();
g.drawImage(image, 0, 0, 70, 70, 70 * j, 70 * i, 70 * j + 70, 70 * i + 70, null);
g.dispose();
SubI a = new SubI();
a.setLoc((10 * i) + j);
imags.add(a);
}
}
ji = new JPanel();
ji.setLayout(new GridLayout(10, 10, 0, 0));
for (int i = 0; i < imgs.length; i++) {
temp = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().createImage(imgs[i].getSource())));
imags.get(i).setLab(temp);
temp.addMouseListener(new DragDropListener());
temp.addMouseMotionListener(new DragDropListener());
ji.add(temp);
}
}
/**
* @author abhin The user defined class which implements the overridden mouse
* listener methods
*/
class DragDropListener extends MouseAdapter implements MouseListener, MouseMotionListener {
int initx;
int inity;
JLabel j;
JLabel u;
/**
* Constructor
*/
DragDropListener() {
super();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
*
* @override
*/
public void mousePressed(MouseEvent e) {
j = (JLabel) e.getSource();
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseAdapter#mouseDragged(java.awt.event.MouseEvent)
*
* @override
*/
public void mouseDragged(MouseEvent e) {
}
/*
* (non-Javadoc)
*
* @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
*
* @override
*/
public void mouseReleased(MouseEvent e) {
initx = (j.getLocation().x);
inity = (j.getLocation().y);
int finx = e.getXOnScreen() - (e.getXOnScreen() % 80);
int finy = e.getYOnScreen() - (e.getYOnScreen() % 80) - 80;
int inloc = 10 * (inity / 80) + (initx / 80);
int finloc = (10 * (finy / 80) + (finx / 80));
if (finloc != imags.get(finloc).getLoc()) {
u = imags.get(finloc).getLab();
u.setLocation(initx, inity);
j.setLocation(finx, finy);
Collections.swap(imags, finloc, inloc);
} else
System.out.println("\nImage Block in Correct Position");
}
}
private void setbut() {
jb = new JPanel();
JButton b = new JButton("Load another image");
b.addActionListener(new LoadListener());
jb.add(b);
}
/**
* @author abhin
* to load the new image
*/
class LoadListener implements ActionListener {
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent a) {
try {
jf.setVisible(false);
jf.remove(ji);
setim();
} catch (Exception e) {
System.out.println("Failed to load new image, old image retained");
} finally {
jf.add(BorderLayout.NORTH, ji);
jf.setVisible(true);
}
}
}
}