/// <summary>
		/// Get all slaves registred in system.
		/// </summary>
		/// <returns>Array of <c>OrationiSlaveItem</c>.</returns>
		public OrationiSlaveItem[] GetSlavesList()
		{
			using (MasterContext masterDb = new MasterContext())
			{
				IEnumerable<SlaveDescription> slaveDescriptions = masterDb.Slaves;
				OrationiSlaveItem[] orationiSlaveItems = new OrationiSlaveItem[slaveDescriptions.Count()];

				int index = 0;
				foreach (SlaveDescription slaveDescription in slaveDescriptions)
				{
					OrationiSlaveItem orationiSlaveItem = new OrationiSlaveItem
					{
						Address = slaveDescription.Address,
						Description = slaveDescription.Description,
						Name = slaveDescription.Name,
						Id = slaveDescription.Id
					};
					orationiSlaveItem.LastActivity = _orationiEngine.WhenSlaveWasActive(orationiSlaveItem.Address);
					orationiSlaveItems[index] = orationiSlaveItem;
					index++;
				}

				return orationiSlaveItems;
			}
		}
		/// <summary>
		/// Get all modules registred in system.
		/// </summary>
		/// <returns>Array of <c>ModuleItem</c>.</returns>
		public ModuleItem[] GetModulesList()
		{
			using (MasterContext masterDb = new MasterContext())
			{
				IEnumerable<ModuleDescription> modules = masterDb.Modules;
				ModuleItem[] result = new ModuleItem[modules.Count()];

				int index = 0;
				foreach (ModuleDescription moduleDescription in modules)
				{
					ModuleItem moduleItem = new ModuleItem
					{
						Id = moduleDescription.Id,
						Name = moduleDescription.Name
					};
					result[index] = moduleItem;
					index++;
				}
				return result;
			}
		}
		private void InitializeDataContext()
		{
			using (MasterContext masterDb = new MasterContext())
				masterDb.Database.EnsureCreated();
		}
		/// <summary>
		/// Return stream of package with module.
		/// </summary>
		/// <param name="moduleId">Module identificator.</param>
		/// <param name="sessionId">Session identificator.</param>
		/// <returns></returns>
		public Stream GetModule(int moduleId, string sessionId)
		{
			if (!SlaveConnections.ContainsKey(sessionId))
				throw new Exception("Slave connection not found.");

			if (!SlaveConnections[sessionId].HasModule(moduleId))
				throw new Exception("Requested module doesn't assigned to current slave.");

			ModuleVersion moduleVersion;
			using (MasterContext masterDb = new MasterContext())
			{
				moduleVersion = masterDb.ModuleVersions.Where(m => m.ModuleId == moduleId)
					.OrderByDescending(m => m.Major)
					.ThenByDescending(m => m.Minor)
					.ThenByDescending(m => m.Build)
					.ThenByDescending(m => m.Revision)
					.FirstOrDefault();

				if (moduleVersion == null)
					throw new Exception("Module version not found.");
			}

			using (FileStream stream = new FileStream(moduleVersion.Path, FileMode.Open, FileAccess.Read, FileShare.Read))
				return stream;
		}
		/// <summary>
		/// Get existing versions of module.
		/// </summary>
		/// <param name="module">Module identifier.</param>
		/// <returns>Array of <c>ModuleVersionItem</c>.</returns>
		public ModuleVersionItem[] GetModuleVerionsList(string module)
		{
			int moduleId;
			if (!int.TryParse(module, out moduleId))
				return new ModuleVersionItem[0];

			using (MasterContext masterDb = new MasterContext())
			{

				IEnumerable<ModuleVersion> moduleVersions = masterDb.ModuleVersions.Where(mv => mv.ModuleId == moduleId);
				ModuleVersionItem[] result = new ModuleVersionItem[moduleVersions.Count()];

				int index = 0;
				foreach (ModuleVersion moduleVersion in moduleVersions)
				{
					ModuleVersionItem moduleVersionItem = new ModuleVersionItem
					{
						ModuleId = moduleId,
						Major = moduleVersion.Major,
						Minor = moduleVersion.Minor,
						Build = moduleVersion.Build,
						Revision = moduleVersion.Revision
					};
					result[index] = moduleVersionItem;
					index++;
				}
				return result;
			}
		}
		/// <summary>
		/// Upload version of module package.
		/// </summary>
		/// <param name="module">Module ideintifier.</param>
		/// <param name="major">Major version.</param>
		/// <param name="minor">Minor version.</param>
		/// <param name="build">Build version.</param>
		/// <param name="revision">Revision.</param>
		/// <param name="packageStream">Stream with module package.</param>
		public void UploadModuleVersion(string module, string major, string minor, string build, string revision, Stream packageStream)
		{
			int moduleId;
			int majorNumber;
			int minorNumber = 0;
			int buildNumber = 0;
			int revisionNumber = 0;

			if (string.IsNullOrEmpty(module))
				return;

			if (!int.TryParse(module, out moduleId))
				return;

			if (string.IsNullOrEmpty(major))
				return;

			if (!int.TryParse(major, out majorNumber))
				return;

			if (!string.IsNullOrEmpty(minor))
			{
				if (!int.TryParse(minor, out minorNumber))
					return;
			}

			if (!string.IsNullOrEmpty(build))
			{
				if (!int.TryParse(build, out buildNumber))
					return;
			}

			if (!string.IsNullOrEmpty(revision))
			{
				if (!int.TryParse(revision, out revisionNumber))
					return;
			}

			ModuleVersion moduleVersion = new ModuleVersion
			{
				ModuleId = moduleId,
				Major = majorNumber,
				Minor = minorNumber,
				Build = buildNumber,
				Revision = revisionNumber
			};

			_orationiEngine.SaveModule(moduleVersion, packageStream);

			using (MasterContext masterDb = new MasterContext())
			{
				masterDb.ModuleVersions.Add(moduleVersion);
				masterDb.SaveChanges();
			}
		}
		/// <summary>
		/// Unassign module from slave.
		/// </summary>
		/// <param name="slave">Slave identifier.</param>
		/// <param name="module">Mpdule identifier.</param>
		public void UnassignModule(string slave, string module)
		{
			Guid slaveId;
			int moduleId;

			if (string.IsNullOrEmpty(slave))
				return;

			if (!Guid.TryParse(slave, out slaveId))
				return;

			if (string.IsNullOrEmpty(module))
				return;

			if (!int.TryParse(module, out moduleId))
				return;

			using (MasterContext masterDb = new MasterContext())
			{
				SlaveModule slaveModule = masterDb.SlaveModules.FirstOrDefault(s => s.SlaveId == slaveId && s.ModuleId == moduleId);
				if (slaveModule == null)
					return;

				masterDb.SlaveModules.Remove(slaveModule);
				masterDb.SaveChanges();

				_orationiEngine.UndeployModule(slaveId, moduleId);
			}
		}
		/// <summary>
		/// Assugn module to slave.
		/// </summary>
		/// <param name="slave">Slave identifier.</param>
		/// <param name="module">Module identifier.</param>
		/// <returns></returns>
		public AssignedModule AssignModule(string slave, string module)
		{
			Guid slaveId;
			int moduleId;

			if (string.IsNullOrEmpty(slave))
				return null;

			if (!Guid.TryParse(slave, out slaveId))
				return null;

			if (string.IsNullOrEmpty(module))
				return null;

			if (!int.TryParse(module, out moduleId))
				return null;

			using (MasterContext masterDb = new MasterContext())
			{
				SlaveModule slaveModule = masterDb.SlaveModules.FirstOrDefault(s => s.SlaveId == slaveId && s.ModuleId == moduleId);
				if (slaveModule == null)
				{
					slaveModule = new SlaveModule()
					{
						ModuleId = moduleId,
						SlaveId = slaveId
					};
					masterDb.SlaveModules.Add(slaveModule);
					masterDb.SaveChanges();
				}
				ModuleVersionItem moduleVersionItem = new ModuleVersionItem { ModuleId = moduleId };

				ModuleVersion moduleVersion = masterDb.ModuleVersions.Where(mv => mv.ModuleId == moduleId)
					.OrderByDescending(mv => mv.Major)
					.ThenByDescending(mv => mv.Minor)
					.ThenByDescending(mv => mv.Build)
					.ThenByDescending(mv => mv.Revision)
					.FirstOrDefault();

				if (moduleVersion != null)
				{
					moduleVersionItem.Major = moduleVersion.Major;
					moduleVersionItem.Minor = moduleVersion.Minor;
					moduleVersionItem.Build = moduleVersion.Build;
					moduleVersionItem.Revision = moduleVersion.Revision;
				};

				_orationiEngine.PushModule(slaveId, moduleVersion);

				AssignedModule assignedModule = new AssignedModule
				{
					SlaveId = slaveId,
					ModuleVersion = moduleVersionItem
				};

				return assignedModule;
			}
		}
		/// <summary>
		/// Remove module from system.
		/// </summary>
		/// <param name="module">Module identifier.</param>
		/// <returns>Array of <c>AssignedModule</c> if module wasn't assigned. Empty array in case of sucessful unregistration.</returns>
		public AssignedModule[] UnregisterModule(string module)
		{
			int moduleId;

			if (string.IsNullOrEmpty(module))
				return new AssignedModule[0];

			if (!int.TryParse(module, out moduleId))
				return new AssignedModule[0];

			using (MasterContext masterDb = new MasterContext())
			{
				IEnumerable<SlaveModule> slaveModules = masterDb.SlaveModules.Where(s => s.ModuleId == moduleId);
				AssignedModule[] assignedModules = new AssignedModule[slaveModules.Count()];
				int index = 0;
				foreach (SlaveModule slaveModule in slaveModules)
				{
					AssignedModule assignedModule = new AssignedModule();
					assignedModule.SlaveId = slaveModule.SlaveId;

					ModuleVersionItem moduleVersionItem = new ModuleVersionItem { ModuleId = moduleId };

					ModuleVersion moduleVersion = masterDb.ModuleVersions.Where(mv => mv.ModuleId == moduleId)
						.OrderByDescending(mv => mv.Major)
						.ThenByDescending(mv => mv.Minor)
						.ThenByDescending(mv => mv.Build)
						.ThenByDescending(mv => mv.Revision)
						.FirstOrDefault();

					if (moduleVersion != null)
					{
						moduleVersionItem.Major = moduleVersion.Major;
						moduleVersionItem.Minor = moduleVersion.Minor;
						moduleVersionItem.Build = moduleVersion.Build;
						moduleVersionItem.Revision = moduleVersion.Revision;
					};

					assignedModule.ModuleVersion = moduleVersionItem;

					assignedModules[index] = assignedModule;
					index++;
				}
			}

			return new AssignedModule[0];
		}
		/// <summary>
		/// Register module in system.
		/// </summary>
		/// <param name="moduleName">Module name.</param>
		/// <returns><c>ModuleItem</c></returns>
		public ModuleItem RegisterModule(string moduleName)
		{
			using (MasterContext masterDb = new MasterContext())
			{
				ModuleDescription module = masterDb.Modules.FirstOrDefault(m => string.Equals(m.Name, moduleName, StringComparison.CurrentCultureIgnoreCase));
				ModuleItem result = new ModuleItem();

				if (module == null)
				{
					module = new ModuleDescription();
					module.Name = moduleName;
					masterDb.Modules.Add(module);
					masterDb.SaveChanges();
				}

				result.Id = module.Id;
				result.Name = module.Name;

				return result;
			}
		}
Beispiel #11
0
		public SlaveConfiguration GetConfiguration()
		{
			SlaveConfiguration configuration = new SlaveConfiguration();

			using (MasterContext masterDb = new MasterContext())
			{
				IEnumerable<SlaveModule> slaveModules = masterDb.SlaveModules.Where(s => s.SlaveId == SlaveId);
				configuration.Modules = new ModuleVersionItem[slaveModules.Count()];

				int index = 0;
				foreach (SlaveModule slaveModule in slaveModules)
				{
					ModuleVersion moduleVersion = masterDb.ModuleVersions.Where(m => m.ModuleId == slaveModule.ModuleId)
																			.OrderByDescending(m => m.Major)
																			.ThenByDescending(m => m.Minor)
																			.ThenByDescending(m => m.Build)
																			.ThenByDescending(m => m.Revision)
																			.FirstOrDefault();

					if (moduleVersion == null)
						continue;

					ModuleVersionItem moduleVersionItem = new ModuleVersionItem
					{
						ModuleId = slaveModule.ModuleId,
						Major = moduleVersion.Major,
						Minor = moduleVersion.Minor,
						Build = moduleVersion.Build,
						Revision = moduleVersion.Revision
					};

					configuration.Modules[index] = moduleVersionItem;
					index++;
				}
			}
			return configuration;
		}
Beispiel #12
0
		public bool HasModule(int moduleId)
		{
			using (MasterContext masterDb = new MasterContext())
			{
				SlaveModule slaveModule = masterDb.SlaveModules.FirstOrDefault(s => s.SlaveId == SlaveId && s.ModuleId == moduleId);
				return slaveModule != null;
			}
		}
Beispiel #13
0
		public SlaveProcessWorker(string sessionId, IOrationiSlaveCallback callback, int inactiveTimeout = 30000)
		{
			SessionId = sessionId;
			Ip = NetworkUtility.GetClientIpFromOperationContext();
			Callback = callback;
			LastActivity = DateTime.Now;
			InactiveTimeout = inactiveTimeout;

			//Save information in Db
			using (MasterContext masterDb = new MasterContext())
			{
				//If slave exist in db - update some information.
				SlaveDescription slave = masterDb.Slaves.FirstOrDefault(s => s.Address == Ip);
				if (slave != null)
				{
					slave.LastConnectionOn = DateTime.Now;
					masterDb.SaveChanges();
					SlaveId = slave.Id;
					return;
				}

				//If slave not exist in db - create new record
				string hostName = NetworkUtility.GetHostName(Ip);
				slave = new SlaveDescription
				{
					Address = Ip,
					//Id = Guid.NewGuid(),
					RegistredOn = DateTime.Now,
					LastConnectionOn = DateTime.Now,
					Name = $"Unknown slave {hostName} ({Ip})"
				};

				masterDb.Slaves.Add(slave);
				masterDb.SaveChanges();

				SlaveId = slave.Id;
			}

#if DEBUG
			Console.WriteLine("Connection from {0} {1}", SessionId, DateTime.Now.ToString("O"));
#endif
		}