1 | /**
|
---|
2 | * DefaultServer.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.DatagramSocket;
|
---|
23 |
|
---|
24 | public abstract class DefaultServer extends Thread {
|
---|
25 |
|
---|
26 |
|
---|
27 | protected static final int MAX_PACKET_LENGTH = 64000;
|
---|
28 | protected static final int FRAMENAME_PACKET_LENGTH = 1000;
|
---|
29 |
|
---|
30 | protected boolean _stop = false;
|
---|
31 | protected DatagramSocket socket = null;
|
---|
32 |
|
---|
33 | public DefaultServer() {
|
---|
34 | super();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public DefaultServer(Runnable target) {
|
---|
38 | super(target);
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected DefaultServer(String name) {
|
---|
42 | super(name);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public DefaultServer(String name, int port) throws IOException {
|
---|
46 | super(name);
|
---|
47 | socket = new DatagramSocket(port);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public DefaultServer(ThreadGroup group, Runnable target) {
|
---|
51 | super(group, target);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public DefaultServer(ThreadGroup group, String name) {
|
---|
55 | super(group, name);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public DefaultServer(Runnable target, String name) {
|
---|
59 | super(target, name);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public DefaultServer(ThreadGroup group, Runnable target, String name) {
|
---|
63 | super(group, target, name);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public DefaultServer(ThreadGroup group, Runnable target, String name,
|
---|
67 | long stackSize) {
|
---|
68 | super(group, target, name, stackSize);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void close() {
|
---|
72 | _stop = true;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void run() {
|
---|
76 | //MessageBay.displayMessage(this.getName() + " started on port "
|
---|
77 | // + socket.getLocalPort());
|
---|
78 | int fail = 0;
|
---|
79 |
|
---|
80 | while (!_stop) {
|
---|
81 | try {
|
---|
82 | listenForMessages();
|
---|
83 | fail = 0;
|
---|
84 | } catch (IOException e) {
|
---|
85 | e.printStackTrace();
|
---|
86 | } catch (Exception e) {
|
---|
87 | /**
|
---|
88 | * If we hit an Exception we don't want to crash,
|
---|
89 | * but we also don't want to get into an infinite loop of ignoring fatal exceptions.
|
---|
90 | * so try again but count the failures to make sure we don't get into an infinite loop.
|
---|
91 | * (don't just use a boolean because the next message we parse could also cause another
|
---|
92 | * non-fatal exception, and we don't want to exit unless absolutely necessary)
|
---|
93 | */
|
---|
94 | e.printStackTrace();
|
---|
95 | if(fail > 10) {
|
---|
96 | System.out.println("*****\nIt appears that we've failed to continue from an uncaught exception\n*****");
|
---|
97 | // close all the servers
|
---|
98 | FrameShare.getInstance().finalise();
|
---|
99 | // attempt to restart
|
---|
100 | // (don't do this yet since there's currently no way to know if the restart fails,
|
---|
101 | // so we could get stuck in a loop)
|
---|
102 | // FrameShare.restart();
|
---|
103 |
|
---|
104 | // if we're only running the frameshare server, exit
|
---|
105 | if(FrameShare.isHeadless()) {
|
---|
106 | System.exit(1);
|
---|
107 | }
|
---|
108 | } else {
|
---|
109 | fail++;
|
---|
110 | System.out.println("*****\nEncountered an uncaught exception, attempting to continue\n*****");
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | closeSocket();
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected void closeSocket() {
|
---|
119 | socket.close();
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected abstract void listenForMessages() throws IOException;
|
---|
123 |
|
---|
124 | } |
---|