tutorial_server_firststeps.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <signal.h>
  22. #include "open62541.h"
  23. UA_Boolean running = true;
  24. static void stopHandler(int sig) {
  25. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  26. running = false;
  27. }
  28. int main(void) {
  29. signal(SIGINT, stopHandler);
  30. signal(SIGTERM, stopHandler);
  31. UA_ServerConfig *config = UA_ServerConfig_new_default();
  32. UA_Server *server = UA_Server_new(config);
  33. UA_StatusCode retval = UA_Server_run(server, &running);
  34. UA_Server_delete(server);
  35. UA_ServerConfig_delete(config);
  36. return (int)retval;
  37. }
  38. /**
  39. * This is all that is needed for a simple OPC UA server. With the GCC compiler,
  40. * the following command produces an executable:
  41. *
  42. * .. code-block:: bash
  43. *
  44. * $ gcc -std=c99 open62541.c myServer.c -o myServer
  45. *
  46. * In a MinGW environment, the Winsock library must be added.
  47. *
  48. * .. code-block:: bash
  49. *
  50. * $ gcc -std=c99 open62541.c myServer.c -lws2_32 -o myServer.exe
  51. *
  52. * Now start the server (stop with ctrl-c):
  53. *
  54. * .. code-block:: bash
  55. *
  56. * $ ./myServer
  57. *
  58. * You have now compiled and run your first OPC UA server. You can go ahead and
  59. * browse the information model with client. The server is listening on
  60. * ``opc.tcp://localhost:4840``. In the next two sections, we will continue to
  61. * explain the different parts of the code in detail.
  62. *
  63. * Server Configuration and Plugins
  64. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  65. *
  66. * *open62541* provides a flexible framework for building OPC UA servers and
  67. * clients. The goals is to have a core library that accomodates for all use
  68. * cases and runs on all platforms. Users can then adjust the library to fit
  69. * their use case via configuration and by developing (platform-specific)
  70. * plugins. The core library is based on C99 only and does not even require
  71. * basic POSIX support. For example, the lowlevel networking code is implemented
  72. * as an exchangeable plugin. But don't worry. *open62541* provides plugin
  73. * implementations for most platforms and sensible default configurations
  74. * out-of-the-box.
  75. *
  76. * In the above server code, we simply take the default server configuration and
  77. * add a single TCP network layer that is listerning on port 4840.
  78. *
  79. * Server Lifecycle
  80. * ^^^^^^^^^^^^^^^^
  81. * The code in this example shows the three parts for server lifecycle
  82. * management: Creating a server, running the server, and deleting the server.
  83. * Creating and deleting a server is trivial once the configuration is set up.
  84. * The server is started with ``UA_Server_run``. Internally, the server then
  85. * uses timeouts to schedule regular tasks. Between the timeouts, the server
  86. * listens on the network layer for incoming messages.
  87. *
  88. * You might ask how the server knows when to stop running. For this, we have
  89. * created a global variable ``running``. Furthermore, we have registered the
  90. * method ``stopHandler`` that catches the signal (interrupt) the program
  91. * receives when the operating systems tries to close it. This happens for
  92. * example when you press ctrl-c in a terminal program. The signal handler then
  93. * sets the variable ``running`` to false and the server shuts down once it
  94. * takes back control. [#f1]_
  95. *
  96. * In order to integrated OPC UA in a single-threaded application with its own
  97. * mainloop (for example provided by a GUI toolkit), one can alternatively drive
  98. * the server manually. See the section of the server documentation on
  99. * :ref:`server-lifecycle` for details.
  100. *
  101. * The server configuration and lifecycle management is needed for all servers.
  102. * We will use it in the following tutorials without further comment.
  103. *
  104. * .. [#f1] Be careful with global variables in multi-threaded applications. You
  105. * might want to allocate the ``running`` variable on the heap. */