tutorial_server_firststeps.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Building a Simple Server
  5. * ------------------------
  6. *
  7. * This series of tutorial guide you through your first steps with open62541.
  8. * For compiling the examples, you need a compiler (MS Visual Studio 2015 or
  9. * newer, GCC, Clang and MinGW32 are all known to be working). The compilation
  10. * instructions are given for GCC but should be straightforward to adapt.
  11. *
  12. * It will also be very helpful to install an OPC UA Client with a graphical
  13. * frontend, such as UAExpert by Unified Automation. That will enable you to
  14. * examine the information model of any OPC UA server.
  15. *
  16. * To get started, downdload the open62541 single-file release from
  17. * http://open62541.org or generate it according to the :ref:`build instructions
  18. * <building>` with the "amalgamation" option enabled. From now on, we assume
  19. * you have the ``open62541.c/.h`` files in the current folder. Now create a new
  20. * C source-file called ``myServer.c`` with the following content: */
  21. #include <open62541/plugin/log_stdout.h>
  22. #include <open62541/server.h>
  23. #include <open62541/server_config_default.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. static volatile UA_Boolean running = true;
  27. static void stopHandler(int sig) {
  28. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  29. running = false;
  30. }
  31. int main(void) {
  32. signal(SIGINT, stopHandler);
  33. signal(SIGTERM, stopHandler);
  34. UA_Server *server = UA_Server_new();
  35. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  36. UA_StatusCode retval = UA_Server_run(server, &running);
  37. UA_Server_delete(server);
  38. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  39. }
  40. /**
  41. * This is all that is needed for a simple OPC UA server. With the GCC compiler,
  42. * the following command produces an executable:
  43. *
  44. * .. code-block:: bash
  45. *
  46. * $ gcc -std=c99 open62541.c myServer.c -o myServer
  47. *
  48. * In a MinGW environment, the Winsock library must be added.
  49. *
  50. * .. code-block:: bash
  51. *
  52. * $ gcc -std=c99 open62541.c myServer.c -lws2_32 -o myServer.exe
  53. *
  54. * Now start the server (stop with ctrl-c):
  55. *
  56. * .. code-block:: bash
  57. *
  58. * $ ./myServer
  59. *
  60. * You have now compiled and run your first OPC UA server. You can go ahead and
  61. * browse the information model with client. The server is listening on
  62. * ``opc.tcp://localhost:4840``. In the next two sections, we will continue to
  63. * explain the different parts of the code in detail.
  64. *
  65. * Server Configuration and Plugins
  66. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  67. *
  68. * *open62541* provides a flexible framework for building OPC UA servers and
  69. * clients. The goals is to have a core library that accommodates for all use
  70. * cases and runs on all platforms. Users can then adjust the library to fit
  71. * their use case via configuration and by developing (platform-specific)
  72. * plugins. The core library is based on C99 only and does not even require
  73. * basic POSIX support. For example, the lowlevel networking code is implemented
  74. * as an exchangeable plugin. But don't worry. *open62541* provides plugin
  75. * implementations for most platforms and sensible default configurations
  76. * out-of-the-box.
  77. *
  78. * In the above server code, we simply take the default server configuration and
  79. * add a single TCP network layer that is listerning on port 4840.
  80. *
  81. * Server Lifecycle
  82. * ^^^^^^^^^^^^^^^^
  83. *
  84. * The code in this example shows the three parts for server lifecycle
  85. * management: Creating a server, running the server, and deleting the server.
  86. * Creating and deleting a server is trivial once the configuration is set up.
  87. * The server is started with ``UA_Server_run``. Internally, the server then
  88. * uses timeouts to schedule regular tasks. Between the timeouts, the server
  89. * listens on the network layer for incoming messages.
  90. *
  91. * You might ask how the server knows when to stop running. For this, we have
  92. * created a global variable ``running``. Furthermore, we have registered the
  93. * method ``stopHandler`` that catches the signal (interrupt) the program
  94. * receives when the operating systems tries to close it. This happens for
  95. * example when you press ctrl-c in a terminal program. The signal handler then
  96. * sets the variable ``running`` to false and the server shuts down once it
  97. * takes back control.
  98. *
  99. * In order to integrated OPC UA in a single-threaded application with its own
  100. * mainloop (for example provided by a GUI toolkit), one can alternatively drive
  101. * the server manually. See the section of the server documentation on
  102. * :ref:`server-lifecycle` for details.
  103. *
  104. * The server configuration and lifecycle management is needed for all servers.
  105. * We will use it in the following tutorials without further comment.
  106. */