Debian Codegraph @

junixsocket-demo/src/main/java/org/newsclub/net/unix/demo/server/SocketServerDemo.java

raw
1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2022 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix.demo.server;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.SocketAddress;
23
24 import org.newsclub.net.unix.demo.DemoHelper;
25 import org.newsclub.net.unix.server.SocketServer;
26
27 /**
28 * A demo program to configure and run several {@link SocketServer} demos from the command line.
29 *
30 * @author Christian Kohlschütter
31 */
32 public final class SocketServerDemo {
33 public static void main(String[] args) throws IOException, InterruptedException {
34 final DemoServerBase demoServer;
35
36 String demo = DemoHelper.getPropertyValue("demo", null, "echo, null, zero, chargen, send-fd");
37 if (demo == null) {
38 demoServer = null;
39 } else {
40 String socketName = DemoHelper.getPropertyValue("socket", "/tmp/junixsocket-" + demo
41 + ".sock", "/tmp/test.sock or localhost:1234");
42
43 final SocketAddress listenAddress = DemoHelper.socketAddress(socketName);
44 System.out.println("Listen address: " + listenAddress);
45
46 switch (demo) {
47 case "echo":
48 demoServer = new EchoServer(listenAddress);
49 break;
50 case "null":
51 demoServer = new NullServer(listenAddress);
52 break;
53 case "zero":
54 demoServer = new ZeroServer(listenAddress);
55 break;
56 case "chargen":
57 boolean fast = DemoHelper.getPropertyValue("fast", "true", "true/false",
58 Boolean::valueOf);
59
60 demoServer = new ChargenServer(listenAddress, fast);
61 break;
62 case "send-fd":
63 File file = DemoHelper.getPropertyValue("file", null, "path to file", File::new);
64
65 demoServer = new SendFileHandleServer(listenAddress, file);
66 break;
67 default:
68 demoServer = null;
69 break;
70 }
71 }
72
73 if (demoServer == null) {
74 System.out.flush();
75 System.err.println("You need to specify a valid demo to run.");
76 return;
77 }
78
79 demoServer.setMaxConcurrentConnections(DemoHelper.getPropertyValue("maxConcurrentConnections",
80 "10", "1", Integer::parseInt));
81 demoServer.setServerTimeout(DemoHelper.getPropertyValue("serverTimeout", "0", "(time in ms)",
82 Integer::parseInt));
83 demoServer.setSocketTimeout(DemoHelper.getPropertyValue("socketTimeout", "60000",
84 "(time in ms)", Integer::parseInt));
85 demoServer.setServerBusyTimeout(DemoHelper.getPropertyValue("serverBusyTimeout", "1000",
86 "(time in ms)", Integer::parseInt));
87
88 demoServer.start();
89 }
90 }