Пример #1
0
        /// <summary>
        /// Reserve devices from service
        /// </summary>
        public bool ReserveDevicesFromService(string DeviceURL, Dictionary <UnrealTargetConstraint, int> DeviceTypes)
        {
            if (String.IsNullOrEmpty(DeviceURL))
            {
                return(false);
            }

            Dictionary <UnrealTargetPlatform, string> DeviceMap = new Dictionary <UnrealTargetPlatform, string>();

            foreach (string Platform in UnrealTargetPlatform.GetValidPlatformNames())
            {
                if (Platform == "PS4" || Platform == "XboxOne")
                {
                    DeviceMap.Add(UnrealTargetPlatform.Parse(Platform), string.Format("{0}-DevKit", Platform));
                }
                else
                {
                    DeviceMap.Add(UnrealTargetPlatform.Parse(Platform), Platform);
                }
            }

            List <string> Devices = new List <string>();

            // convert devices to request list
            foreach (KeyValuePair <UnrealTargetConstraint, int> Entry in DeviceTypes)
            {
                if (Entry.Key.Platform == null)
                {
                    continue;
                }

                if (!DeviceMap.ContainsKey(Entry.Key.Platform.Value))
                {
                    // if an unsupported device, we can't reserve it
                    Log.Info("Unable to reserve service device of type: {0}", Entry.Key);
                    return(false);
                }

                if (!string.IsNullOrEmpty(Entry.Key.Model))
                {
                    // if specific device model, we can't currently reserve it from (legacy) service
                    if (DeviceURL.ToLower().Contains("deviceservice.epicgames.net"))
                    {
                        Log.Info("Unable to reserve service device of model: {0} on legacy service", Entry.Key.Model);
                        return(false);
                    }
                }

                for (int i = 0; i < Entry.Value; i++)
                {
                    // @todo: if any additional reservation requirements, encode constraint into json
                    string Constraint = Entry.Key.PerfSpec.ToString();
                    if (!string.IsNullOrEmpty(Entry.Key.Model))
                    {
                        Constraint = Entry.Key.Model;
                    }
                    Devices.Add(DeviceMap[Entry.Key.Platform.Value] + ":" + Constraint);
                }
            }

            // reserve devices
            Uri ReservationServerUri;

            if (Uri.TryCreate(DeviceURL, UriKind.Absolute, out ReservationServerUri))
            {
                DeviceReservationAutoRenew DeviceReservation = null;

                string PoolID = Globals.WorkerPoolID != -1 ? Globals.WorkerPoolID.ToString() : "";

                try
                {
                    DeviceReservation = new DeviceReservationAutoRenew(DeviceURL, 0, PoolID, Devices.ToArray());
                }
                catch (Exception Ex)
                {
                    Log.Info("Unable to make device registration: {0}", Ex.Message);
                    return(false);
                }

                if (DeviceReservation == null || DeviceReservation.Devices.Count != Devices.Count())
                {
                    return(false);
                }

                // Add target devices from reservation
                List <ITargetDevice> ReservedDevices = new List <ITargetDevice>();
                foreach (var Device in DeviceReservation.Devices)
                {
                    DeviceDefinition Def = new DeviceDefinition();
                    Def.Address    = Device.IPOrHostName;
                    Def.Name       = Device.Name;
                    Def.Platform   = DeviceMap.FirstOrDefault(Entry => Entry.Value == Device.Type).Key;
                    Def.DeviceData = Device.DeviceData;
                    Def.Model      = string.Empty;

                    if (!String.IsNullOrEmpty(Device.PerfSpec) && !Enum.TryParse <EPerfSpec>(Device.PerfSpec, true, out Def.PerfSpec))
                    {
                        throw new AutomationException("Unable to convert perfspec '{0}' into an EPerfSpec", Device.PerfSpec);
                    }

                    ITargetDevice TargetDevice = CreateAndRegisterDeviceFromDefinition(Def);

                    // If a device from service can't be added, fail reservation and cleanup devices
                    // @todo: device problem reporting, requesting additional devices
                    if (TargetDevice == null)
                    {
                        ReportDeviceError(Device.Name, "CreateDeviceError");

                        // If some devices from reservation have been created, release them which will also dispose of reservation
                        if (ReservedDevices.Count > 0)
                        {
                            ReleaseDevices(ReservedDevices);
                        }
                        else
                        {
                            // otherwise, no devices have been creation so just cancel this reservation
                            DeviceReservation.Dispose();
                        }

                        Log.Info("Unable to make device registration: device registration failed for {0}:{1}", Def.Platform, Def.Name);
                        return(false);
                    }
                    else
                    {
                        ReservedDevices.Add(TargetDevice);
                    }

                    ServiceDeviceInfo[TargetDevice]   = Def;
                    ServiceReservations[TargetDevice] = DeviceReservation;
                }

                Log.Info("Successfully reserved service devices");
                Devices.ForEach(Device => Log.Verbose("    Device: {0}", Device));

                return(true);
            }

            Log.Info("Unable to reserve service devices:");
            Devices.ForEach(Device => Log.Info("    Device: {0}", Device));
            return(false);
        }
        /// <summary>
        /// Reserve devices from service
        /// </summary>
        public bool ReserveDevicesFromService(string DeviceURL, Dictionary <UnrealTargetConstraint, int> DeviceTypes)
        {
            if (String.IsNullOrEmpty(DeviceURL))
            {
                return(false);
            }

            Dictionary <UnrealTargetPlatform, string> DeviceMap = new Dictionary <UnrealTargetPlatform, string>()
            {
                // todo: add other platforms and externalize this mapping
                { UnrealTargetPlatform.PS4, "PS4-DevKit" },
                { UnrealTargetPlatform.XboxOne, "XboxOne-DevKit" },
                { UnrealTargetPlatform.Android, "Android" },
                { UnrealTargetPlatform.Switch, "Switch" }
            };

            List <string> Devices = new List <string>();

            // convert devices to request list
            foreach (KeyValuePair <UnrealTargetConstraint, int> Entry in DeviceTypes)
            {
                if (!DeviceMap.ContainsKey(Entry.Key.Platform))
                {
                    // if an unsupported device, we can't reserve it
                    Log.Error("Unable to reserve service device of type: {0}", Entry.Key);
                    return(false);
                }

                for (int i = 0; i < Entry.Value; i++)
                {
                    // @todo: if any additional reservation requirements, encode constraint into json
                    Devices.Add(DeviceMap[Entry.Key.Platform] + ":" + Entry.Key.PerfSpec);
                }
            }

            // reserve devices
            Uri ReservationServerUri;

            if (Uri.TryCreate(DeviceURL, UriKind.Absolute, out ReservationServerUri))
            {
                DeviceReservationAutoRenew DeviceReservation = null;

                try
                {
                    DeviceReservation = new DeviceReservationAutoRenew(DeviceURL, 0, Devices.ToArray());
                }
                catch (Exception Ex)
                {
                    Log.Info("Unable to make device registration: {0}", Ex.Message);
                    return(false);
                }

                if (DeviceReservation == null || DeviceReservation.Devices.Count != Devices.Count())
                {
                    return(false);
                }

                // Add target devices from reservation
                List <ITargetDevice> ReservedDevices = new List <ITargetDevice>();
                foreach (var Device in DeviceReservation.Devices)
                {
                    DeviceDefinition Def = new DeviceDefinition();
                    Def.Address    = Device.IPOrHostName;
                    Def.Name       = Device.Name;
                    Def.Platform   = DeviceMap.FirstOrDefault(Entry => Entry.Value == Device.Type).Key;
                    Def.DeviceData = Device.DeviceData;

                    if (!String.IsNullOrEmpty(Device.PerfSpec) && !Enum.TryParse <EPerfSpec>(Device.PerfSpec, true, out Def.PerfSpec))
                    {
                        throw new AutomationException("Unable to convert perfspec '{0}' into an EPerfSpec", Device.PerfSpec);
                    }

                    ITargetDevice TargetDevice = CreateAndRegisterDeviceFromDefinition(Def);

                    // If a device from service can't be added, fail reservation and cleanup devices
                    // @todo: device problem reporting, requesting additional devices
                    if (TargetDevice == null)
                    {
                        ReportDeviceError(Device.Name, "CreateDeviceError");

                        // If some devices from reservation have been created, release them which will also dispose of reservation
                        if (ReservedDevices.Count > 0)
                        {
                            ReleaseDevices(ReservedDevices);
                        }
                        else
                        {
                            // otherwise, no devices have been creation so just cancel this reservation
                            DeviceReservation.Dispose();
                        }

                        Log.Info("Unable to make device registration: device registration failed for {0}:{1}", Def.Platform, Def.Name);
                        return(false);
                    }
                    else
                    {
                        ReservedDevices.Add(TargetDevice);
                    }

                    ServiceDeviceInfo[TargetDevice]   = Def;
                    ServiceReservations[TargetDevice] = DeviceReservation;
                }

                Log.Info("Successfully reserved service devices");
                Devices.ForEach(Device => Log.Verbose("    Device: {0}", Device));

                return(true);
            }

            Log.Error("Unable to reserve service devices:");
            Devices.ForEach(Device => Log.Error("    Device: {0}", Device));
            return(false);
        }