Skip to content

Commit cff4645

Browse files
committed
jnut/Client.java: rename primary()=>becomePrimary(), add becomeSecondary() to match
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 07da21f commit cff4645

1 file changed

Lines changed: 49 additions & 6 deletions

File tree

jNut/src/main/java/org/networkupstools/jnut/Device.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public String getDescription() throws IOException, NutException {
9797
* or an upsmon replacement.
9898
* @throws IOException
9999
* @throws NutException
100+
* @see #becomePrimary
101+
* @see #becomeSecondary
102+
* @see Client#authenticate
100103
*/
101104
public void login() throws IOException, NutException {
102105
if(client!=null)
@@ -111,22 +114,26 @@ public void login() throws IOException, NutException {
111114
}
112115

113116
/**
114-
* This function doesn't do much by itself. It is used by <i>upsmon</i> to make
115-
* sure that master-level functions like FSD are available if necessary.
117+
* This function does little by itself.
118+
* It is used by <i>upsmon</i> to make sure that master-level functions
119+
* like FSD are available if necessary.
116120
* <p>
117121
* NOTE: API changed since NUT 2.8.0 to replace MASTER with PRIMARY
118122
* (and backwards-compatible alias handling)
119123
* @throws IOException
120124
* @throws NutException
125+
* @see #becomeSecondary
126+
* @see #login
127+
* @see Client#authenticate
121128
*/
122-
public void primary() throws IOException, NutException {
129+
public void becomePrimary() throws IOException, NutException {
123130
if(client!=null)
124131
{
125132
try {
126133
String res = client.query("PRIMARY", name);
127134
if(!res.startsWith("OK"))
128135
{
129-
throw new NutException(NutException.UnknownResponse, "Unknown response in Device.primary : " + res);
136+
throw new NutException(NutException.UnknownResponse, "Unknown response in Device.becomePrimary : " + res);
130137
}
131138
} catch (NutException ex) {
132139
// Retry with MASTER if PRIMARY failed
@@ -139,7 +146,7 @@ public void primary() throws IOException, NutException {
139146
* Internal helper to send the legacy MASTER command.
140147
* <p>
141148
* This is used by the deprecated {@link #master()} method and as a
142-
* compatibility fallback for {@link #primary()} when the PRIMARY
149+
* compatibility fallback for {@link #becomePrimary()} when the PRIMARY
143150
* command is not recognized by the (older) data server.
144151
*
145152
* @throws IOException
@@ -158,15 +165,51 @@ private void sendMasterCommand() throws IOException, NutException {
158165
}
159166

160167
/**
161-
* @deprecated Use primary() instead
168+
* @deprecated Use {@link #becomePrimary} instead
162169
* @throws IOException
163170
* @throws NutException
171+
* @see #becomeSecondary NOTE: There never was a "slave" command in jNut
164172
*/
165173
@Deprecated
166174
public void master() throws IOException, NutException {
167175
sendMasterCommand();
168176
}
169177

178+
/**
179+
* This function does little by itself.
180+
* It is used by <i>upsmon</i> to make sure that slave instances are known
181+
* and waited for by the master instance to disconnect from the data server
182+
* in case of FSD and mass shutdown.
183+
* <p>
184+
* NOTE: API changed since NUT 2.8.0 to replace SLAVE with SECONDARY
185+
* (and backwards-compatible alias handling)
186+
* @throws IOException
187+
* @throws NutException
188+
* @see #becomePrimary
189+
* @see #login
190+
* @see Client#authenticate
191+
*/
192+
public void becomeSecondary() throws IOException, NutException {
193+
if(client!=null)
194+
{
195+
try {
196+
String res = client.query("SECONDARY", name);
197+
if(!res.startsWith("OK"))
198+
{
199+
throw new NutException(NutException.UnknownResponse, "Unknown response in Device.becomeSecondary : " + res);
200+
}
201+
} catch (NutException ex) {
202+
// Retry with SLAVE if SECONDARY failed
203+
String res = client.query("SLAVE", name);
204+
if(!res.startsWith("OK"))
205+
{
206+
// Normally response should be OK or ERR and nothing else.
207+
throw new NutException(NutException.UnknownResponse, "Unknown response in Device.becomeSecondary : " + res);
208+
}
209+
}
210+
}
211+
}
212+
170213
/**
171214
* Set the "forced shutdown" flag.
172215
* <p>

0 commit comments

Comments
 (0)