1 | /**
|
---|
2 | * MessageReciever.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.IOException;
|
---|
22 | import java.net.DatagramPacket;
|
---|
23 |
|
---|
24 | import org.expeditee.gui.MessageBay;
|
---|
25 |
|
---|
26 | public class MessageReciever extends DefaultServer {
|
---|
27 | public final static int OFFSET = 2;
|
---|
28 |
|
---|
29 | public MessageReciever(int port) throws IOException {
|
---|
30 | super("MessageReciever", port + OFFSET);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void listenForMessages() throws IOException {
|
---|
34 | byte[] buf = new byte[FrameServer.MAX_PACKET_LENGTH];
|
---|
35 |
|
---|
36 | // receive request
|
---|
37 | DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
---|
38 | socket.receive(packet);
|
---|
39 |
|
---|
40 | String packetContents = new String(packet.getData(), 0, packet
|
---|
41 | .getLength());
|
---|
42 | String sendersName = FrameShare.getInstance().getPeerName(
|
---|
43 | packet.getPort() + 1, packet.getAddress());
|
---|
44 | /**
|
---|
45 | * If the sender of the message is not on our peer list then use their
|
---|
46 | * IP addy.
|
---|
47 | */
|
---|
48 | if (sendersName == null)
|
---|
49 | sendersName = packet.getAddress().toString();
|
---|
50 |
|
---|
51 | packetContents = sendersName + " says: " + packetContents;
|
---|
52 | MessageBay.displayMessage(packetContents);
|
---|
53 | }
|
---|
54 | }
|
---|