fr.quatrevieux.singleinstance.DistantInstance   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 39
c 1
b 0
f 0
dl 0
loc 90
ccs 27
cts 29
cp 0.931
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ProcessInfo.hasPort() 0 2 1
A ProcessInfo.ProcessInfo(int) 0 2 1
A DistantInstance(LockFile) 0 4 1
A load() 0 11 2
A send(Message) 0 3 1
A pid() 0 2 1
A port() 0 14 4
A client() 0 6 2
1
/*
2
 * This file is part of SingleInstance.
3
 *
4
 * SingleInstance is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * SingleInstance is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with SingleInstance.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.singleinstance;
21
22
23
import fr.quatrevieux.singleinstance.ipc.InstanceClient;
24
import fr.quatrevieux.singleinstance.ipc.Message;
25
import fr.quatrevieux.singleinstance.ipc.MessageSender;
26
27
import java.io.EOFException;
28
import java.io.IOException;
29
30
/**
31
 * Handle information and interactions with the distant instance (i.e. the first running instance)
32
 */
33
final public class DistantInstance implements MessageSender {
34
    static private class ProcessInfo {
35
        public int pid;
36 1
        public int port = -1;
37
38 1
        public ProcessInfo(int pid) {
39 1
            this.pid = pid;
40 1
        }
41
42
        public boolean hasPort() {
43 1
            return port != -1;
44
        }
45
    }
46
47
    final private LockFile lockFile;
48
49
    private ProcessInfo info;
50
    private InstanceClient client;
51
52 1
    public DistantInstance(LockFile lockFile) throws IOException {
53 1
        this.lockFile = lockFile;
54
55 1
        load();
56 1
    }
57
58
    /**
59
     * Get the PID of the distant instance
60
     *
61
     * @return The PID as int
62
     */
63
    public int pid() {
64 1
        return info.pid;
65
    }
66
67
    /**
68
     * Get the opened port number
69
     *
70
     * @return The port number as int
71
     * @throws IllegalStateException When port cannot be resolved
72
     */
73
    public int port() {
74 1
        if (!info.hasPort()) {
75
            try {
76 1
                load();
77
            } catch (IOException e) {
78
                // Ignore
79 1
            }
80
81 1
            if (!info.hasPort()) {
82 1
                throw new IllegalStateException("The distant server is not started or cannot access to the port number");
83
            }
84
        }
85
86 1
        return info.port;
87
    }
88
89
    /**
90
     * Get the IPC client to communicate with the distant instance
91
     * Note: send methods can be used as shortcut
92
     *
93
     * @return The client instance
94
     * @throws IllegalStateException When cannot find the port number
95
     */
96
    public InstanceClient client() {
97 1
        if (client != null) {
98
            return client;
99
        }
100
101 1
        return client = new InstanceClient(port());
102
    }
103
104
    @Override
105
    public void send(Message message) throws IOException {
106 1
        client().send(message);
107 1
    }
108
109
    /**
110
     * Load info from the lock file
111
     */
112
    private void load() throws IOException {
113 1
        info = lockFile.read(input -> {
114 1
            ProcessInfo processInfo = new ProcessInfo(input.readInt());
115
116
            try {
117 1
                processInfo.port = input.readInt();
118 1
            } catch (EOFException e) {
119 1
                processInfo.port = -1;
120 1
            }
121
122 1
            return processInfo;
123
        });
124 1
    }
125
}
126