1 | /**
|
---|
2 | * FrameSaver.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.io.StringReader;
|
---|
24 | import java.net.DatagramPacket;
|
---|
25 |
|
---|
26 | import org.expeditee.gui.FrameIO;
|
---|
27 | import org.expeditee.gui.MessageBay;
|
---|
28 |
|
---|
29 | public class FrameSaver extends DefaultServer {
|
---|
30 | public final static int OFFSET = 1;
|
---|
31 |
|
---|
32 | public FrameSaver(int port) throws IOException {
|
---|
33 | super("FrameSaver", port + OFFSET);
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | protected void listenForMessages() throws IOException {
|
---|
38 | byte[] buf = new byte[MAX_PACKET_LENGTH];
|
---|
39 |
|
---|
40 | // receive request
|
---|
41 | DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
---|
42 | socket.receive(packet);
|
---|
43 |
|
---|
44 | BufferedReader packetContents = new BufferedReader(new StringReader(
|
---|
45 | new String(packet.getData(), 0, packet.getLength())));
|
---|
46 |
|
---|
47 | // Get the name of the frame
|
---|
48 | String frameName = packetContents.readLine();
|
---|
49 | int version = Integer.parseInt(packetContents.readLine());
|
---|
50 |
|
---|
51 | MessageBay.displayMessage("Recieved request to save " + frameName);
|
---|
52 |
|
---|
53 | // TODO: find out what's causing a NullPointerException here
|
---|
54 | try {
|
---|
55 | FrameIO.SavePublicFrame(FrameShare.getInstance().getPeerName(
|
---|
56 | packet.getPort() + 2, packet.getAddress()), frameName, version,
|
---|
57 | packetContents);
|
---|
58 | } catch(Exception e) {
|
---|
59 | e.printStackTrace();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|