Skip to content

Commit

Permalink
refactor(server): Simplify naming conventions in the DiscoveryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Nov 14, 2023
1 parent 09317bc commit 2b3c3eb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/server/ua_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ UA_DiscoveryManager_free(UA_Server *server,
return UA_STATUSCODE_BADINTERNALERROR;
}

registeredServer_list_entry *rs, *rs_tmp;
registeredServer *rs, *rs_tmp;
LIST_FOREACH_SAFE(rs, &dm->registeredServers, pointers, rs_tmp) {
LIST_REMOVE(rs, pointers);
UA_RegisteredServer_clear(&rs->registeredServer);
UA_free(rs);
}

# ifdef UA_ENABLE_DISCOVERY_MULTICAST
serverOnNetwork_list_entry *son, *son_tmp;
serverOnNetwork *son, *son_tmp;
LIST_FOREACH_SAFE(son, &dm->serverOnNetwork, pointers, son_tmp) {
LIST_REMOVE(son, pointers);
UA_ServerOnNetwork_clear(&son->serverOnNetwork);
Expand Down Expand Up @@ -108,7 +108,7 @@ UA_DiscoveryManager_cleanupTimedOut(UA_Server *server,
if(server->config.discoveryCleanupTimeout)
timedOut -= server->config.discoveryCleanupTimeout * UA_DATETIME_SEC;

registeredServer_list_entry *current, *temp;
registeredServer *current, *temp;
LIST_FOREACH_SAFE(current, &dm->registeredServers, pointers, temp) {
UA_Boolean semaphoreDeleted = false;

Expand Down
20 changes: 10 additions & 10 deletions src/server/ua_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ _UA_BEGIN_DECLS

#ifdef UA_ENABLE_DISCOVERY

typedef struct registeredServer_list_entry {
LIST_ENTRY(registeredServer_list_entry) pointers;
typedef struct registeredServer {
LIST_ENTRY(registeredServer) pointers;
UA_RegisteredServer registeredServer;
UA_DateTime lastSeen;
} registeredServer_list_entry;
} registeredServer;

/* Store async register service calls. So we can cancel outstanding requests
* during shutdown. */
Expand Down Expand Up @@ -55,19 +55,19 @@ typedef struct {
* [hostname]. A [ip].
*/

typedef struct serverOnNetwork_list_entry {
LIST_ENTRY(serverOnNetwork_list_entry) pointers;
typedef struct serverOnNetwork {
LIST_ENTRY(serverOnNetwork) pointers;
UA_ServerOnNetwork serverOnNetwork;
UA_DateTime created;
UA_DateTime lastSeen;
UA_Boolean txtSet;
UA_Boolean srvSet;
char* pathTmp;
} serverOnNetwork_list_entry;
} serverOnNetwork;

#define SERVER_ON_NETWORK_HASH_SIZE 1000
typedef struct serverOnNetwork_hash_entry {
serverOnNetwork_list_entry* entry;
serverOnNetwork *entry;
struct serverOnNetwork_hash_entry* next;
} serverOnNetwork_hash_entry;

Expand All @@ -83,7 +83,7 @@ struct UA_DiscoveryManager {
/* Outstanding requests. So they can be cancelled during shutdown. */
asyncRegisterRequest registerRequests[UA_MAXREGISTERREQUESTS];

LIST_HEAD(, registeredServer_list_entry) registeredServers;
LIST_HEAD(, registeredServer) registeredServers;
size_t registeredServersSize;
UA_Server_registerServerCallback registerServerCallback;
void* registerServerCallbackData;
Expand All @@ -100,7 +100,7 @@ struct UA_DiscoveryManager {
* message was from itself */
UA_String selfFqdnMdnsRecord;

LIST_HEAD(, serverOnNetwork_list_entry) serverOnNetwork;
LIST_HEAD(, serverOnNetwork) serverOnNetwork;

UA_UInt32 serverOnNetworkRecordIdCounter;
UA_DateTime serverOnNetworkRecordIdLastReset;
Expand Down Expand Up @@ -145,7 +145,7 @@ UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
const char *fqdnMdnsRecord,
const char *serverName,
size_t serverNameLen,
struct serverOnNetwork_list_entry **addedEntry);
struct serverOnNetwork **addedEntry);

UA_StatusCode
UA_DiscoveryManager_removeEntryFromServersOnNetwork(UA_DiscoveryManager *dm,
Expand Down
27 changes: 14 additions & 13 deletions src/server/ua_discovery_mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# include <netdb.h> // for recvfrom in cygwin
#endif

static struct serverOnNetwork_list_entry *
static struct serverOnNetwork *
mdns_record_add_or_get(UA_DiscoveryManager *dm, const char *record, const char *serverName,
size_t serverNameLen, UA_Boolean createNew) {
UA_UInt32 hashIdx = UA_ByteString_hash(0, (const UA_Byte*)record,
Expand All @@ -60,9 +60,10 @@ mdns_record_add_or_get(UA_DiscoveryManager *dm, const char *record, const char *
if(!createNew)
return NULL;

struct serverOnNetwork_list_entry *listEntry;
UA_StatusCode retval = UA_DiscoveryManager_addEntryToServersOnNetwork(dm, record, serverName,
serverNameLen, &listEntry);
struct serverOnNetwork *listEntry;
UA_StatusCode retval =
UA_DiscoveryManager_addEntryToServersOnNetwork(dm, record, serverName,
serverNameLen, &listEntry);
if (retval != UA_STATUSCODE_GOOD)
return NULL;

Expand All @@ -75,8 +76,8 @@ UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
const char *fqdnMdnsRecord,
const char *serverName,
size_t serverNameLen,
struct serverOnNetwork_list_entry **addedEntry) {
struct serverOnNetwork_list_entry *entry =
struct serverOnNetwork **addedEntry) {
struct serverOnNetwork *entry =
mdns_record_add_or_get(dm, fqdnMdnsRecord, serverName,
serverNameLen, false);
if(entry) {
Expand All @@ -89,8 +90,8 @@ UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
"Multicast DNS: Add entry to ServersOnNetwork: %s (%*.s)",
fqdnMdnsRecord, (int)serverNameLen, serverName);

struct serverOnNetwork_list_entry *listEntry = (serverOnNetwork_list_entry*)
UA_malloc(sizeof(struct serverOnNetwork_list_entry));
struct serverOnNetwork *listEntry = (serverOnNetwork*)
UA_malloc(sizeof(struct serverOnNetwork));
if(!listEntry)
return UA_STATUSCODE_BADOUTOFMEMORY;
listEntry->created = UA_DateTime_now();
Expand Down Expand Up @@ -189,7 +190,7 @@ UA_DiscoveryManager_removeEntryFromServersOnNetwork(UA_DiscoveryManager *dm,
"Multicast DNS: Remove entry from ServersOnNetwork: %s (%*.s)",
fqdnMdnsRecord, (int)serverNameLen, serverName);

struct serverOnNetwork_list_entry *entry =
struct serverOnNetwork *entry =
mdns_record_add_or_get(dm, fqdnMdnsRecord, serverName,
serverNameLen, false);
if(!entry)
Expand Down Expand Up @@ -248,7 +249,7 @@ mdns_append_path_to_url(UA_String *url, const char *path) {

static void
setTxt(UA_DiscoveryManager *dm, const struct resource *r,
struct serverOnNetwork_list_entry *entry) {
struct serverOnNetwork *entry) {
entry->txtSet = true;
xht_t *x = txt2sd(r->rdata, r->rdlength);
char *path = (char *) xht_get(x, "path");
Expand Down Expand Up @@ -307,7 +308,7 @@ setTxt(UA_DiscoveryManager *dm, const struct resource *r,
/* [servername]-[hostname]._opcua-tcp._tcp.local. 86400 IN SRV 0 5 port [hostname]. */
static void
setSrv(UA_DiscoveryManager *dm, const struct resource *r,
struct serverOnNetwork_list_entry *entry) {
struct serverOnNetwork *entry) {
entry->srvSet = true;

/* The specification Part 12 says: The hostname maps onto the SRV record
Expand Down Expand Up @@ -373,7 +374,7 @@ mdns_record_received(const struct resource *r, void *data) {
servernameLen--; /* remove point */

/* Get entry */
struct serverOnNetwork_list_entry *entry =
struct serverOnNetwork *entry =
mdns_record_add_or_get(dm, r->name, r->name,
servernameLen, r->ttl > 0);
if(!entry)
Expand Down Expand Up @@ -1213,7 +1214,7 @@ UA_Discovery_addRecord(UA_DiscoveryManager *dm, const UA_String *servername,
return UA_STATUSCODE_BADOUTOFMEMORY;
}

struct serverOnNetwork_list_entry *listEntry;
struct serverOnNetwork *listEntry;
/* The servername is servername + hostname. It is the same which we get
* through mDNS and therefore we need to match servername */
UA_StatusCode retval =
Expand Down
62 changes: 31 additions & 31 deletions src/server/ua_services_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void Service_FindServers(UA_Server *server, UA_Session *session,
UA_ApplicationDescription_copy(&server->config.applicationDescription,
&response->servers[pos++]);

registeredServer_list_entry* current;
registeredServer *current;
LIST_FOREACH(current, &dm->registeredServers, pointers) {
UA_Boolean usable = (request->serverUrisSize == 0);
if(!usable) {
Expand Down Expand Up @@ -194,7 +194,7 @@ void Service_FindServers(UA_Server *server, UA_Session *session,
static UA_Boolean
entryMatchesCapabilityFilter(size_t serverCapabilityFilterSize,
UA_String *serverCapabilityFilter,
serverOnNetwork_list_entry* current) {
serverOnNetwork *current) {
/* If the entry has less capabilities defined than the filter, there's no match */
if(serverCapabilityFilterSize > current->serverOnNetwork.serverCapabilitiesSize)
return false;
Expand Down Expand Up @@ -249,7 +249,7 @@ Service_FindServersOnNetwork(UA_Server *server, UA_Session *session,
/* Iterate over all records and add to filtered list */
UA_UInt32 filteredCount = 0;
UA_STACKARRAY(UA_ServerOnNetwork*, filtered, recordCount);
serverOnNetwork_list_entry* current;
serverOnNetwork *current;
LIST_FOREACH(current, &dm->serverOnNetwork, pointers) {
if(filteredCount >= recordCount)
break;
Expand Down Expand Up @@ -502,13 +502,10 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
return;

/* Find the server from the request in the registered list */
registeredServer_list_entry* current;
registeredServer_list_entry *registeredServer_entry = NULL;
LIST_FOREACH(current, &dm->registeredServers, pointers) {
if(UA_String_equal(&current->registeredServer.serverUri, &requestServer->serverUri)) {
registeredServer_entry = current;
registeredServer *rs = NULL;
LIST_FOREACH(rs, &dm->registeredServers, pointers) {
if(UA_String_equal(&rs->registeredServer.serverUri, &requestServer->serverUri))
break;
}
}

UA_MdnsDiscoveryConfiguration *mdnsConfig = NULL;
Expand Down Expand Up @@ -561,7 +558,8 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
responseHeader->serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
return;
}
memcpy(filePath, requestServer->semaphoreFilePath.data, requestServer->semaphoreFilePath.length );
memcpy(filePath, requestServer->semaphoreFilePath.data,
requestServer->semaphoreFilePath.length );
filePath[requestServer->semaphoreFilePath.length] = '\0';
if(!UA_fileExists( filePath )) {
responseHeader->serviceResult = UA_STATUSCODE_BADSEMPAHOREFILEMISSING;
Expand All @@ -579,7 +577,8 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
#ifdef UA_ENABLE_DISCOVERY_MULTICAST
if(server->config.mdnsEnabled) {
for(size_t i = 0; i < requestServer->discoveryUrlsSize; i++) {
/* create TXT if is online and first index, delete TXT if is offline and last index */
/* create TXT if is online and first index, delete TXT if is offline
* and last index */
UA_Boolean updateTxt = (requestServer->isOnline && i==0) ||
(!requestServer->isOnline && i==requestServer->discoveryUrlsSize);
UA_Discovery_updateMdnsForDiscoveryUrl(dm, mdnsServerName, mdnsConfig,
Expand All @@ -591,11 +590,12 @@ process_RegisterServer(UA_Server *server, UA_Session *session,

if(!requestServer->isOnline) {
// server is shutting down. Remove it from the registered servers list
if(!registeredServer_entry) {
if(!rs) {
// server not found, show warning
UA_LOG_WARNING_SESSION(server->config.logging, session,
"Could not unregister server %.*s. Not registered.",
(int)requestServer->serverUri.length, requestServer->serverUri.data);
(int)requestServer->serverUri.length,
requestServer->serverUri.data);
responseHeader->serviceResult = UA_STATUSCODE_BADNOTHINGTODO;
return;
}
Expand All @@ -608,38 +608,38 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
}

// server found, remove from list
LIST_REMOVE(registeredServer_entry, pointers);
UA_RegisteredServer_clear(&registeredServer_entry->registeredServer);
UA_free(registeredServer_entry);
LIST_REMOVE(rs, pointers);
UA_RegisteredServer_clear(&rs->registeredServer);
UA_free(rs);
dm->registeredServersSize--;
responseHeader->serviceResult = UA_STATUSCODE_GOOD;
return;
}

UA_StatusCode retval = UA_STATUSCODE_GOOD;
if(!registeredServer_entry) {
if(!rs) {
// server not yet registered, register it by adding it to the list
UA_LOG_DEBUG_SESSION(server->config.logging, session, "Registering new server: %.*s",
(int)requestServer->serverUri.length, requestServer->serverUri.data);
UA_LOG_DEBUG_SESSION(server->config.logging, session,
"Registering new server: %.*s",
(int)requestServer->serverUri.length,
requestServer->serverUri.data);

registeredServer_entry =
(registeredServer_list_entry *)UA_malloc(sizeof(registeredServer_list_entry));
if(!registeredServer_entry) {
rs = (registeredServer*)UA_malloc(sizeof(registeredServer));
if(!rs) {
responseHeader->serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
return;
}

LIST_INSERT_HEAD(&dm->registeredServers,
registeredServer_entry, pointers);
LIST_INSERT_HEAD(&dm->registeredServers, rs, pointers);
dm->registeredServersSize++;
} else {
UA_RegisteredServer_clear(&registeredServer_entry->registeredServer);
UA_RegisteredServer_clear(&rs->registeredServer);
}

// Always call the callback, if it is set.
// Previously we only called it if it was a new register call. It may be the case that this endpoint
// registered before, then crashed, restarts and registeres again. In that case the entry is not deleted
// and the callback would not be called.
// Always call the callback, if it is set. Previously we only called it if
// it was a new register call. It may be the case that this endpoint
// registered before, then crashed, restarts and registeres again. In that
// case the entry is not deleted and the callback would not be called.
if(dm->registerServerCallback) {
UA_UNLOCK(&server->serviceMutex);
dm->registerServerCallback(requestServer,
Expand All @@ -650,8 +650,8 @@ process_RegisterServer(UA_Server *server, UA_Session *session,
// copy the data from the request into the list
UA_EventLoop *el = server->config.eventLoop;
UA_DateTime nowMonotonic = el->dateTime_nowMonotonic(el);
UA_RegisteredServer_copy(requestServer, &registeredServer_entry->registeredServer);
registeredServer_entry->lastSeen = nowMonotonic;
UA_RegisteredServer_copy(requestServer, &rs->registeredServer);
rs->lastSeen = nowMonotonic;
responseHeader->serviceResult = retval;
}

Expand Down

0 comments on commit 2b3c3eb

Please sign in to comment.