1 | /**
|
---|
2 | * ImageServer.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.network;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileInputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.io.OutputStream;
|
---|
26 | import java.net.ServerSocket;
|
---|
27 | import java.net.Socket;
|
---|
28 |
|
---|
29 | import org.expeditee.gui.FrameIO;
|
---|
30 |
|
---|
31 | public class ImageServer extends DefaultServer {
|
---|
32 |
|
---|
33 | public static void sendImage(File file, Socket socket) throws IOException {
|
---|
34 | if(!file.isFile()) {
|
---|
35 | System.out.println("Could not find " + file.getName());
|
---|
36 | return;
|
---|
37 | }
|
---|
38 | OutputStream os = socket.getOutputStream();
|
---|
39 | // send the file over the socket
|
---|
40 | int fileLen = (int) file.length();
|
---|
41 | System.out.println("Sending " + file.getName() + " (" + fileLen + " bytes)");
|
---|
42 | byte[] fileLenBytes = new byte[4];
|
---|
43 | fileLenBytes[0] = (byte) ((fileLen >> 24) & 0xFF);
|
---|
44 | fileLenBytes[1] = (byte) ((fileLen >> 16) & 0xFF);
|
---|
45 | fileLenBytes[2] = (byte) ((fileLen >> 8) & 0xFF);
|
---|
46 | fileLenBytes[3] = (byte) ((fileLen) & 0xFF);
|
---|
47 | os.write(fileLenBytes);
|
---|
48 | FileInputStream in = new FileInputStream(file);
|
---|
49 | byte[] buf = null;
|
---|
50 | if(in.available() > 1024) {
|
---|
51 | buf = new byte[1024];
|
---|
52 | while(in.available() > 1024) {
|
---|
53 | in.read(buf);
|
---|
54 | os.write(buf);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | buf = new byte[in.available()];
|
---|
58 | in.read(buf);
|
---|
59 | os.write(buf);
|
---|
60 | os.flush();
|
---|
61 | in.close();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Reads an image request, and sends the image back if it was found
|
---|
66 | *
|
---|
67 | * @author jts21
|
---|
68 | *
|
---|
69 | */
|
---|
70 | private static class ImageSender extends Thread {
|
---|
71 |
|
---|
72 | private Socket socket;
|
---|
73 |
|
---|
74 | public ImageSender(Socket client) {
|
---|
75 | super("ImageSender");
|
---|
76 | this.socket = client;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void run() {
|
---|
81 | try {
|
---|
82 | InputStream is = this.socket.getInputStream();
|
---|
83 | // file name length is the first byte
|
---|
84 | int fileNameLen = is.read();
|
---|
85 | if(fileNameLen <= 0)
|
---|
86 | return;
|
---|
87 | // file name is the remaining bytes of the stream, encoded in UTF-8
|
---|
88 | byte[] fileName = new byte[fileNameLen];
|
---|
89 | is.read(fileName);
|
---|
90 | sendImage(new File(FrameIO.IMAGES_PATH + new String(fileName, FrameShare.CHARSET)), socket);
|
---|
91 | this.socket.close();
|
---|
92 | } catch (IOException e) {
|
---|
93 | e.printStackTrace();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public final static int OFFSET = 5;
|
---|
99 |
|
---|
100 | private ServerSocket socket;
|
---|
101 |
|
---|
102 | public ImageServer(int port) throws IOException {
|
---|
103 | super("ImageServer");
|
---|
104 | this.socket = new ServerSocket(port + OFFSET);
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | protected void listenForMessages() throws IOException {
|
---|
109 | Socket client = socket.accept();
|
---|
110 | // should probably only allow a limited number of threads here
|
---|
111 | new ImageSender(client).start();
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | protected void closeSocket() {
|
---|
116 | try {
|
---|
117 | socket.close();
|
---|
118 | } catch (IOException e) {
|
---|
119 | e.printStackTrace();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|