tutorial_server_firststeps.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <ua_server.h>
  22. #include <ua_config_default.h>
  23. #include <ua_log_stdout.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. 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_ServerConfig *config = UA_ServerConfig_new_default();
  35. UA_Server *server = UA_Server_new(config);
  36. UA_StatusCode retval = UA_Server_run(server, &running);
  37. UA_Server_delete(server);
  38. UA_ServerConfig_delete(config);
  39. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  40. }
  41. /**
  42. * This is all that is needed for a simple OPC UA server. With the GCC compiler,
  43. * the following command produces an executable:
  44. *
  45. * .. code-block:: bash
  46. *
  47. * $ gcc -std=c99 open62541.c myServer.c -o myServer
  48. *
  49. * In a MinGW environment, the Winsock library must be added.
  50. *
  51. * .. code-block:: bash
  52. *
  53. * $ gcc -std=c99 open62541.c myServer.c -lws2_32 -o myServer.exe
  54. *
  55. * Now start the server (stop with ctrl-c):
  56. *
  57. * .. code-block:: bash
  58. *
  59. * $ ./myServer
  60. *
  61. * You have now compiled and run your first OPC UA server. You can go ahead and
  62. * browse the information model with client. The server is listening on
  63. * ``opc.tcp://localhost:4840``. In the next two sections, we will continue to
  64. * explain the different parts of the code in detail.
  65. *
  66. * Server Configuration and Plugins
  67. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  68. *
  69. * *open62541* provides a flexible framework for building OPC UA servers and
  70. * clients. The goals is to have a core library that accommodates for all use
  71. * cases and runs on all platforms. Users can then adjust the library to fit
  72. * their use case via configuration and by developing (platform-specific)
  73. * plugins. The core library is based on C99 only and does not even require
  74. * basic POSIX support. For example, the lowlevel networking code is implemented
  75. * as an exchangeable plugin. But don't worry. *open62541* provides plugin
  76. * implementations for most platforms and sensible default configurations
  77. * out-of-the-box.
  78. *
  79. * In the above server code, we simply take the default server configuration and
  80. * add a single TCP network layer that is listerning on port 4840.
  81. *
  82. * Server Lifecycle
  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. [#f1]_
  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. */