//[AuthorizeJwt(GuardianApplicationRole.ZoneServer)] //TODO: Eventually we'll need to auth these zoneservers.
        public async Task <IActionResult> RegisterZoneServer([FromBody] ZoneServerRegisterationRequest registerationRequest,
                                                             [FromServices] IZoneInstanceWorkQueue instanceWorkQueue, [FromServices] IZoneServerRepository zoneRepo)
        {
            //TODO: JSON
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            //Conceptually, just because there is no work doesn't mean this is an error
            //Requesting users who were trying to make an instance could have abandoned that request.
            //Instances may eventually free themselves after inactivity and attempt to reregister instead of shutting down at first
            //So we just want to say "Nothing to do right now" so they can sleep and maybe manually shutdown after a timeout period.
            if (instanceWorkQueue.isEmpty)
            {
                return(NoWorkForInstanceResponse());
            }

            //The specification says this could complete immediately
            //with null if no works exists, there is technically a data race condition between checking isEmpty
            //and trying to dequeue so the result may not be predictible.
            ZoneInstanceWorkEntry zoneInstanceWorkEntry = await instanceWorkQueue.DequeueAsync()
                                                          .ConfigureAwait(false);

            //TODO: If anything here fails after dequeueing we could lose CRITICAL data to keep things running
            //We need VERY good failure handling, and to reenter this work request into the queue somehow.
            //Otherwise the request for the instance will be lost and unhandled forever.
            ProjectVersionStage.AssertAlpha();

            if (zoneInstanceWorkEntry == null)
            {
                return(NoWorkForInstanceResponse());
            }

            //TODO: Validate endpoint
            //Since there IS work to do, we can't just tell the zone instance
            //We must register it into the zone server repo
            if (!await zoneRepo.TryCreateAsync(new ZoneInstanceEntryModel(registerationRequest.ZoneServerEndpoint.EndpointAddress, (short)registerationRequest.ZoneServerEndpoint.EndpointPort, zoneInstanceWorkEntry.WorldId)))
            {
                //As stated above, we need good handling for this else
                //we will encounter MAJOR issues.
                return(NoWorkForInstanceResponse());
            }

            //Success
            return(Ok(new ZoneServerRegisterationResponse(zoneInstanceWorkEntry.WorldId)));
        }
Beispiel #2
0
 /// <inheritdoc />
 public async Task <ZoneServerRegisterationResponse> RegisterZoneServer(ZoneServerRegisterationRequest request)
 {
     return(await(await GetService().ConfigureAwait(false)).RegisterZoneServer(request).ConfigureAwait(false));
 }