aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/gnunet/util/ServerExample.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/gnunet/util/ServerExample.java')
-rw-r--r--src/test/java/org/gnunet/util/ServerExample.java79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/test/java/org/gnunet/util/ServerExample.java b/src/test/java/org/gnunet/util/ServerExample.java
new file mode 100644
index 0000000..a344e2a
--- /dev/null
+++ b/src/test/java/org/gnunet/util/ServerExample.java
@@ -0,0 +1,79 @@
1/*
2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.util;
22
23import java.net.InetSocketAddress;
24import java.net.SocketAddress;
25import java.util.Arrays;
26
27import static org.gnunet.util.Server.*;
28
29/**
30 * Example server implementation.
31 *
32 * @author Florian Dold
33 */
34public class ServerExample {
35
36 public static void main(String[] args) {
37 // usually servers should run inside a service, this is just for testing
38 new Program(args) {
39 @Override
40 public void run() {
41 Server s = new Server(Arrays.asList(new SocketAddress[]{new InetSocketAddress("127.0.0.1", 3456)}),
42 RelativeTime.MINUTE,
43 false);
44 s.setHandler(new Server.MessageRunabout() {
45 public void visit(TestMessage tm) {
46 System.out.println("got TEST message");
47 final Server.ClientHandle sender = getSender();
48 sender.notifyTransmitReady(4, RelativeTime.FOREVER, new MessageTransmitter() {
49 @Override
50 public void transmit(Connection.MessageSink sink) {
51 sink.send(new TestMessage());
52 System.out.println("TEST message sent");
53 sender.receiveDone(true);
54 }
55
56 @Override
57 public void handleError() {
58 System.out.println("error talking to client!");
59 }
60 });
61 }
62
63 public void visit(UnknownMessageBody b) {
64 System.out.println("got message of unknown type " + b.id);
65 }
66 });
67
68 s.notifyDisconnect(new DisconnectHandler() {
69 @Override
70 public void onDisconnect(Server.ClientHandle clientHandle) {
71 System.out.println("client disconnected");
72
73 }
74 });
75
76 }
77 }.start();
78 }
79}