-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagePeer.java
227 lines (208 loc) · 5.54 KB
/
ImagePeer.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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* @author abhin
* to implement a p2p file sharing network
* the class which creates each individual peer
*/
public class ImagePeer {
private JFrame j;
private JPanel im = new JPanel();
private PrintWriter w;
private Socket sock;
private ObjectInputStream oin;
private BufferedReader rea;
private Socket c;
private ObjectInputStream iin;
private ServerSocket s;
private static ArrayList<Peers> activepeers = new ArrayList<Peers>();
private static ArrayList<SubI> images = new ArrayList<SubI>();
/**
* @param args string arguments
* main method
*/
public static void main(String args[]) {
ImagePeer obj = new ImagePeer();
obj.execute();
}
@SuppressWarnings("unchecked")
private void execute() {
String ip = JOptionPane.showInputDialog("Connect to Server:");
try {
sock = new Socket(ip, 9000);
InputStreamReader ir = new InputStreamReader(sock.getInputStream());
rea = new BufferedReader(ir);
w = new PrintWriter(sock.getOutputStream());
oin = new ObjectInputStream(sock.getInputStream());
iin = new ObjectInputStream(sock.getInputStream());
String us = JOptionPane.showInputDialog("Username");
w.println(us);
w.flush();
String pa = JOptionPane.showInputDialog("Password");
w.println(pa);
w.flush();
String auth = rea.readLine();
if (auth.equals("false")) {
JOptionPane.showMessageDialog(null, "Login Fail");
} else {
creategui();
im.setSize(700, 700);
im.setLayout(new GridLayout(10, 10, 0, 0));
int pc = Integer.parseInt(rea.readLine());
activepeers = (ArrayList<Peers>) oin.readObject();
int portno = activepeers.get(pc).getRp();
//CreateServer server = new CreateServer(portno);
//server.start();
for (int i = 0; i < activepeers.size(); i++) {
if (i == pc)
continue;
Peers p = activepeers.get(i);
Thread t = new Thread(new Peerhandler(p));
t.start();
}
}
} catch (IOException e) {
System.exit(0);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* @author abhin
* Thread to handle input from each peer
*/
public class Peerhandler implements Runnable {
public Peerhandler(Peers p) {
try {
if (p.getRa().toString().equals("localhost/127.0.0.1") == false) {
Socket s = new Socket(p.getRa(), p.getRp());
iin = new ObjectInputStream(s.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public synchronized void run() {
try {
SubI o = (SubI) iin.readObject();
while (o != null) {
im.add(o.getLab());
im.repaint();
im.revalidate();
o = (SubI) iin.readObject();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @author abhin
* class that creates serversocket to write to peers
*/
public class CreateServer extends Thread {
private ServerSocket serversock;
private ArrayList<Linkedclient> connectedclients = new ArrayList<Linkedclient>();
/**
* @param port create server socket at this port
* @throws IOException
*/
public CreateServer(int port) throws IOException {
serversock = new ServerSocket(port);
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
try {
while (true) {
Linkedclient lc = new Linkedclient(serversock.accept(), this);
connectedclients.add(lc);
lc.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return list of linked peers
*/
public ArrayList<Linkedclient> getconnectedclients() {
return connectedclients;
}
/**
* @param a write to peers
*/
public synchronized void write(Linkedclient a) {
try {
for (int i = 0; i < images.size(); i++) {
a.getObjOut().writeObject(images.get(i));
a.getObjOut().flush();
TimeUnit.SECONDS.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @author abhin
* thread to write to peers
*/
public class Linkedclient extends Thread {
private CreateServer server;
private Socket socket;
private ObjectOutputStream objo;
/**
* @param socket socket to write to
* @param server socket to write from
*/
public Linkedclient(Socket socket, CreateServer server) {
this.server = server;
this.socket = socket;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
try {
objo = new ObjectOutputStream(socket.getOutputStream());
server.write(this);
} catch (Exception e) {
server.getconnectedclients().remove(this);
}
}
/**
* @return output stream to write to
*/
public ObjectOutputStream getObjOut() {
return objo;
}
}
/**
* creates the gui to display image
*/
private void creategui() {
j = new JFrame("Image Peer");
j.getContentPane().add(BorderLayout.CENTER, im);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(700, 700);
j.setVisible(true);
}
}