Skip to content

Commit 04bf337

Browse files
authored
Merge pull request #3256 from ControlSystemStudio/pva_cmd_message
PVA: Support CMD_MESSAGE on server side
2 parents 3b23198 + 639d0b9 commit 04bf337

File tree

9 files changed

+80
-21
lines changed

9 files changed

+80
-21
lines changed

core/pva/src/main/java/org/epics/pva/data/PVAStatus.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -22,7 +22,7 @@ public class PVAStatus
2222
/** Type or severity of status message */
2323
public enum Type
2424
{
25-
/** OK */
25+
/** OK (info) */
2626
OK,
2727
/** Warning */
2828
WARNING,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Oak Ridge National Laboratory.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
******************************************************************************/
8+
package org.epics.pva.server;
9+
10+
import java.nio.ByteBuffer;
11+
12+
import org.epics.pva.common.PVAHeader;
13+
import org.epics.pva.common.RequestEncoder;
14+
import org.epics.pva.data.PVAStatus;
15+
import org.epics.pva.data.PVAString;
16+
17+
/** Encode CMD_MESSAGE from server to client */
18+
public class MessageRequest implements RequestEncoder
19+
{
20+
private final int request_id;
21+
private final PVAStatus.Type type;
22+
private final String message;
23+
24+
/** @param request_id ID of client request to which this message applies
25+
* @param type Message type
26+
* @param message Message text
27+
*/
28+
public MessageRequest(final int request_id, final PVAStatus.Type type, final String message)
29+
{
30+
this.request_id = request_id;
31+
this.type = type;
32+
this.message = message;
33+
}
34+
35+
@Override
36+
public void encodeRequest(byte version, ByteBuffer buffer) throws Exception
37+
{
38+
final int payload = 4 + 1 + PVAString.getEncodedSize(message);
39+
PVAHeader.encodeMessageHeader(buffer, PVAHeader.FLAG_SERVER, PVAHeader.CMD_MESSAGE, payload);
40+
buffer.putInt(request_id);
41+
buffer.put((byte) type.ordinal());
42+
PVAString.encodeString(message, buffer);
43+
}
44+
}

core/pva/src/main/java/org/epics/pva/server/PutHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -110,7 +110,7 @@ else if (subcmd == 0 || subcmd == PVAHeader.CMD_SUB_DESTROY)
110110
// Notify PV
111111
try
112112
{
113-
pv.wrote(written, data);
113+
pv.wrote(tcp, written, data);
114114
}
115115
catch (Exception ex)
116116
{

core/pva/src/main/java/org/epics/pva/server/ServerDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019-2021 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -61,7 +61,7 @@ public static void main(String[] args) throws Exception
6161
final ServerPV pv2 = server.createPV("demo2", data);
6262

6363
// Writable
64-
final ServerPV write_pv = server.createPV("demo3", writable_data, (pv, changes, written) ->
64+
final ServerPV write_pv = server.createPV("demo3", writable_data, (tcp, pv, changes, written) ->
6565
{
6666
// Write handler could check what was changed,
6767
// clamp data to certain range etc.

core/pva/src/main/java/org/epics/pva/server/ServerPV.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019-2020 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -40,7 +40,7 @@ public class ServerPV implements AutoCloseable
4040
private static final RPCService DEFAULT_RPC_SERVICE = request -> NO_SERVICE_VALUE;
4141

4242
/** {@link WriteEventHandler} for read-only PVs */
43-
static final WriteEventHandler READONLY_WRITE_HANDLER = (pv, changes, data) ->
43+
static final WriteEventHandler READONLY_WRITE_HANDLER = (tcp, pv, changes, data) ->
4444
{
4545
throw new Exception("PV " + pv.getName() + " is read-only");
4646
};
@@ -223,13 +223,14 @@ boolean isWritable()
223223
}
224224

225225
/** Notification that a client wrote to the PV
226+
* @param tcp {@link ServerTCPHandler} that received the 'write'
226227
* @param changes Elements that the client tried to change
227228
* @param written_data Data written by the client
228229
* @throws Exception on error
229230
*/
230-
void wrote(final BitSet changes, final PVAStructure written_data) throws Exception
231+
void wrote(final ServerTCPHandler tcp, final BitSet changes, final PVAStructure written_data) throws Exception
231232
{
232-
write_handler.handleWrite(this, changes, written_data);
233+
write_handler.handleWrite(tcp, this, changes, written_data);
233234
}
234235

235236
/** Invoke RPC service

core/pva/src/main/java/org/epics/pva/server/WriteEventHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
99

1010
import java.util.BitSet;
1111

12+
import org.epics.pva.common.TCPHandler;
1213
import org.epics.pva.data.PVAStructure;
1314

1415
/** Handler for a client's write (PUT) to a PV
@@ -29,10 +30,11 @@ public interface WriteEventHandler
2930
* or throw an exception to notify client that the write
3031
* access was refused.
3132
*
33+
* @param tcp TCP handler that received the write request
3234
* @param pv PV that the client wrote
3335
* @param changes Fields of the PV data that were changed
3436
* @param written Data that the client wrote
3537
* @throws Exception on error
3638
*/
37-
public void handleWrite(ServerPV pv, BitSet changes, PVAStructure written) throws Exception;
39+
public void handleWrite(TCPHandler tcp, ServerPV pv, BitSet changes, PVAStructure written) throws Exception;
3840
}

core/pva/src/test/java/org/epics/pva/combined/WriteDemo.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019-2022 Oak Ridge National Laboratory.
2+
* Copyright (c) 2019-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -22,8 +22,10 @@
2222
import org.epics.pva.client.PVAChannel;
2323
import org.epics.pva.client.PVAClient;
2424
import org.epics.pva.data.PVADouble;
25+
import org.epics.pva.data.PVAStatus;
2526
import org.epics.pva.data.PVAStructure;
2627
import org.epics.pva.data.nt.PVATimeStamp;
28+
import org.epics.pva.server.MessageRequest;
2729
import org.epics.pva.server.PVAServer;
2830
import org.epics.pva.server.WriteEventHandler;
2931

@@ -47,14 +49,22 @@ static void serve(final String name)
4749
final PVAStructure data = new PVAStructure("demo", "demo_t",
4850
new PVADouble("value", 3.13),
4951
new PVATimeStamp());
50-
final WriteEventHandler write_handler = (pv, changes, written) ->
52+
final WriteEventHandler write_handler = (tcp, pv, changes, written) ->
5153
{
5254
// Set timestamp, then take data as received
5355
PVATimeStamp.set(written, Instant.now());
5456
pv.update(written);
5557

56-
System.out.println("Received " + name + " = " + written);
57-
if (((PVADouble)written.get(1)).get() < 0)
58+
double value = ((PVADouble)written.get(1)).get();
59+
60+
// Demo of issuing a status message
61+
// The request ID should point to the
62+
// client request to which this message applies.
63+
// In this demo, we simply use '1'
64+
tcp.submit(new MessageRequest(1, PVAStatus.Type.OK,
65+
"Received " + name + " = " + value));
66+
67+
if (value < 0)
5868
done.countDown();
5969
};
6070
server.createPV(name, data, write_handler);
@@ -76,7 +86,7 @@ public static void main(String[] args) throws Exception
7686
// Configure logging
7787
LogManager.getLogManager().readConfiguration(PVASettings.class.getResourceAsStream("/pva_logging.properties"));
7888
final Logger root = Logger.getLogger("");
79-
root.setLevel(Level.WARNING);
89+
root.setLevel(Level.INFO);
8090
for (Handler handler : root.getHandlers())
8191
handler.setLevel(root.getLevel());
8292

core/pva/src/test/java/org/epics/pva/server/BoolDemo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2022 Oak Ridge National Laboratory.
2+
* Copyright (c) 2022-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -14,6 +14,7 @@
1414
import java.util.logging.LogManager;
1515

1616
import org.epics.pva.PVASettings;
17+
import org.epics.pva.common.TCPHandler;
1718
import org.epics.pva.data.PVABool;
1819
import org.epics.pva.data.PVAStructure;
1920
import org.epics.pva.data.nt.PVATimeStamp;
@@ -53,7 +54,7 @@ public static void main(String[] args) throws Exception
5354

5455
// Create PVs
5556
final ServerPV pv1 = server.createPV("bool", data);
56-
final ServerPV pv2 = server.createPV("struct", struct, (ServerPV pv, BitSet changes, PVAStructure written) ->
57+
final ServerPV pv2 = server.createPV("struct", struct, (TCPHandler tcp, ServerPV pv, BitSet changes, PVAStructure written) ->
5758
{
5859
System.out.println("Somebody wrote this to '" + pv.getName() + "':\n" + written);
5960
pv.update(written);

core/pva/src/test/java/org/epics/pva/server/TableDemo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2020 Oak Ridge National Laboratory.
2+
* Copyright (c) 2020-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -12,6 +12,7 @@
1212
import java.util.logging.LogManager;
1313

1414
import org.epics.pva.PVASettings;
15+
import org.epics.pva.common.TCPHandler;
1516
import org.epics.pva.data.PVADoubleArray;
1617
import org.epics.pva.data.PVAIntArray;
1718
import org.epics.pva.data.PVAStringArray;
@@ -52,7 +53,7 @@ public static void main(String[] args) throws Exception
5253
// Create PVs
5354
final ServerPV pv1 = server.createPV("demo", data, new WriteEventHandler() {
5455
@Override
55-
public void handleWrite(ServerPV pv, BitSet changes, PVAStructure written) throws Exception {
56+
public void handleWrite(TCPHandler tcp, ServerPV pv, BitSet changes, PVAStructure written) throws Exception {
5657
System.out.println();
5758
}
5859
});

0 commit comments

Comments
 (0)