tutorial_server_firststeps.c 4.6 KB

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