tutorial_pubsub_connection.c 3.8 KB

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