public async Task <IActionResult> Put([FromQuery] string id, [FromBody] RoomData room) { var res = await UserHelper.IsUserAuthorized(this.serviceContext, this.httpClient, id); if (res.Result != null) { return(res.Result); } if (String.IsNullOrEmpty(room.Name)) { return(new BadRequestResult()); } room = new RoomData(room.Name); string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.ROOMS_API, room.Id); StringContent putContent = HttpHelper.GetJSONContent(room); using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent)) { return(new ContentResult() { StatusCode = (int)response.StatusCode, Content = await response.Content.ReadAsStringAsync() }); } }
public ValuesController(StatelessServiceContext context) { _correlationId = Guid.NewGuid().ToString(); _logger = new WebApiLogger(context); _servicesCommunicationLogger = new ServicesCommunicationLogger(context); _partitionHelper = new PartitionHelper(_servicesCommunicationLogger); _logger.ActivatingController(); }
public async Task <IActionResult> Put([FromQuery] string id, [FromBody] UserData user) { if (String.IsNullOrEmpty(id)) { // Register if (String.IsNullOrEmpty(user.Name)) { return(new BadRequestResult()); } user = new UserData(user.Name); } else { // Login string proxyUrlLogin = PartitionHelper.GetProxyUrl(this.serviceContext, $"{HttpHelper.USERS_API}/{id}", id); using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrlLogin)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } user = JsonConvert.DeserializeObject <UserData>(await response.Content.ReadAsStringAsync()); } user.LastLoginAt = DateTime.Now; } // Set as connected user string remoteAddress = $"{this.HttpContext.Connection.RemoteIpAddress}:{this.HttpContext.Connection.RemotePort}"; string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.CONNECTED_USERS_API, user.Id); StringContent putContent = HttpHelper.GetJSONContent(new ConnectedUserData(user.Id, user.Name, remoteAddress)); using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } } // Save user proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.USERS_API, user.Id); putContent = HttpHelper.GetJSONContent(user); using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent)) { return(new ContentResult() { StatusCode = (int)response.StatusCode, Content = await response.Content.ReadAsStringAsync() }); } }
private PartitionHelper GetOrCreatePartitionHelper() { if (_partitionHelper != null) { return(_partitionHelper); } lock (_lock) { if (_partitionHelper == null) { _partitionHelper = new PartitionHelper(); } return(_partitionHelper); } }
private void BulkAll(IObserver <BulkAllResponse> observer) { var documents = _partitionedBulkRequest.Documents; var partitioned = new PartitionHelper <T>(documents, _bulkSize); #pragma warning disable 4014 #pragma warning disable VSTHRD110 // Observe result of async calls partitioned.ForEachAsync( #pragma warning restore 4014 (buffer, page) => BulkAsync(buffer, page, 0), (buffer, response) => observer.OnNext(response), ex => OnCompleted(ex, observer), _maxDegreeOfParallelism ); #pragma warning restore VSTHRD110 // Observe result of async calls }
public async Task <IActionResult> Delete([FromQuery] string id) { if (String.IsNullOrEmpty(id)) { return(new BadRequestResult()); } // Logout // Set as not connected user string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, $"{HttpHelper.CONNECTED_USERS_API}/{id}", id); using (HttpResponseMessage response = await this.httpClient.DeleteAsync(proxyUrl)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } } return(new OkResult()); }
public async Task <IActionResult> Get([FromQuery] string id, string roomId) { var res = await UserHelper.IsUserAuthorized(this.serviceContext, this.httpClient, id); if (res.Result != null) { return(res.Result); } if (String.IsNullOrEmpty(roomId)) { return(new BadRequestResult()); } var result = await RoomHelper.RoomExists(this.serviceContext, this.httpClient, this.StatusCode, roomId); if (result.Result != null) { return(result.Result); } if (!result.Room.Members.Contains(id)) { return(new UnauthorizedResult()); } string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, $"{HttpHelper.MESSAGES_API}/{roomId}", roomId); using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl)) { return(new ContentResult() { StatusCode = (int)response.StatusCode, Content = await response.Content.ReadAsStringAsync() }); } }
public async Task <IActionResult> Get([FromQuery] string id) { var res = await UserHelper.IsUserAuthorized(this.serviceContext, this.httpClient, id); if (res.Result != null) { return(res.Result); } Uri proxyAddress = PartitionHelper.GetProxyAddress(this.serviceContext); ServicePartitionList partitions = await PartitionHelper.GetAllPartitions(this.serviceContext, this.fabricClient); List <RoomData> result = new List <RoomData>(); foreach (Partition partition in partitions) { string proxyUrl = PartitionHelper.GetProxyUrl(proxyAddress, HttpHelper.ROOMS_API, ((Int64RangePartitionInformation)partition.PartitionInformation).LowKey); using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { continue; } result.AddRange(JsonConvert.DeserializeObject <List <RoomData> >(await response.Content.ReadAsStringAsync())); } } if (result.Count <= 0) { return(new NoContentResult()); } return(this.Json(result)); }
public async Task <IActionResult> Post([FromQuery] string id, string roomId, [FromBody] MessageData msg) { var res = await UserHelper.IsUserAuthorized(this.serviceContext, this.httpClient, id); if (res.Result != null) { return(res.Result); } if (String.IsNullOrEmpty(roomId) || (!String.IsNullOrEmpty(msg.RoomId) && roomId != msg.RoomId) || String.IsNullOrEmpty(msg.Content)) { return(new BadRequestResult()); } var result = await RoomHelper.RoomExists(this.serviceContext, this.httpClient, this.StatusCode, roomId); if (result.Result != null) { return(result.Result); } if (!result.Room.Members.Contains(id)) { return(new UnauthorizedResult()); } msg = new MessageData(id, res.User.Name, roomId, msg.Content); string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.MESSAGES_API, roomId); StringContent postContent = HttpHelper.GetJSONContent(msg); using (HttpResponseMessage response = await this.httpClient.PostAsync(proxyUrl, postContent)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } } // Broadcast message to online members foreach (string member in result.Room.Members) { proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, $"{HttpHelper.CONNECTED_USERS_API}/{member}", member); using (HttpResponseMessage response = await httpClient.GetAsync(proxyUrl)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { continue; } var user = JsonConvert.DeserializeObject <ConnectedUserData>(await response.Content.ReadAsStringAsync()); // TODO: Send message to connected user Console.WriteLine($"Sending message to: {user.Ip}"); } } return(new ContentResult() { StatusCode = (int)System.Net.HttpStatusCode.OK, Content = JsonConvert.SerializeObject(msg, Formatting.Indented) }); }
/// <summary> /// Calculates the minimum block size required for a single partition, taking into account any children partitions. /// </summary> public PartitionHelper Partition(int hdNumberToGet, int partNumberToGet, long newHdSize) { var partition = _imageSchema.HardDrives[hdNumberToGet].Partitions[partNumberToGet]; var partitionHelper = new PartitionHelper { MinSizeBlk = 0 }; var extendedPartitionHelper = new ExtendedPartitionHelper(); if (partition.Type.ToLower() == "extended") { extendedPartitionHelper = ExtendedPartition(hdNumberToGet, newHdSize); } partitionHelper.VolumeGroupHelper = VolumeGroup(hdNumberToGet, partNumberToGet, newHdSize); var lbsByte = _imageSchema.HardDrives[hdNumberToGet].Lbs; var imagePath = Settings.PrimaryStoragePath + Path.DirectorySeparatorChar + "images" + Path.DirectorySeparatorChar + _imageProfile.Image.Name + Path.DirectorySeparatorChar + "hd" + hdNumberToGet; //Look if any volume groups are present for this partition. If so set the volumesize for the volume group to the minimum size //required for the volume group. Volume groups are always treated as resizable even if none of the individual //logical volumes are resizable if (partitionHelper.VolumeGroupHelper.Pv != null) { partitionHelper.PartitionHasVolumeGroup = true; partition.VolumeSize = (partitionHelper.VolumeGroupHelper.MinSizeBlk * lbsByte / 1024 / 1024); } if (partition.ForceFixedSize) { partitionHelper.MinSizeBlk = partition.Size; partitionHelper.IsDynamicSize = false; } //Use partition size that user has set for the partition, if it is set. else if (!string.IsNullOrEmpty(partition.CustomSize) && !string.IsNullOrEmpty(partition.CustomSizeUnit)) { long customSizeBytes = 0; switch (partition.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(partition.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(partition.CustomSize) * 1024 * 1024 * 1024; break; case "%": double hdPercent = Convert.ToDouble(partition.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } partitionHelper.MinSizeBlk = customSizeBytes / lbsByte; partitionHelper.IsDynamicSize = false; } //If partition is not resizable. Determine partition size. Also if the partition is less than 5 gigs assume it is that // size for a reason, do not resize it even if it is marked as a resizable partition else if ((partition.VolumeSize == 0 && partition.Type.ToLower() != "extended") || (partition.Type.ToLower() == "extended" && extendedPartitionHelper.IsOnlySwap) || partition.Size * lbsByte <= 5368709120 || partition.FsType == "swap") { partitionHelper.MinSizeBlk = partition.Size; partitionHelper.IsDynamicSize = false; } //If resizable determine what percent of drive partition was originally and match that to the new drive //while making sure the min size is still less than the resized size. else { partitionHelper.IsDynamicSize = true; if (partition.Type.ToLower() == "extended") { partitionHelper.MinSizeBlk = extendedPartitionHelper.MinSizeBlk; } else if (partitionHelper.VolumeGroupHelper.Pv != null) { partitionHelper.MinSizeBlk = partitionHelper.VolumeGroupHelper.MinSizeBlk; } else { string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "winpe", "xfs" }) { try { imageFile = Directory.GetFiles( imagePath + Path.DirectorySeparatorChar, "part" + partition.Number + "." + ext + ".*") .FirstOrDefault(); } catch (Exception ex) { Logger.Log(ex.Message); } if (imageFile != null) { break; } } if (Path.GetExtension(imageFile) == ".wim") { partitionHelper.MinSizeBlk = (partition.UsedMb * 1024 * 1024) / lbsByte; } else { //The resize value and used_mb value are calculated during upload by two different methods //Use the one that is bigger just in case. if (partition.VolumeSize > partition.UsedMb) { partitionHelper.MinSizeBlk = partition.VolumeSize * 1024 * 1024 / lbsByte; } else { partitionHelper.MinSizeBlk = (partition.UsedMb * 1024 * 1024) / lbsByte; } } } } return(partitionHelper); }
/// <summary> /// Calculates the minimum block size required for a single logical volume, assuming the logical volume cannot have any /// children. /// </summary> public PartitionHelper LogicalVolume(Models.ImageSchema.LogicalVolume lv, int lbsByte, long newHdSize, int hdNumberToGet) { var logicalVolumeHelper = new PartitionHelper { MinSizeBlk = 0 }; if (lv.ForceFixedSize) { logicalVolumeHelper.MinSizeBlk = lv.Size; logicalVolumeHelper.IsDynamicSize = false; } else if (!string.IsNullOrEmpty(lv.CustomSize) && !string.IsNullOrEmpty(lv.CustomSizeUnit)) { long customSizeBytes = 0; switch (lv.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024 * 1024; break; case "%": double hdPercent = Convert.ToDouble(lv.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } logicalVolumeHelper.MinSizeBlk = customSizeBytes / lbsByte; logicalVolumeHelper.IsDynamicSize = false; } else { var imagePath = Settings.PrimaryStoragePath + Path.DirectorySeparatorChar + "images" + Path.DirectorySeparatorChar + _imageProfile.Image.Name + Path.DirectorySeparatorChar + "hd" + hdNumberToGet; logicalVolumeHelper.IsDynamicSize = true; string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { try { imageFile = Directory.GetFiles( imagePath + Path.DirectorySeparatorChar, lv.VolumeGroup + "-" + lv.Name + "." + ext + ".*") .FirstOrDefault(); } catch (Exception ex) { Logger.Log(ex.Message); } if (imageFile != null) { break; } } if (Path.GetExtension(imageFile) == ".wim") { logicalVolumeHelper.MinSizeBlk = (lv.UsedMb * 1024 * 1024) / lbsByte; } else { //fix me - a hack when using core storage with dynamic partitions on macos environment if (lv.FsType.ToLower().Contains("hfs") && newHdSize <= 121332826112) { //assume fusion, set minsize to full size of drive logicalVolumeHelper.MinSizeBlk = Convert.ToInt64(newHdSize * .8) / lbsByte; } else { if (lv.VolumeSize > lv.UsedMb) { logicalVolumeHelper.MinSizeBlk = lv.VolumeSize * 1024 * 1024 / lbsByte; } else { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } } } } return(logicalVolumeHelper); }
/// <summary> /// Calculates the minimum block size required for a single logical volume, assuming the logical volume cannot have any /// children. /// </summary> public PartitionHelper LogicalVolume(LogicalVolume lv, int lbsByte, long newHdSize, int hdNumberToGet) { var logicalVolumeHelper = new PartitionHelper { MinSizeBlk = 0 }; if (lv.ForceFixedSize) { logicalVolumeHelper.MinSizeBlk = lv.Size; logicalVolumeHelper.IsDynamicSize = false; } else if (!string.IsNullOrEmpty(lv.CustomSize) && !string.IsNullOrEmpty(lv.CustomSizeUnit)) { long customSizeBytes = 0; switch (lv.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024 * 1024; break; case "%": var hdPercent = Convert.ToDouble(lv.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } logicalVolumeHelper.MinSizeBlk = customSizeBytes / lbsByte; logicalVolumeHelper.IsDynamicSize = false; } else { logicalVolumeHelper.IsDynamicSize = true; string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { imageFile = new FilesystemServices().GetLVMFileNameWithFullPath(_imageProfile.Image.Name, hdNumberToGet.ToString(), lv.VolumeGroup, lv.Name, ext); if (!string.IsNullOrEmpty(imageFile)) { break; } } if (Path.GetExtension(imageFile) == ".wim") { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } else { //fix me - a hack when using core storage with dynamic partitions on macos environment if (lv.FsType.ToLower().Contains("hfs") && newHdSize <= 121332826112) { //assume fusion, set minsize to full size of drive logicalVolumeHelper.MinSizeBlk = Convert.ToInt64(newHdSize * .8) / lbsByte; } else { if (lv.VolumeSize > lv.UsedMb) { logicalVolumeHelper.MinSizeBlk = lv.VolumeSize * 1024 * 1024 / lbsByte; } else { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } } } } return(logicalVolumeHelper); }
/// <summary> /// Calculates the minimum block size required for a single logical volume, assuming the logical volume cannot have any /// children. /// </summary> public PartitionHelper LogicalVolume(LogicalVolume lv, int lbsByte, long newHdSize, int hdNumberToGet) { var logicalVolumeHelper = new PartitionHelper { MinSizeBlk = 0 }; if (lv.ForceFixedSize) { logicalVolumeHelper.MinSizeBlk = lv.Size; logicalVolumeHelper.IsDynamicSize = false; } else if (!string.IsNullOrEmpty(lv.CustomSize) && !string.IsNullOrEmpty(lv.CustomSizeUnit)) { long customSizeBytes = 0; switch (lv.CustomSizeUnit) { case "MB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024; break; case "GB": customSizeBytes = Convert.ToInt64(lv.CustomSize) * 1024 * 1024 * 1024; break; case "%": var hdPercent = Convert.ToDouble(lv.CustomSize) / 100; customSizeBytes = Convert.ToInt64(hdPercent * newHdSize); break; } logicalVolumeHelper.MinSizeBlk = customSizeBytes / lbsByte; logicalVolumeHelper.IsDynamicSize = false; } else { logicalVolumeHelper.IsDynamicSize = true; string imageFile = null; foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "xfs" }) { imageFile = new FilesystemServices().GetLVMFileNameWithFullPath(_imageProfile.Image.Name, hdNumberToGet.ToString(), lv.VolumeGroup, lv.Name, ext); if (!string.IsNullOrEmpty(imageFile)) { break; } } if (Path.GetExtension(imageFile) == ".wim") { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } else { if (lv.VolumeSize > lv.UsedMb) { logicalVolumeHelper.MinSizeBlk = lv.VolumeSize * 1024 * 1024 / lbsByte; } else { logicalVolumeHelper.MinSizeBlk = lv.UsedMb * 1024 * 1024 / lbsByte; } } } return(logicalVolumeHelper); }
public async Task <IActionResult> Delete([FromQuery] string id, string roomId) { var res = await UserHelper.IsUserAuthorized(this.serviceContext, this.httpClient, id); if (res.Result != null) { return(res.Result); } if (String.IsNullOrEmpty(roomId)) { return(new BadRequestResult()); } var result = await RoomHelper.RoomExists(this.serviceContext, this.httpClient, this.StatusCode, roomId); if (result.Result != null) { return(result.Result); } // Remove user from the room result.Room.Members.Remove(id); string proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.ROOMS_API, roomId); StringContent putContent = HttpHelper.GetJSONContent(result.Room); using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } } // Remove room from user list proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, $"{HttpHelper.USERS_API}/{id}", id); UserData user; using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl)) { if (response.StatusCode != System.Net.HttpStatusCode.OK) { return(this.StatusCode((int)response.StatusCode)); } user = JsonConvert.DeserializeObject <UserData>(await response.Content.ReadAsStringAsync()); } user.Rooms.Remove(roomId); proxyUrl = PartitionHelper.GetProxyUrl(this.serviceContext, HttpHelper.USERS_API, user.Id); putContent = HttpHelper.GetJSONContent(user); using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent)) { return(new ContentResult() { StatusCode = (int)response.StatusCode, Content = await response.Content.ReadAsStringAsync() }); } }