tutorial_server_firststeps.c 4.7 KB

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