Skip to content

Commit

Permalink
Hide no_session connect for cleaner public api
Browse files Browse the repository at this point in the history
  • Loading branch information
Pro committed Mar 7, 2017
1 parent c91cedb commit 4968e20
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 283 deletions.
119 changes: 11 additions & 108 deletions examples/discovery/client_find_servers.c
Expand Up @@ -13,110 +13,6 @@ UA_Logger logger = UA_Log_Stdout;

#define DISCOVERY_SERVER_ENDPOINT "opc.tcp://localhost:4840"

static UA_StatusCode
FindServers(const char *discoveryServerUrl, size_t *registeredServerSize,
UA_ApplicationDescription **registeredServers) {
UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode retval = UA_Client_connect_no_session(client, discoveryServerUrl);
if (retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return retval;
}

UA_FindServersRequest request;
UA_FindServersRequest_init(&request);

/* If you want to find specific servers, you can also include the server
* URIs in the request: */
//request.serverUrisSize = 1;
//request.serverUris = UA_malloc(sizeof(UA_String));
//request.serverUris[0] = UA_String_fromChars("open62541.example.server_register");

//request.localeIdsSize = 1;
//request.localeIds = UA_malloc(sizeof(UA_String));
//request.localeIds[0] = UA_String_fromChars("en");

// now send the request
UA_FindServersResponse response;
UA_FindServersResponse_init(&response);
__UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_FINDSERVERSREQUEST],
&response, &UA_TYPES[UA_TYPES_FINDSERVERSRESPONSE]);

//UA_Array_delete(request.serverUris, request.serverUrisSize, &UA_TYPES[UA_TYPES_STRING]);
//UA_Array_delete(request.localeIds, request.localeIdsSize, &UA_TYPES[UA_TYPES_STRING]);

if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT,
"FindServers failed with statuscode %s", UA_StatusCode_name(response.responseHeader.serviceResult));
UA_FindServersResponse_deleteMembers(&response);
UA_Client_disconnect(client);
UA_Client_delete(client);
return response.responseHeader.serviceResult;
}

*registeredServerSize = response.serversSize;
*registeredServers = (UA_ApplicationDescription *) UA_Array_new(response.serversSize,
&UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
for (size_t i = 0; i < response.serversSize; i++)
UA_ApplicationDescription_copy(&response.servers[i], &(*registeredServers)[i]);
UA_FindServersResponse_deleteMembers(&response);

UA_Client_disconnect(client);
UA_Client_delete(client);
return (int) UA_STATUSCODE_GOOD;
}


static UA_StatusCode
FindServersOnNetwork(const char *discoveryServerUrl, size_t *serverOnNetworkSize, UA_ServerOnNetwork **serverOnNetwork) {
UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode retval = UA_Client_connect_no_session(client, discoveryServerUrl);
if (retval != UA_STATUSCODE_GOOD) {
UA_Client_delete(client);
return retval;
}


UA_FindServersOnNetworkRequest request;
UA_FindServersOnNetworkRequest_init(&request);

request.startingRecordId = 0;
request.maxRecordsToReturn = 0; // get all
/*
* Here you can define some filtering rules:
*/
//request.serverCapabilityFilterSize = 1;
//request.serverCapabilityFilter[0] = UA_String_fromChars("LDS");

// now send the request
UA_FindServersOnNetworkResponse response;
UA_FindServersOnNetworkResponse_init(&response);
__UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_FINDSERVERSONNETWORKREQUEST],
&response, &UA_TYPES[UA_TYPES_FINDSERVERSONNETWORKRESPONSE]);

//UA_Array_delete(request.serverCapabilityFilter, request.serverCapabilityFilterSize, &UA_TYPES[UA_TYPES_STRING]);

if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT,
"FindServersOnNetwork failed with statuscode %s", UA_StatusCode_name(response.responseHeader.serviceResult));
UA_StatusCode retVal = response.responseHeader.serviceResult;
UA_FindServersOnNetworkResponse_deleteMembers(&response);
UA_Client_disconnect(client);
UA_Client_delete(client);
return retVal;
}

*serverOnNetworkSize = response.serversSize;
*serverOnNetwork = (UA_ServerOnNetwork *) UA_Array_new(response.serversSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]);
for (size_t i = 0; i < response.serversSize; i++)
UA_ServerOnNetwork_copy(&response.servers[i], &(*serverOnNetwork)[i]);
UA_FindServersOnNetworkResponse_deleteMembers(&response);

UA_Client_disconnect(client);
UA_Client_delete(client);
return (int) UA_STATUSCODE_GOOD;
}

int main(void) {

/*
Expand All @@ -127,10 +23,13 @@ int main(void) {
UA_ServerOnNetwork *serverOnNetwork = NULL;
size_t serverOnNetworkSize = 0;

UA_StatusCode retval = FindServersOnNetwork(DISCOVERY_SERVER_ENDPOINT, &serverOnNetworkSize, &serverOnNetwork);
UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
UA_StatusCode retval = UA_Client_findServersOnNetwork(client, DISCOVERY_SERVER_ENDPOINT, 0, 0,
0, NULL, &serverOnNetworkSize, &serverOnNetwork);
if (retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "Could not call FindServersOnNetwork service. Is the discovery server started? StatusCode %s",
UA_StatusCode_name(retval));
UA_Client_delete(client);
return (int) retval;
}

Expand Down Expand Up @@ -158,9 +57,13 @@ int main(void) {
UA_ApplicationDescription *applicationDescriptionArray = NULL;
size_t applicationDescriptionArraySize = 0;

UA_StatusCode retval = FindServers(DISCOVERY_SERVER_ENDPOINT,
&applicationDescriptionArraySize,
&applicationDescriptionArray);
UA_StatusCode retval;
{
UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
retval = UA_Client_findServers(client, DISCOVERY_SERVER_ENDPOINT, 0, NULL, 0, NULL,
&applicationDescriptionArraySize, &applicationDescriptionArray);
UA_Client_delete(client);
}
if (retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "Could not call FindServers service. "
"Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval));
Expand Down
86 changes: 63 additions & 23 deletions include/ua_client.h
Expand Up @@ -52,7 +52,7 @@ typedef struct UA_ClientConfig {
const UA_DataType *customDataTypes;
} UA_ClientConfig;

/**
/*
* Client Lifecycle
* ---------------- */
typedef enum {
Expand All @@ -72,7 +72,7 @@ typedef enum {
struct UA_Client;
typedef struct UA_Client UA_Client;

/* Create a new client
/** Create a new client
*
* @param config for the new client. You can use UA_ClientConfig_standard
* which has sane defaults
Expand All @@ -81,49 +81,89 @@ typedef struct UA_Client UA_Client;
* @return return the new Client object */
UA_Client UA_EXPORT * UA_Client_new(UA_ClientConfig config);

/* Get the client connection status */
/** Get the client connection status */
UA_ClientState UA_EXPORT UA_Client_getState(UA_Client *client);

/* Reset a client */
/** Reset a client */
void UA_EXPORT UA_Client_reset(UA_Client *client);

/* Delete a client */
/** Delete a client */
void UA_EXPORT UA_Client_delete(UA_Client *client);

/**
* Manage the Connection
* --------------------- */
/* Gets a list of endpoints of a server
/** Gets a list of endpoints of a server
*
* @param client to use
* @param server url to connect (for example "opc.tcp://localhost:16664")
* @param client to use. Must be connected to the same endpoint given in serverUrl or otherwise in disconnected state.
* @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
* @param endpointDescriptionsSize size of the array of endpoint descriptions
* @param endpointDescriptions array of endpoint descriptions that is allocated
* by the function (you need to free manually)
* @return Indicates whether the operation succeeded or returns an error code */
* @return Indicates whether the operation succeeded or returns an error code
*/
UA_StatusCode UA_EXPORT
UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
size_t* endpointDescriptionsSize,
UA_EndpointDescription** endpointDescriptions);

/* Connect to the selected server
/**
* Gets a list of all registered servers at the given server.
*
* @param client to use
* @param endpointURL to connect (for example "opc.tcp://localhost:16664")
* @return Indicates whether the operation succeeded or returns an error code */
* You can pass an optional filter for serverUris. If the given server is not registered,
* an empty array will be returned. If the server is registered, only that application
* description will be returned.
*
* Additionally you can optionally indicate which locale you want for the server name
* in the returned application description. The array indicates the order of preference.
* A server may have localized names.
*
* @param client to use. Must be connected to the same endpoint given in serverUrl or otherwise in disconnected state.
* @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
* @param serverUrisSize Optional filter for specific server uris
* @param serverUris Optional filter for specific server uris
* @param localeIdsSize Optional indication which locale you prefer
* @param localeIds Optional indication which locale you prefer
* @param registeredServerSize size of returned array, i.e., number of found/registered servers
* @param registeredServers array containing found/registered servers
* @return Indicates whether the operation succeeded or returns an error code
*/
UA_StatusCode UA_EXPORT
UA_Client_connect(UA_Client *client, const char *endpointUrl);
UA_Client_findServers(UA_Client *client, const char *serverUrl,
size_t serverUrisSize, UA_String *serverUris,
size_t localeIdsSize, UA_String *localeIds,
size_t *registeredServerSize, UA_ApplicationDescription **registeredServers);

/* Connect to the selected server.
* This will not create a session.
/**
* Get a list of all known server in the network. Only supported by LDS servers.
*
*
* @param client to use. Must be connected to the same endpoint given in serverUrl or otherwise in disconnected state.
* @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
* @param startingRecordId optional. Only return the records with an ID higher or equal the given. Can be used for pagination to only get a subset of the full list
* @param maxRecordsToReturn optional. Only return this number of records
* @param serverCapabilityFilterSize optional. Filter the returned list to only get servers with given capabilities, e.g. "LDS"
* @param serverCapabilityFilter optional. Filter the returned list to only get servers with given capabilities, e.g. "LDS"
* @param serverOnNetworkSize size of returned array, i.e., number of known/registered servers
* @param serverOnNetwork array containing known/registered servers
* @return Indicates whether the operation succeeded or returns an error code
*/
UA_StatusCode UA_EXPORT
UA_Client_findServersOnNetwork(UA_Client *client, const char *serverUrl,
UA_UInt32 startingRecordId, UA_UInt32 maxRecordsToReturn,
size_t serverCapabilityFilterSize, UA_String *serverCapabilityFilter,
size_t *serverOnNetworkSize, UA_ServerOnNetwork **serverOnNetwork);

/*
* Manage the Connection
* --------------------- */

/** Connect to the selected server
*
* @param client to use
* @param endpointURL to connect (for example "opc.tcp://localhost:16664")
* @return Indicates whether the operation succeeded or returns an error code */
UA_StatusCode UA_EXPORT
UA_Client_connect_no_session(UA_Client *client, const char *endpointUrl);
UA_Client_connect(UA_Client *client, const char *endpointUrl);

/* Connect to the selected server with the given username and password
/** Connect to the selected server with the given username and password
*
* @param client to use
* @param endpointURL to connect (for example "opc.tcp://localhost:16664")
Expand All @@ -134,10 +174,10 @@ UA_StatusCode UA_EXPORT
UA_Client_connect_username(UA_Client *client, const char *endpointUrl,
const char *username, const char *password);

/* Close a connection to the selected server */
/** Close a connection to the selected server */
UA_StatusCode UA_EXPORT UA_Client_disconnect(UA_Client *client);

/* Renew the underlying secure channel */
/** Renew the underlying secure channel */
UA_StatusCode UA_EXPORT UA_Client_manuallyRenewSecureChannel(UA_Client *client);

/**
Expand Down

0 comments on commit 4968e20

Please sign in to comment.