tutorial_pubsub_connection.c 3.7 KB

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