1 | package org.expeditee.network;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.OutputStream;
|
---|
8 | import java.net.ServerSocket;
|
---|
9 | import java.net.Socket;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.expeditee.gui.FrameIO;
|
---|
14 |
|
---|
15 | public class ImageSaver extends DefaultServer {
|
---|
16 |
|
---|
17 | public static boolean recvImage(File file, Socket socket) throws IOException {
|
---|
18 | InputStream is = socket.getInputStream();
|
---|
19 | // file length is the first 4 bytes
|
---|
20 | int fileLen = (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
|
---|
21 | // if the above reads failed, fileLen will be -1
|
---|
22 | if(fileLen <= 0) {
|
---|
23 | return false;
|
---|
24 | }
|
---|
25 | System.out.println("Receiving " + file.getName() + " (" + fileLen + " bytes)");
|
---|
26 | file.getParentFile().mkdirs();
|
---|
27 | FileOutputStream out = new FileOutputStream(file);
|
---|
28 | // file is the remaining bytes of the stream
|
---|
29 | byte[] buf = new byte[1024];
|
---|
30 | int r = -1;
|
---|
31 | int len = 0;
|
---|
32 | while((r = is.read(buf)) > 0) {
|
---|
33 | out.write(buf, 0, r);
|
---|
34 | len += r;
|
---|
35 | if(fileLen - len <= 0) {
|
---|
36 | break;
|
---|
37 | }
|
---|
38 | if(fileLen - len < 1024) {
|
---|
39 | buf = new byte[fileLen - len];
|
---|
40 | }
|
---|
41 | }
|
---|
42 | out.flush();
|
---|
43 | out.close();
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Reads an image storage request, and saves the image on the server
|
---|
49 | *
|
---|
50 | * @author jts21
|
---|
51 | *
|
---|
52 | */
|
---|
53 | private static class ImageReceiver extends Thread {
|
---|
54 |
|
---|
55 | private Socket socket;
|
---|
56 |
|
---|
57 | public ImageReceiver(Socket client) {
|
---|
58 | super("ImageReceiver");
|
---|
59 | this.socket = client;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void run() {
|
---|
64 | try {
|
---|
65 | InputStream is = this.socket.getInputStream();
|
---|
66 | OutputStream os = this.socket.getOutputStream();
|
---|
67 | // number of files is the first byte
|
---|
68 | int numFiles = is.read();
|
---|
69 | if(numFiles == -1)
|
---|
70 | return;
|
---|
71 | // read the filenames
|
---|
72 | File[] files = new File[numFiles];
|
---|
73 | List<Integer> wantFiles = new ArrayList<Integer>();
|
---|
74 | for(int i = 0; i < numFiles; i++) {
|
---|
75 | int fileNameLen = is.read();
|
---|
76 | byte[] fileName = new byte[fileNameLen];
|
---|
77 | is.read(fileName);
|
---|
78 | files[i] = new File(FrameIO.IMAGES_PATH + new String(fileName, ImageServer.CHARSET));
|
---|
79 | // pick out which files we want
|
---|
80 | if(!files[i].exists()) {
|
---|
81 | wantFiles.add(i);
|
---|
82 | os.write(i);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | // get the files
|
---|
86 | for(Integer i : wantFiles) {
|
---|
87 | recvImage(files[i], socket);
|
---|
88 | }
|
---|
89 | this.socket.close();
|
---|
90 | } catch (IOException e) {
|
---|
91 | e.printStackTrace();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public final static int OFFSET = 6;
|
---|
97 |
|
---|
98 | private ServerSocket socket;
|
---|
99 |
|
---|
100 | public ImageSaver(int port) throws IOException {
|
---|
101 | super("ImageSaver");
|
---|
102 | this.socket = new ServerSocket(port + OFFSET);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | protected void listenForMessages() throws IOException {
|
---|
107 | Socket client = socket.accept();
|
---|
108 | // should probably only allow a limited number of threads here
|
---|
109 | new ImageReceiver(client).start();
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public void run() {
|
---|
114 | //MessageBay.displayMessage(this.getName() + " started on port "
|
---|
115 | // + socket.getLocalPort());
|
---|
116 |
|
---|
117 | while (!_stop) {
|
---|
118 | try {
|
---|
119 | listenForMessages();
|
---|
120 |
|
---|
121 | } catch (IOException e) {
|
---|
122 | e.printStackTrace();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | try {
|
---|
126 | socket.close();
|
---|
127 | } catch (IOException e) {
|
---|
128 | e.printStackTrace();
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | }
|
---|