diff --git a/exampleClient/src/org/epics/exampleJava/exampleClient/HelloWorldRPC.java b/exampleClient/src/org/epics/exampleJava/exampleClient/HelloWorldRPC.java index 26b6445..8e5f6af 100644 --- a/exampleClient/src/org/epics/exampleJava/exampleClient/HelloWorldRPC.java +++ b/exampleClient/src/org/epics/exampleJava/exampleClient/HelloWorldRPC.java @@ -13,6 +13,7 @@ import org.epics.pvaClient.*; import org.epics.pvaClient.PvaClientRPC; import org.epics.pvaClient.PvaClientRPCRequester; +import org.epics.pvaccess.server.rpc.RPCRequestException; import org.epics.pvdata.factory.FieldFactory; import org.epics.pvdata.factory.PVDataFactory; import org.epics.pvdata.pv.FieldCreate; @@ -73,10 +74,25 @@ static void exampleSimple(PvaClient pva,String channelName) PVString pvArgument = pvRequest.getSubField(PVString.class,"value"); pvArgument.put("World"); System.out.println("send " + pvArgument.get()); - PVStructure pvResult = pva.channel(channelName).rpc(pvRequest); - System.out.println("result\n" + pvResult); + try { + PVStructure pvResult = pva.channel(channelName).rpc(pvRequest); + System.out.println("result\n" + pvResult); + } + catch (RPCRequestException ex) + { + // The client connected to the server, but the server request method issued its + // standard summary exception indicating it couldn't complete the requested task. + System.err.println("Acquisition of greeting was not successful, " + + "service responded with an error: " + ex.getMessage()); + } + catch (Exception ex) + { + // The client failed to connect to the server. The server isn't running or + // some other network related error occurred. + System.err.println("RPC request failed , " + ex.getMessage()); + } } - + static void exampleMore(PvaClient pva,String channelName) { System.out.println("_____exampleMore___"); @@ -86,14 +102,29 @@ static void exampleMore(PvaClient pva,String channelName) PVStructure pvRequest = pvDataCreate.createPVStructure(topStructure); PVString pvArgument = pvRequest.getSubField(PVString.class,"value"); PvaClientRPC rpc = pva.channel(channelName).createRPC(); - pvArgument.put("World"); - System.out.println("send " + pvArgument.get()); - PVStructure pvResult = rpc.request(pvRequest); - System.out.println("result\n" + pvResult); - pvArgument.put("Again"); - System.out.println("send " + pvArgument.get()); - pvResult = rpc.request(pvRequest); - System.out.println("result\n" + pvResult); + try { + pvArgument.put("World"); + System.out.println("send " + pvArgument.get()); + PVStructure pvResult = rpc.request(pvRequest); + System.out.println("result\n" + pvResult); + pvArgument.put("Again"); + System.out.println("send " + pvArgument.get()); + pvResult = rpc.request(pvRequest); + System.out.println("result\n" + pvResult); + } + catch (RPCRequestException ex) + { + // The client connected to the server, but the server request method issued its + // standard summary exception indicating it couldn't complete the requested task. + System.err.println("Acquisition of greeting was not successful, " + + "service responded with an error: " + ex.getMessage()); + } + catch (Exception ex) + { + // The client failed to connect to the server. The server isn't running or + // some other network related error occurred. + System.err.println("RPC request failed , " + ex.getMessage()); + } } static void exampleEvenMore(PvaClient pva,String channelName) @@ -113,30 +144,37 @@ static void exampleEvenMore(PvaClient pva,String channelName) rpc.issueConnect(); status = rpc.waitConnect(); if(!status.isOK()) {System.out.println(" rpc connect failed"); return;} - pvArgument.put("World"); - System.out.println("send " + pvArgument.get()); - rpc.request(pvRequest, requester); - requester.waitResponse(); - pvArgument.put("Again"); - System.out.println("send " + pvArgument.get()); - rpc.request(pvRequest, requester); - requester.waitResponse(); - rpc.setResponseTimeout(.001); - pvArgument.put("Once again"); - System.out.println("send " + pvArgument.get()); try { + pvArgument.put("World"); + System.out.println("send " + pvArgument.get()); rpc.request(pvRequest, requester); requester.waitResponse(); - } catch (Exception e) - { - System.err.println("Expected exception " + e.getMessage()); - } - try { + pvArgument.put("Again"); + System.out.println("send " + pvArgument.get()); rpc.request(pvRequest, requester); + requester.waitResponse(); + //rpc.setResponseTimeout(.001); + pvArgument.put("Once again"); + System.out.println("send " + pvArgument.get()); + System.out.println("This should succeed"); rpc.request(pvRequest, requester); - } catch (Exception e) + requester.waitResponse(); + System.out.println("This should fail"); + rpc.request(pvRequest, requester); + rpc.request(pvRequest, requester); + } + catch (RPCRequestException ex) { - System.err.println("Expected exception " + e.getMessage()); + // The client connected to the server, but the server request method issued its + // standard summary exception indicating it couldn't complete the requested task. + System.err.println("Acquisition of greeting was not successful, " + + "service responded with an error: " + ex.getMessage()); + } + catch (Exception ex) + { + // The client failed to connect to the server. The server isn't running or + // some other network related error occurred. + System.err.println("RPC request failed , " + ex.getMessage()); } } @@ -157,7 +195,5 @@ public static void main( String[] args ) e.printStackTrace(System.err); System.exit(1); } - pva.destroy(); } - } diff --git a/exampleClient/src/org/epics/exampleJava/exampleClient/Monitor.java b/exampleClient/src/org/epics/exampleJava/exampleClient/Monitor.java index f02b4c6..71dbfc4 100644 --- a/exampleClient/src/org/epics/exampleJava/exampleClient/Monitor.java +++ b/exampleClient/src/org/epics/exampleJava/exampleClient/Monitor.java @@ -39,7 +39,7 @@ static class ClientMonitor implements PvaClientChannelStateChangeRequester,PvaCl private boolean unlistenCalled = false; private PvaClientChannel pvaClientChannel = null; - PvaClientMonitor pvaClientMonitor = null; + private PvaClientMonitor pvaClientMonitor = null; void init(PvaClient pvaClient) { @@ -94,7 +94,7 @@ public void channelStateChange(PvaClientChannel channel, boolean isConnected) { */ public void monitorConnect(Status status,PvaClientMonitor monitor,Structure structure) { - System.out.println("channelMonitorConnect " + channelName + " status "); + System.out.println("channelMonitorConnect " + channelName + " status " + status); if(!status.isOK()) return; monitorConnected = true; if(isStarted) return; diff --git a/exampleClient/src/org/epics/exampleJava/exampleClient/TestGetConnect.java b/exampleClient/src/org/epics/exampleJava/exampleClient/TestGetConnect.java new file mode 100644 index 0000000..ca9d423 --- /dev/null +++ b/exampleClient/src/org/epics/exampleJava/exampleClient/TestGetConnect.java @@ -0,0 +1,239 @@ +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ + +/** + * @author mrk + */ + +package org.epics.exampleJava.exampleClient; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Arrays; + +import org.epics.pvaClient.PvaClient; +import org.epics.pvaClient.PvaClientChannel; +import org.epics.pvaClient.PvaClientChannelStateChangeRequester; +import org.epics.pvaClient.PvaClientGet; +import org.epics.pvaClient.PvaClientGetRequester; +import org.epics.pvdata.pv.Status; + + +public class TestGetConnect +{ + + static class ClientGet implements PvaClientChannelStateChangeRequester,PvaClientGetRequester + { + private String channelName; + private String providerName; + private String request; + private boolean channelConnected = false; + private boolean getConnected = false; + private PvaClientChannel pvaClientChannel = null; + private PvaClientGet pvaClientGet = null; + + private void init(PvaClient pvaClient) + { + pvaClientChannel = pvaClient.createChannel(channelName,providerName); + pvaClientChannel.setStateChangeRequester(this); + } + + static public ClientGet create( + PvaClient pvaClient,String channelName,String providerName,String request) + { + ClientGet clientGet = new ClientGet(channelName,providerName,request); + clientGet.init(pvaClient); + return clientGet; + } + + + private ClientGet(String channelName,String providerName,String request) + { + this.channelName = channelName; + this.providerName = providerName; + this.request = request; + } + + public void delete() + { + if(pvaClientGet!=null) pvaClientGet.destroy(); + if(pvaClientChannel!=null) pvaClientChannel.destroy(); + } + + /* (non-Javadoc) + * @see org.epics.pvaClient.PvaClientChannelStateChangeRequester#channelStateChange(org.epics.pvaClient.PvaClientChannel, boolean) + */ + public void channelStateChange(PvaClientChannel channel, boolean isConnected) { + channelConnected = isConnected; + if(isConnected && pvaClientGet==null) { + pvaClientGet = pvaClientChannel.createGet(request); + pvaClientGet.setRequester(this); + pvaClientGet.issueConnect(); + + } + } + /* (non-Javadoc) + * @see org.epics.pvaClient.PvaClientGetRequester#channelGetConnect(org.epics.pvdata.pv.Status, org.epics.pvaClient.PvaClientGet) + */ + public void channelGetConnect( + Status status, + PvaClientGet clientGet) + { + getConnected = true; + System.out.println("channelGetConnect " + channelName + " status "); + } + /* (non-Javadoc) + * @see org.epics.pvaClient.PvaClientGetRequester#getDone(org.epics.pvdata.pv.Status, org.epics.pvaClient.PvaClientGet) + */ + public void getDone( + Status status, + PvaClientGet clientGet) + { + if(status.isOK()) { + System.out.println(pvaClientGet.getData().getPVStructure());; + } else { + System.out.println("getDone " + channelName + " status "); + } + } + + public boolean connect() + { + try { + pvaClientChannel.connect(2.0); + return true; + } catch (Exception e) { + return false; + } + } + public void get() + { + if(!channelConnected) { + System.out.println(channelName + " channel not connected"); + return; + } + if(!getConnected) { + System.out.println(channelName + " channelGet not connected"); + + return; + } + pvaClientGet.issueGet(); + } + } + + public static void main( String[] args ) + { + String provider ="pva"; + String channelName ="PVRdouble"; + String request = "value,alarm,timeStamp"; + boolean debug = false; + int nargs = args.length; + int nextarg = 0; + while(true) { + if(nargs<=0) break; + String arg = args[nextarg]; + if(arg.equals("-help")) { + System.out.println("-p provider -r request -d debug channelNames"); + System.out.println("default"); + System.out.println( + "-p " + provider + + " -r " + request + + " -d " + debug + + " " + channelName + ); + return; + } + if(arg.equals("-p")) { + nargs = nargs-2; + nextarg++; + provider = args[nextarg]; + nextarg++; + continue; + } + if(arg.equals("-r")) { + nargs = nargs-2; + nextarg++; + request = args[nextarg]; + nextarg++; + continue; + } + if(arg.equals("-d")) { + nargs = nargs-2; + nextarg++; + String value = args[nextarg]; + debug = (value.equals("true") ? true : false); + nextarg++; + continue; + } + if(arg.startsWith("-")) { + System.err.printf("Illegal argument " + arg); + System.exit(1); + } + break; + } + String[] channelNames = new String[(nargs==0) ? 1 : nargs]; + int i=0; + if(nargs==0 ) { + channelNames[0] = channelName; + } else while(nargs>0) { + channelNames[i] = args[nextarg]; + i++; + nargs--; + nextarg++; + } + System.out.println( + "provider\"" + provider + "\"" + + + " request " + request + + " debug " + debug + + " channelNames " + Arrays.toString(channelNames) + ); + System.out.println("_____get starting_______"); + try { + if(debug) PvaClient.setDebug(true); + PvaClient pva = PvaClient.get(provider); + int num = channelNames.length; + ClientGet[] clientGets = new ClientGet[num]; + for(i=0; i0) { + channelNames[i] = args[nextarg]; + i++; + nargs--; + nextarg++; + } + System.out.println( + "provider\"" + provider + "\"" + + + " request " + request + + " debug " + debug + + " channelNames " + Arrays.toString(channelNames) + ); + System.out.println("_____monitor starting_______"); + try { + if(debug) PvaClient.setDebug(true); + PvaClient pva = PvaClient.get(provider); + int num = channelNames.length; + ClientMonitor[] clientMonitors = new ClientMonitor[num]; + for(i=0; i 0 ? args[0] : "anonymous"; - pvArguments.getStringField("personsname").put(name); - - try - { - // Create an RPC request and block until response is received. There is - // no need to explicitly wait for connection; this method takes care of it. - // In case of an error, an exception is throw. - PVStructure pvResult = client.request(pvArguments, REQUEST_TIMEOUT); + pvArguments.getSubField(PVString.class, "query.personsname").put(name); + // Create an RPC request and block until response is received. There + // is + // no need to explicitly wait for connection; this method takes care + // of it. + // In case of an error, an exception is throw. + PVStructure pvResult = client.request(pvArguments, REQUEST_TIMEOUT); - // Extract the result using the introspection interface of the returned - // datum, and print it. This particular service never returns a null result. - String res = pvResult.getStringField("greeting").get(); - System.out.println(res); - } - catch (RPCRequestException ex) - { - // The client connected to the server, but the server request method issued its - // standard summary exception indicating it couldn't complete the requested task. - System.err.println("Acquisition of greeting was not successful, " + - "service responded with an error: " + ex.getMessage()); - } - catch (IllegalStateException ex) - { - // The client failed to connect to the server. The server isn't running or - // some other network related error occurred. - System.err.println("Acquisition of greeting was not successful, " + - "failed to connect: "+ ex.getMessage()); - } - - // Disconnect from the service client. - client.destroy(); - } - finally - { - // Stop pvAccess client, so that this application exits cleanly. - org.epics.pvaccess.ClientFactory.stop(); + // Extract the result using the introspection interface of the + // returned + // datum, and print it. This particular service never returns a null + // result. + String res = pvResult.getStringField("greeting").get(); + System.out.println(res); + + System.out.println("The following should fail"); + requestStructure = + nturiBuilder.addQueryString("junk").createStructure(); + pvArguments = PVDataFactory.getPVDataCreate().createPVStructure(requestStructure); + pvResult = client.request(pvArguments, REQUEST_TIMEOUT); + res = pvResult.getStringField("greeting").get(); + System.out.println(res); + } catch (RPCRequestException ex) { + // The client connected to the server, but the server request method + // issued its + // standard summary exception indicating it couldn't complete the + // requested task. + System.err.println("Acquisition of greeting was not successful, " + "service responded with an error: " + + ex.getMessage()); + } catch (Exception ex) { + // The client failed to connect to the server. The server isn't + // running or + // some other network related error occurred. + System.err + .println("Acquisition of greeting was not successful, " + "failed to connect: " + ex.getMessage()); } + + // Disconnect from the service client. + client.destroy(); } } diff --git a/helloRPC/src/org/epics/exampleJava/helloRPC/HelloPvaClient.java b/helloRPC/src/org/epics/exampleJava/helloRPC/HelloPvaClient.java new file mode 100644 index 0000000..938a6b2 --- /dev/null +++ b/helloRPC/src/org/epics/exampleJava/helloRPC/HelloPvaClient.java @@ -0,0 +1,85 @@ +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ + +package org.epics.exampleJava.helloRPC; +import org.epics.nt.NTURI; +import org.epics.nt.NTURIBuilder; +import org.epics.pvaClient.PvaClient; +import org.epics.pvaClient.PvaClientChannel; +import org.epics.pvaClient.PvaClientRPC; +import org.epics.pvaccess.server.rpc.RPCRequestException; +import org.epics.pvdata.factory.PVDataFactory; +import org.epics.pvdata.pv.PVStructure; +import org.epics.pvdata.pv.Structure; + +/** + * HelloClient is a main class that illustrates a simple example of an EPICS V4 + * client/server interaction, using PvaClient + * + * @author Marty Kraimer 2019.04 + * @version Original + */ +public class HelloPvaClient +{ + /** + * The main establishes the connection to the helloServer, constructs the + * mechanism to pass parameters to the server, calls the server in the EV4 + * 2-step way, gets the response from the helloServer, unpacks it, and + * prints the greeting. + * + * @param args - the name of person to greet + */ + public static void main(String[] args) + { + PvaClient pva= PvaClient.get("pva"); + try + { + NTURIBuilder nturiBuilder = NTURI.createBuilder(); + Structure requestStructure = + nturiBuilder.addQueryString("personsname").createStructure(); + // Create the data instance used to send data to the server. That is, + // instantiate an instance of the "introspection interface" for the data interface of + // the hello server. The data interface was defined statically above. + PVStructure pvArguments = + PVDataFactory.getPVDataCreate().createPVStructure(requestStructure); + PvaClientChannel pvaChannel = pva.createChannel("helloService"); + PvaClientRPC clientRPC = pvaChannel.createRPC(); + + // Get the value of the first input argument to this executable and use it + // to set the data to be sent to the server through the introspection interface. + String name = args.length > 0 ? args[0] : "anonymous"; + pvArguments.getStringField("query.personsname").put(name); + // Create an RPC request and block until response is received. There is + // no need to explicitly wait for connection; this method takes care of it. + // In case of an error, an exception is throw. + PVStructure pvResult = clientRPC.request(pvArguments); + // Extract the result using the introspection interface of the returned + // datum, and print it. This particular service never returns a null result. + String res = pvResult.getStringField("greeting").get(); + System.out.println(res); + + System.out.println("The following should fail"); + requestStructure = + nturiBuilder.addQueryString("junk").createStructure(); + pvArguments = PVDataFactory.getPVDataCreate().createPVStructure(requestStructure); + pvResult = clientRPC.request(pvArguments); + res = pvResult.getStringField("greeting").get(); + System.out.println(res); + } + catch (RPCRequestException ex) + { + // The client connected to the server, but the server request method issued its + // standard summary exception indicating it couldn't complete the requested task. + System.err.println("Acquisition of greeting was not successful, " + + "service responded with an error: " + ex.getMessage()); + } + catch (Exception ex) + { + // The client failed to connect to the server. The server isn't running or + // some other network related error occurred. + System.err.println("RPC request failed , " + ex.getMessage()); + } + } +} diff --git a/helloRPC/src/org/epics/exampleJava/helloRPC/HelloService.java b/helloRPC/src/org/epics/exampleJava/helloRPC/HelloService.java index e55ca96..352688d 100644 --- a/helloRPC/src/org/epics/exampleJava/helloRPC/HelloService.java +++ b/helloRPC/src/org/epics/exampleJava/helloRPC/HelloService.java @@ -60,7 +60,7 @@ public PVStructure request(PVStructure args) throws RPCRequestException { // Extract the arguments. Just one in this case. // Report an error by throwing a RPCRequestException. - PVString inputPersonNameField = args.getStringField("personsname"); + PVString inputPersonNameField = args.getStringField("query.personsname"); if (inputPersonNameField == null) throw new RPCRequestException(StatusType.ERROR, "PVString field with name 'personsname' expected.");