server.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <time.h>
  6. #include "ua_types.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <signal.h>
  10. #include <errno.h> // errno, EINTR
  11. // provided by the open62541 lib
  12. #include "ua_server.h"
  13. // provided by the user, implementations available in the /examples folder
  14. #include "logger_stdout.h"
  15. #include "networklayer_tcp.h"
  16. UA_Boolean running = 1;
  17. UA_Logger logger;
  18. static void stopHandler(int sign) {
  19. printf("Received Ctrl-C\n");
  20. running = 0;
  21. }
  22. static UA_ByteString loadCertificate(void) {
  23. UA_ByteString certificate = UA_STRING_NULL;
  24. FILE *fp = NULL;
  25. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  26. fp=fopen("localhost.der", "rb");
  27. if(!fp) {
  28. errno = 0; // we read errno also from the tcp layer...
  29. return certificate;
  30. }
  31. fseek(fp, 0, SEEK_END);
  32. certificate.length = ftell(fp);
  33. certificate.data = malloc(certificate.length*sizeof(UA_Byte));
  34. if(!certificate.data)
  35. return certificate;
  36. fseek(fp, 0, SEEK_SET);
  37. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  38. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  39. fclose(fp);
  40. return certificate;
  41. }
  42. static void testCallback(UA_Server *server, void *data) {
  43. logger.log_info(UA_LOGGERCATEGORY_USERLAND, "testcallback");
  44. }
  45. int main(int argc, char** argv) {
  46. signal(SIGINT, stopHandler); /* catches ctrl-c */
  47. UA_Server *server = UA_Server_new();
  48. logger = Logger_Stdout_new();
  49. UA_Server_setLogger(server, logger);
  50. UA_Server_setServerCertificate(server, loadCertificate());
  51. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  52. UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL, .work.methodCall = {.method = testCallback, .data = NULL} };
  53. UA_Server_addRepeatedWorkItem(server, &work, 20000000, NULL); // call every 2 sec
  54. // add a variable node to the adresspace
  55. UA_Int32 *myInteger = UA_Int32_new();
  56. *myInteger = 42;
  57. UA_Variant *myIntegerVariant = UA_Variant_new();
  58. UA_Variant_setValue(myIntegerVariant, myInteger, &UA_TYPES[UA_TYPES_INT32]);
  59. UA_QualifiedName myIntegerName;
  60. UA_QUALIFIEDNAME_ASSIGN(myIntegerName, "the answer");
  61. UA_Server_addVariableNode(server, myIntegerVariant, &UA_NODEID_NULL, &myIntegerName,
  62. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  63. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  64. #ifdef BENCHMARK
  65. UA_UInt32 nodeCount = 500;
  66. char str[15];
  67. for(UA_UInt32 i = 0;i<nodeCount;i++) {
  68. UA_Int32 *data = UA_Int32_new();
  69. *data = 42;
  70. UA_Variant *variant = UA_Variant_new();
  71. UA_Variant_setValue(variant, data, &UA_TYPES[UA_TYPES_INT32]);
  72. UA_QualifiedName *nodeName = UA_QualifiedName_new();
  73. sprintf(str,"%d",i);
  74. UA_QualifiedName_copycstring(str, nodeName);
  75. UA_Server_addVariableNode(server, variant, &UA_NODEID_NULL, nodeName,
  76. &UA_NODEID_STATIC(0, UA_NS0ID_OBJECTSFOLDER),
  77. &UA_NODEID_STATIC(0, UA_NS0ID_ORGANIZES));
  78. }
  79. #endif
  80. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  81. UA_Server_delete(server);
  82. return retval;
  83. }