summaryrefslogtreecommitdiff
path: root/java/sources/server.java
blob: 0ba1127d31f859421db4fe2bde74ec128e9305f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import digital.cabin.ezipc.EzIPCServer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;


class EzIPCServerTest
{
    private static final String PIPE_PATH = "/tmp/ezi_conn_test";

    public static void main(String[] args)
    {
        System.out.println("EZIPC Java Server");

        try
        {
            EzIPCServer srv = new EzIPCServer("/tmp/ezi_conn_test");

            System.out.print("Opening connection...");
            srv.open();
            System.out.println(" DONE!");

            ByteBuffer msg_ok = ByteBuffer.allocate(132);
            msg_ok.order(ByteOrder.nativeOrder());
            msg_ok.putInt(1);

            ByteBuffer msg_exit = ByteBuffer.allocate(132);
            msg_exit.order(ByteOrder.nativeOrder());
            msg_exit.putInt(2);

            while(true)
            {
                byte[] in = new byte[132];
                srv.receive(in);

                ByteBuffer msg_in = ByteBuffer.wrap(in);
                msg_in.order(ByteOrder.nativeOrder());

                int type = msg_in.getInt();
                if(type == 0)
                {
                    int end = 4;
                    for(; end < 132; end++) if(in[end] == 0) break;

                    String msg = new String(in, 4, end - 4,
                            StandardCharsets.UTF_8);

                    System.out.println("received: " + msg);

                    srv.send(msg_ok.array());
                }
                else if(type == 1)
                {
                    System.out.println("received spurious OK");
                    srv.send(msg_exit.array());
                }
                else if(type == 2)
                {
                    System.out.println("received EXIT");
                    srv.send(msg_exit.array());
                    break;
                }
                else
                {
                    System.out.println("received unkown msg type:" + type);
                    srv.send(msg_exit.array());
                }
            }

            srv.close();
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            System.exit(1);
        }

        System.out.println("bye...");
    }
}