Debian Codegraph @

junixsocket-demo/src/main/java/org/newsclub/net/unix/demo/client/DemoClient.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.client;
19
20 import java.io.IOException;
21 import java.net.SocketAddress;
22
23 import org.newsclub.net.unix.AFSocket;
24 import org.newsclub.net.unix.demo.DemoHelper;
25
26 /**
27 * A demo program to configure and run several {@link AFSocket} client demos from the command line.
28 *
29 * @author Christian Kohlschütter
30 */
31 public final class DemoClient {
32 public static void main(String[] args) throws IOException, InterruptedException {
33 final DemoClientBase demoClient;
34
35 String demo = DemoHelper.getPropertyValue("demo", "read-write", "read, read-write, read-fd");
36 String socketName = DemoHelper.getPropertyValue("socket", "/tmp/junixsocket.sock",
37 "/tmp/test.sock or localhost:1234");
38
39 if (demo == null) {
40 demoClient = null;
41 } else {
42 switch (demo) {
43 case "read":
44 demoClient = new ReadClient();
45 break;
46 case "read-write":
47 demoClient = new ReadWriteClient();
48 break;
49 case "read-fd":
50 demoClient = new ReadFileHandleClient();
51 break;
52 default:
53 demoClient = null;
54 break;
55 }
56 }
57
58 if (demoClient == null) {
59 System.out.flush();
60 System.err.println("You need to specify a valid demo to run.");
61 return;
62 }
63
64 SocketAddress endpoint = DemoHelper.socketAddress(socketName);
65
66 try {
67 demoClient.connect(endpoint);
68 } finally {
69 demoClient.close();
70 }
71 }
72 }