tutorial_pubsub_connection.c 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <ua_server.h>
  4. #include <ua_config_default.h>
  5. #include <ua_log_stdout.h>
  6. #include <ua_network_pubsub_udp.h>
  7. #include <signal.h>
  8. #include <stdlib.h>
  9. UA_Boolean running = true;
  10. static void stopHandler(int sign) {
  11. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  12. running = false;
  13. }
  14. /**
  15. * The PubSub connection example demonstrate the PubSub TransportLayer configuration and
  16. * the dynamic creation of PubSub Connections on runtime.
  17. */
  18. int main(void) {
  19. signal(SIGINT, stopHandler);
  20. signal(SIGTERM, stopHandler);
  21. UA_ServerConfig *config = UA_ServerConfig_new_default();
  22. /* Add the PubSubTransportLayer implementation to the server config.
  23. * The PubSubTransportLayer is a factory to create new connections
  24. * on runtime. The UA_PubSubTransportLayer is used for all kinds of
  25. * concrete connections e.g. UDP, MQTT, AMQP...
  26. */
  27. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  28. if(!config->pubsubTransportLayers) {
  29. UA_ServerConfig_delete(config);
  30. return -1;
  31. }
  32. /* It is possible to use multiple PubSubTransportLayers on runtime. The correct factory
  33. * is selected on runtime by the standard defined PubSub TransportProfileUri's.
  34. */
  35. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  36. config->pubsubTransportLayersSize++;
  37. UA_Server *server = UA_Server_new(config);
  38. /* Create a new ConnectionConfig. The addPubSubConnection function takes the
  39. * config and create a new connection. The Connection identifier is
  40. * copied to the NodeId parameter.*/
  41. UA_PubSubConnectionConfig connectionConfig;
  42. memset(&connectionConfig, 0, sizeof(connectionConfig));
  43. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  44. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  45. connectionConfig.enabled = UA_TRUE;
  46. /* The address and interface is part of the standard
  47. * defined UA_NetworkAddressUrlDataType.
  48. */
  49. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  50. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  51. connectionConfig.publisherId.numeric = UA_UInt32_random();
  52. /* Connection options are given as Key/Value Pairs. The available options are
  53. * maybe standard or vendor defined.
  54. */
  55. UA_KeyValuePair connectionOptions[3];
  56. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  57. UA_UInt32 ttl = 10;
  58. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  59. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  60. UA_Boolean loopback = UA_FALSE;
  61. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_UINT32]);
  62. connectionOptions[2].key = UA_QUALIFIEDNAME(0, "reuse");
  63. UA_Boolean reuse = UA_TRUE;
  64. UA_Variant_setScalar(&connectionOptions[2].value, &reuse, &UA_TYPES[UA_TYPES_UINT32]);
  65. connectionConfig.connectionProperties = connectionOptions;
  66. connectionConfig.connectionPropertiesSize = 3;
  67. /* Create a new concrete connection and add the connection
  68. * to the current PubSub configuration.
  69. */
  70. UA_NodeId connectionIdentifier;
  71. UA_StatusCode retval = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  72. if(retval == UA_STATUSCODE_GOOD){
  73. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  74. "The PubSub Connection was created successfully!");
  75. }
  76. retval |= UA_Server_run(server, &running);
  77. UA_Server_delete(server);
  78. UA_ServerConfig_delete(config);
  79. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  80. }