1 | /**
|
---|
2 | * FrameServer.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.BufferedReader;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.net.DatagramPacket;
|
---|
24 | import java.net.InetAddress;
|
---|
25 |
|
---|
26 | import org.expeditee.gui.FrameIO;
|
---|
27 | import org.expeditee.gui.MessageBay;
|
---|
28 |
|
---|
29 | public class FrameServer extends DefaultServer {
|
---|
30 | public final static int OFFSET = 0;
|
---|
31 |
|
---|
32 | public FrameServer(int port) throws IOException {
|
---|
33 | super("FrameServer", port);
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected String getFrame(String frameName) {
|
---|
37 | StringBuffer sb = new StringBuffer();
|
---|
38 | BufferedReader br = FrameIO.LoadPublicFrame(frameName);
|
---|
39 | if (br == null)
|
---|
40 | return null;
|
---|
41 |
|
---|
42 | String s = null;
|
---|
43 | try {
|
---|
44 | while ((s = br.readLine()) != null) {
|
---|
45 | // Check if there is space for the next line in the packet
|
---|
46 | if (sb.length() + s.length() > MAX_PACKET_LENGTH) {
|
---|
47 | MessageBay.errorMessage(frameName
|
---|
48 | + " is too large to be sent in a single packet");
|
---|
49 | break;
|
---|
50 | }
|
---|
51 | sb.append(s).append('\n');
|
---|
52 | }
|
---|
53 | } catch (IOException e) {
|
---|
54 | e.printStackTrace();
|
---|
55 | }
|
---|
56 |
|
---|
57 | return sb.toString();
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void listenForMessages() throws IOException {
|
---|
62 | byte[] buf = new byte[FRAMENAME_PACKET_LENGTH];
|
---|
63 |
|
---|
64 | // receive request
|
---|
65 | DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
---|
66 | socket.receive(packet);
|
---|
67 |
|
---|
68 | String frameName = new String(packet.getData(), 0, packet.getLength());
|
---|
69 | MessageBay.displayMessage("Recieved request for " + frameName);
|
---|
70 |
|
---|
71 | // figure out response
|
---|
72 | String dString = getFrame(frameName);
|
---|
73 | if (dString == null) {
|
---|
74 | dString = "";
|
---|
75 | }
|
---|
76 |
|
---|
77 | buf = dString.getBytes(FrameShare.CHARSET);
|
---|
78 |
|
---|
79 | // send the response to the client at "address" and "port"
|
---|
80 | InetAddress address = packet.getAddress();
|
---|
81 | int port = packet.getPort();
|
---|
82 | packet = new DatagramPacket(buf, buf.length, address, port);
|
---|
83 | socket.send(packet);
|
---|
84 | }
|
---|
85 | }
|
---|