tutorial_pubsub_connection.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <open62541/plugin/log_stdout.h>
  4. #include <open62541/plugin/pubsub_udp.h>
  5. #include <open62541/server.h>
  6. #include <open62541/server_config_default.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_Server *server = UA_Server_new();
  22. UA_ServerConfig *config = UA_Server_getConfig(server);
  23. UA_ServerConfig_setDefault(config);
  24. /* Add the PubSubTransportLayer implementation to the server config.
  25. * The PubSubTransportLayer is a factory to create new connections
  26. * on runtime. The UA_PubSubTransportLayer is used for all kinds of
  27. * concrete connections e.g. UDP, MQTT, AMQP...
  28. */
  29. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  30. if(!config->pubsubTransportLayers) {
  31. UA_Server_delete(server);
  32. return -1;
  33. }
  34. /* It is possible to use multiple PubSubTransportLayers on runtime. The correct factory
  35. * is selected on runtime by the standard defined PubSub TransportProfileUri's. */
  36. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  37. config->pubsubTransportLayersSize++;
  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. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  49. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  50. connectionConfig.publisherId.numeric = UA_UInt32_random();
  51. /* Connection options are given as Key/Value Pairs. The available options are
  52. * maybe standard or vendor defined. */
  53. UA_KeyValuePair connectionOptions[3];
  54. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  55. UA_UInt32 ttl = 10;
  56. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  57. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  58. UA_Boolean loopback = UA_FALSE;
  59. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_UINT32]);
  60. connectionOptions[2].key = UA_QUALIFIEDNAME(0, "reuse");
  61. UA_Boolean reuse = UA_TRUE;
  62. UA_Variant_setScalar(&connectionOptions[2].value, &reuse, &UA_TYPES[UA_TYPES_UINT32]);
  63. connectionConfig.connectionProperties = connectionOptions;
  64. connectionConfig.connectionPropertiesSize = 3;
  65. /* Create a new concrete connection and add the connection
  66. * to the current PubSub configuration. */
  67. UA_NodeId connectionIdentifier;
  68. UA_StatusCode retval = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  69. if(retval == UA_STATUSCODE_GOOD){
  70. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  71. "The PubSub Connection was created successfully!");
  72. }
  73. retval |= UA_Server_run(server, &running);
  74. UA_Server_delete(server);
  75. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  76. }