/// <summary> /// Sends changes notifications to the connected profile server. /// </summary> /// <param name="Change">Change to send.</param> /// <returns>true if the function succeeds, false otherwise.</returns> public async Task <bool> SendChangeNotification(NeighbourhoodChange Change) { return(await SendChangeNotifications(new List <NeighbourhoodChange>() { Change })); }
/// <summary> /// Removes servers from the profile server's neighborhood. /// </summary> /// <param name="NeighborhoodList">List of servers to cancel neighbor connection with..</param> /// <returns>true if the function succeeds, false otherwise.</returns> public bool CancelNeighborhood(List <ProfileServer> NeighborhoodList) { log.Trace("(NeighborhoodList.Count:{0})", NeighborhoodList.Count); List <NeighbourhoodChange> changes = new List <NeighbourhoodChange>(); bool res = false; lock (neighborsLock) { foreach (ProfileServer ps in NeighborhoodList) { // Do not process your own profile server. if (ps.Name == profileServer.Name) { continue; } ps.Lock(); if (ps.IsInitialized()) { // Ignore servers that are not in the neighborhood. if (neighbors.ContainsKey(ps.Name)) { neighbors.Remove(ps.Name); log.Trace("Profile server '{0}' removed from the neighborhood of server '{1}'.", ps.Name, profileServer.Name); // This neighbor server already runs, so we know its profile // we can inform our profile server about it. NeighbourhoodChange change = new NeighbourhoodChange(); change.RemovedNodeId = ProtocolHelper.ByteArrayToByteString(ps.GetNetworkId()); changes.Add(change); } } else { // This neighbor server does not run yet, so we do not have its profile. // We will uninstall a possibly installed event. log.Debug("Profile server '{0}' is not initialized yet, uninstalling notification for server '{1}'.", ps.Name, profileServer.Name); ps.UninstallInitializationNeighborhoodNotification(profileServer); } ps.Unlock(); } } if ((connectedProfileServer != null) && connectedProfileServerWantsUpdates) { // If our profile server is running already, removing servers to its neighborhood // ends with sending update notification to the profile server. if (changes.Count > 0) { log.Debug("Sending {0} neighborhood changes to profile server '{1}'.", changes.Count, profileServer.Name); LocProtocolMessage message = connectedProfileServerMessageBuilder.CreateNeighbourhoodChangedNotificationRequest(changes); res = SendMessageAsync(connectedProfileServer, message).Result; } else { log.Debug("No neighborhood changes to send to profile server '{0}'.", profileServer.Name); res = true; } } else { // Our profile server is not started/connected yet, which means we just modified its neighborhood, // and the information about its neighborhood will be send to it once it sends us GetNeighbourNodesByDistanceLocalRequest. log.Debug("Profile server '{0}' is not connected or not fully initialized yet, can't send changes.", profileServer.Name); res = true; } log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Implementation of the test itself. /// </summary> /// <returns>true if the test passes, false otherwise.</returns> public override async Task <bool> RunAsync() { IPAddress ServerIp = (IPAddress)ArgumentValues["Server IP"]; int PrimaryPort = (int)ArgumentValues["primary Port"]; int BasePort = (int)ArgumentValues["Base Port"]; int LocPort = (int)ArgumentValues["LOC Port"]; log.Trace("(ServerIp:'{0}',PrimaryPort:{1},BasePort:{2},LocPort:{3})", ServerIp, PrimaryPort, BasePort, LocPort); bool res = false; Passed = false; ProtocolClient client = new ProtocolClient(); ProfileServer profileServer = null; LocServer locServer = null; try { MessageBuilder mb = client.MessageBuilder; // Step 1 log.Trace("Step 1"); profileServer = new ProfileServer("TestProfileServer", ServerIp, BasePort, client.GetIdentityKeys(), new ProfileServerProtocol.GpsLocation(1, 2)); bool profileServerStartOk = profileServer.Start(); locServer = new LocServer("TestLocServer", ServerIp, LocPort); bool locServerStartOk = locServer.Start(); await locServer.WaitForProfileServerConnectionAsync(); bool step1Ok = profileServerStartOk && locServerStartOk; log.Trace("Step 1: {0}", step1Ok ? "PASSED" : "FAILED"); // Step 2 log.Trace("Step 2"); NeighbourhoodChange change = new NeighbourhoodChange() { AddedNodeInfo = profileServer.GetNodeInfo(LocPort) }; bool changeNotificationOk = await locServer.SendChangeNotification(change); IncomingServerMessage incomingServerMessage = await profileServer.WaitForConversationRequest(ServerRole.ServerNeighbor, ConversationRequest.RequestTypeOneofCase.StartNeighborhoodInitialization); Iop.Profileserver.Message finishRequest = await profileServer.SendFinishNeighborhoodInitializationRequest(incomingServerMessage.Client); incomingServerMessage = await profileServer.WaitForResponse(ServerRole.ServerNeighbor, finishRequest); bool statusOk = incomingServerMessage.IncomingMessage.Response.Status == Iop.Profileserver.Status.Ok; bool step2Ok = changeNotificationOk && (finishRequest != null) && statusOk; log.Trace("Step 2: {0}", step2Ok ? "PASSED" : "FAILED"); Passed = step1Ok && step2Ok; res = true; } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } client.Dispose(); foreach (ProtocolClient protocolClient in TestProfiles.Values) { protocolClient.Dispose(); } if (profileServer != null) { profileServer.Shutdown(); } if (locServer != null) { locServer.Shutdown(); } log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Adds neighbors to the profile server. /// </summary> /// <param name="NeighborhoodList">List of all servers in the new neighborhood.</param> /// <returns>true if the function succeeds, false otherwise.</returns> public bool AddNeighborhood(List <ProfileServer> NeighborhoodList) { log.Trace("(NeighborhoodList.Count:{0})", NeighborhoodList.Count); List <NeighbourhoodChange> changes = new List <NeighbourhoodChange>(); bool res = false; lock (neighborsLock) { foreach (ProfileServer ps in NeighborhoodList) { // Do not add your own profile server. if (ps.Name == profileServer.Name) { continue; } // Ignore neighbors that we already have in the list. if (neighbors.ContainsKey(ps.Name)) { log.Debug("Profile server '{0}' already has '{1}' as its neighbor.", profileServer.Name, ps.Name); continue; } ps.Lock(); if (ps.IsInitialized()) { neighbors.Add(ps.Name, ps); log.Debug("Profile server '{0}' added to the neighborhood of server '{1}'.", ps.Name, profileServer.Name); // This neighbor server already runs, so we know its profile // we can inform our profile server about it. NeighbourhoodChange change = new NeighbourhoodChange(); change.AddedNodeInfo = ps.GetNodeInfo(); changes.Add(change); } else { // This neighbor server does not run yet, so we do not have its profile. // We will install an event to be triggered when this server starts. log.Debug("Profile server '{0}' is not initialized yet, installing notification for server '{1}'.", ps.Name, profileServer.Name); ps.InstallInitializationNeighborhoodNotification(profileServer); } ps.Unlock(); } } if ((connectedProfileServer != null) && connectedProfileServerWantsUpdates) { // If our profile server is running already, adding servers to its neighborhood // ends with sending update notification to the profile server. if (changes.Count > 0) { log.Debug("Sending {0} neighborhood changes to profile server '{1}'.", changes.Count, profileServer.Name); Message message = connectedProfileServerMessageBuilder.CreateNeighbourhoodChangedNotificationRequest(changes); res = SendMessageAsync(connectedProfileServer, message).Result; } else { log.Debug("No neighborhood changes to send to profile server '{0}'.", profileServer.Name); res = true; } } else { // Our profile server is not started/connected yet, which means we just modified its neighborhood, // and the information about its neighborhood will be send to it once it sends us GetNeighbourNodesByDistanceLocalRequest. log.Debug("Profile server '{0}' is not connected or not fully initialized yet, can't send changes.", profileServer.Name); res = true; } log.Trace("(-):{0}", res); return(res); }