Skip to content

Commit

Permalink
clean up the canonical examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Jul 9, 2015
1 parent 15d7798 commit db1c497
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
30 changes: 14 additions & 16 deletions examples/server_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
*/

#include <signal.h>
#include <stdlib.h>

#ifdef UA_NO_AMALGAMATION
# include <time.h>
# include "ua_types.h"
# include "ua_server.h"
# include "logger_stdout.h"
Expand All @@ -13,11 +15,6 @@
# include "open62541.h"
#endif

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

UA_Boolean running = UA_TRUE;
UA_Logger logger;

Expand All @@ -29,24 +26,25 @@ static UA_StatusCode helloWorldMethod(const UA_NodeId objectId, const UA_Variant
memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
tmp.length += inputStr->length;
}
UA_Variant_setScalar(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
UA_String_deleteMembers(&tmp);
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Hello World was called");
return UA_STATUSCODE_GOOD;
}

static void stopHandler(int sign) {
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
running = 0;
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = 0;
}

int main(int argc, char** argv) {
signal(SIGINT, stopHandler); /* catches ctrl-c */
signal(SIGINT, stopHandler); /* catches ctrl-c */

/* initialize the server */
UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
logger = Logger_Stdout_new();
UA_Server_setLogger(server, logger);
UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));

/* add the method node with the callback */
UA_Argument inputArguments;
Expand All @@ -67,14 +65,14 @@ int main(int argc, char** argv) {
outputArguments.name = UA_STRING("MyOutput");
outputArguments.valueRank = -1;

UA_Server_addMethodNode(server, UA_QUALIFIEDNAME(1,"ping"), UA_NODEID_NUMERIC(1,62541),
UA_Server_addMethodNode(server, UA_QUALIFIEDNAME(1, "hello world"), UA_NODEID_NUMERIC(1,62541),
UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
&helloWorldMethod, 1, &inputArguments, 1, &outputArguments);

/* start server */
UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false
UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false

/* ctrl-c received -> clean up */
UA_Server_delete(server);
return retval;
/* ctrl-c received -> clean up */
UA_Server_delete(server);
return retval;
}
20 changes: 8 additions & 12 deletions examples/server_repeated_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include <signal.h>
#include <errno.h> // errno, EINTR
#include <string.h>

#ifdef UA_NO_AMALGAMATION
# include "ua_types.h"
Expand All @@ -22,28 +18,28 @@ UA_Boolean running = 1;
UA_Logger logger;

static void stopHandler(int sign) {
printf("Received Ctrl-C\n");
running = 0;
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = 0;
}

static void testCallback(UA_Server *server, void *data) {
UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "testcallback");
}

int main(int argc, char** argv) {
signal(SIGINT, stopHandler); /* catches ctrl-c */
signal(SIGINT, stopHandler); /* catches ctrl-c */

UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
logger = Logger_Stdout_new();
UA_Server_setLogger(server, logger);
UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));

/* add a repeated job to the server */
/* add a repeated job to the server */
UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
.job.methodCall = {.method = testCallback, .data = NULL} };
UA_Server_addRepeatedJob(server, job, 2000, NULL); // call every 2 sec

UA_StatusCode retval = UA_Server_run(server, 1, &running);
UA_Server_delete(server);
return retval;
UA_Server_delete(server);
return retval;
}
20 changes: 8 additions & 12 deletions examples/server_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include <signal.h>
#include <errno.h> // errno, EINTR
#include <string.h>

#ifdef UA_NO_AMALGAMATION
# include "ua_types.h"
Expand All @@ -22,19 +18,19 @@ UA_Boolean running = 1;
UA_Logger logger;

static void stopHandler(int sign) {
printf("Received Ctrl-C\n");
running = 0;
UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = 0;
}

int main(int argc, char** argv) {
signal(SIGINT, stopHandler); /* catches ctrl-c */
signal(SIGINT, stopHandler); /* catches ctrl-c */

UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
logger = Logger_Stdout_new();
UA_Server_setLogger(server, logger);
UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));

/* add a variable node to the adresspace */
/* add a variable node to the adresspace */
UA_Variant *myIntegerVariant = UA_Variant_new();
UA_Int32 myInteger = 42;
UA_Variant_setScalarCopy(myIntegerVariant, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
Expand All @@ -46,7 +42,7 @@ int main(int argc, char** argv) {
myIntegerNodeId, parentNodeId, parentReferenceNodeId);

UA_StatusCode retval = UA_Server_run(server, 1, &running);
UA_Server_delete(server);
UA_Server_delete(server);

return retval;
return retval;
}
1 change: 1 addition & 0 deletions include/ua_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C" {
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "ua_config.h"

/** @brief A two-state logical value (true or false). */
Expand Down

0 comments on commit db1c497

Please sign in to comment.