High Performance OPC UA Server SDK  1.2.0.193
server_main.c
/*****************************************************************************
* *
* Copyright (c) 2006-2016 Unified Automation GmbH. All rights reserved. *
* *
* Software License Agreement ("SLA") Version 2.6 *
* *
* Unless explicitly acquired and licensed from Licensor under another *
* license, the contents of this file are subject to the Software License *
* Agreement ("SLA") Version 2.6, or subsequent versions as allowed by the *
* SLA, and You may not copy or use this file in either source code or *
* executable form, except in compliance with the terms and conditions of *
* the SLA. *
* *
* All software distributed under the SLA is provided strictly on an "AS *
* IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, *
* AND LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT *
* LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR *
* PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the SLA for specific *
* language governing rights and limitations under the SLA. *
* *
* The complete license agreement can be found here: *
* http://unifiedautomation.com/License/SLA/2.6/ *
* *
*****************************************************************************/
#include <stdio.h>
#include <uaapplication/application.h>
#include <uaserver/init.h>
#include <trace/trace.h>
#include <appconfig.h>
#include <uaprovider/server/uaprovider_server.h>
#include <platform/hostname.h>
#include <platform/shutdown.h>
#include <crashhandler/crashhandler.h>
#include <memory/memory_layout.h>
#include <memory/memory.h>
#include "custom_provider.h"
#include "../args.h"
struct uaapplication g_app;
struct appconfig g_appconfig;
static struct uaserver_provider g_provider[] = {
{-1, uaprovider_server_init},
{-1, custom_provider_init}
};
int server_main_clear(void)
{
uaserver_clear(g_provider, countof(g_provider));
return 0;
}
int server_main_init(int trace_level, int facility_mask, const char *config_file)
{
int ret;
unsigned int i;
static char fqdn[64];
struct ua_string tmp;
ua_get_fqdn(fqdn, sizeof(fqdn));
g_appconfig.manufacturer_name = "Unified Automation GmbH";
g_appconfig.product_name = "UaSDK High Performance - Lesson04";
g_appconfig.product_uri = "urn:UnifiedAutomation:UaSdkHP:Lesson04";
g_appconfig.application_name = "UaSDK High Performance - Lesson04";
g_appconfig.application_uri = "urn:UnifiedAutomation:HPC:Example";
g_appconfig.dns = fqdn;
ret = uaapplication_init(&g_app, trace_level, facility_mask, config_file);
if (ret != 0) {
TRACE_ERROR(TRACE_FAC_APPLICATION, "uaapplication_init failed.\n");
goto out_init;
}
/* replace [hostname] place holder in endpoint urls with fqdn */
for (i = 0; i < g_appconfig.endpoint.num_endpoints; ++i) {
ua_string_set(&tmp, g_appconfig.endpoint.endpoints[i].endpoint_url);
if (ua_string_index_of(&tmp, "[hostname]") != -1) {
ua_string_replace(&tmp, "[hostname]", fqdn);
/* free old endpoint url */
ua_free((void *)g_appconfig.endpoint.endpoints[i].endpoint_url);
/* set new one */
g_appconfig.endpoint.endpoints[i].endpoint_url = ua_strdup(ua_string_const_data(&tmp));
}
}
TRACE_INFO(TRACE_FAC_APPLICATION, "Initializing providers...\n");
ret = uaserver_init(g_provider, countof(g_provider));
if (ret < 0) goto out_server_init;
TRACE_INFO(TRACE_FAC_APPLICATION, "Starting server...\n");
ret = uaserver_start();
if (ret < 0) goto out_server_start;
return 0;
out_server_start:
uaserver_clear(g_provider, countof(g_provider));
out_server_init:
out_init:
return ret;
}
int main(int argc, char *argv[])
{
int ret;
unsigned int i;
struct commandline_args args;
parse_commandline(argc, argv, &args);
ret = server_main_init(args.trace_level, args.facility_mask, args.config_file);
if (ret != 0) {
printf("server_main_init() failed with error 0x%08x\n", ret);
exit(EXIT_FAILURE);
}
printf("Server is up and running.\n");
for (i = 0; i < g_appconfig.endpoint.num_endpoints; ++i) {
printf("Listening on %s\n", g_appconfig.endpoint.endpoints[i].endpoint_url);
}
while (ua_shutdown_should_shutdown() == 0) {
}
TRACE_NOTICE(TRACE_FAC_APPLICATION, "Shutting down server...\n");
server_main_clear();
return 0;
}