private void OnReceive(IAsyncResult ar) { UdpClient udp = (UdpClient)ar.AsyncState; try { IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0); byte[] data = udp.EndReceive(ar, ref endpoint); // check if we have any pending operations PendingOp op = null; lock (pendingOps) { pendingOps.TryGetValue(endpoint.Address, out op); pendingOps[endpoint.Address] = null; } if (op != null && !op.cancelled) { // figure out what this message is if (op.messageCode == MicroMessageCodes.GetTimestamp && data.Length == 8) { // we have a value message, parse BigEndianBinaryReader reader = new BigEndianBinaryReader(data); reader.ReadUInt16(); ushort secs = reader.ReadUInt16(); int ticks = reader.ReadInt32(); RaiseTimestampEvent(endpoint.Address, new MicroTimestamp(secs, ticks), true); } else if (op.messageCode == MicroMessageCodes.TimingMode && data.Length == 3) { bool server = data[2] == 1; RaiseBoolEvent(endpoint.Address, server, true, ModeReceived); } else if (op.messageCode == MicroMessageCodes.TimingPulse && data.Length == 3) { bool result = data[2] == 1; RaiseBoolEvent(endpoint.Address, result, true, PulseReceived); } else if (op.messageCode == MicroMessageCodes.TimingResync && data.Length == 3) { bool result = data[2] == 0; RaiseBoolEvent(endpoint.Address, result, true, ResyncAcknowledged); } else if (op.messageCode == MicroMessageCodes.TimingSync && data.Length == 3) { bool result = data[2] == 1; RaiseBoolEvent(endpoint.Address, result, true, SyncReceived); } } udp.BeginReceive(OnReceive, udp); } catch (ObjectDisposedException) { } }
public Task<IHttpResponseMessageAbstraction> EnableDisableUserChangeRequest(string dnsName, string location, string payload) { lock (Clusters) { var converter = new ClusterProvisioningServerPayloadConverter(); var request = converter.DeserializeChangeRequest<HttpUserChangeRequest>(payload); var passThroughResponse = new PassthroughResponse(); var statusResponse = new UserChangeOperationStatusResponse(); var pendingOp = new PendingOp(statusResponse); passThroughResponse.Data = pendingOp.Id; statusResponse.OperationType = request.Operation; statusResponse.UserType = UserType.Http; statusResponse.RequestIssueDate = DateTime.UtcNow; statusResponse.State = UserChangeOperationState.Pending; Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails error; var cluster = GetCloudServiceInternal(dnsName); // If the Cluster is not Found. if (cluster.IsNull()) { error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.NotFound, ErrorId = "NOT FOUND", ErrorMessage = "The requested cluster does not exist.", }; passThroughResponse.Error = error; passThroughResponse.Data = null; return this.ReturnResponse(passThroughResponse); } if (!this.SupportedConnectivityClusterVersions.Contains(cluster.Cluster.Version)) { error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.BadRequest, ErrorId = "UNSUPPORTED", ErrorMessage = string.Format( CultureInfo.InvariantCulture, "Cluster connectivity changes are not supported for this cluster version '{0}'.", cluster.Cluster.Version) }; passThroughResponse.Error = error; passThroughResponse.Data = null; return this.ReturnResponse(passThroughResponse); } // If the cluster has a pending operation (for the simulator pending operations always take <OperationTimeToCompletionInSeconds> seconds) if (cluster.HttpPendingOp.IsNotNull()) { if (cluster.HttpPendingOp.Response.RequestIssueDate.AddMilliseconds(OperationTimeToCompletionInMilliseconds) > DateTime.UtcNow) { error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.Conflict, ErrorId = "CONFLICT", ErrorMessage = "Another attempt to update the server is in progress." }; passThroughResponse.Error = error; passThroughResponse.Data = null; return this.ReturnResponse(passThroughResponse); } } if (request.Operation == UserChangeOperationType.Enable) { // If the cluster already has a User and we are trying to enable again. if (cluster.Cluster.HttpUserName.IsNotNullOrEmpty()) { error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.BadRequest, ErrorId = "Bad Request", ErrorMessage = "Attempt to enable a user when a user has already been enabled. Please disable the user first and try again." }; passThroughResponse.Error = error; passThroughResponse.Data = null; return this.ReturnResponse(passThroughResponse); } // check that the user is not trying to update the http user with the same user name as rdp if (!String.IsNullOrEmpty(cluster.Cluster.RdpUserName)) { if (cluster.Cluster.RdpUserName.Equals(request.Username)) { error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.BadRequest, ErrorId = "USERCHANGE_INVALIDNAME", ErrorMessage = "Http username is not allowed to be same as RDP user name" }; passThroughResponse.Error = error; passThroughResponse.Data = null; return this.ReturnResponse(passThroughResponse); } } // Otherwise this is a good request, set the pending op. if (request.Username == "fail") { statusResponse.State = UserChangeOperationState.Error; statusResponse.Error = new Microsoft.WindowsAzure.Management.HDInsight.Contracts.ErrorDetails() { StatusCode = HttpStatusCode.BadRequest, ErrorId = "Bad Request", ErrorMessage = "The request failed." }; } cluster.HttpPendingOp = pendingOp; ProcessingOps.Add(pendingOp.Id, pendingOp); cluster.Cluster.HttpUserName = request.Username; cluster.Cluster.HttpPassword = request.Password; return this.ReturnResponse(passThroughResponse); } // It's a disable, that's always an op (or a duplicate op). cluster.HttpPendingOp = pendingOp; ProcessingOps.Add(pendingOp.Id, pendingOp); cluster.Cluster.HttpUserName = string.Empty; cluster.Cluster.HttpPassword = string.Empty; return this.ReturnResponse(passThroughResponse); } }
private void SendCommand(MicroMessageCodes code, IPAddress address) { pendingOps[address] = new PendingOp(code); client.Send(new byte[] { (byte)code }, 1, new IPEndPoint(address, 20)); }
private void AddFlushRequestOp() { pendingOps.Add(PendingOp.CreateFlushRequest()); }
private void AddBufferWriteRequestOp(ShadowBufferData buffer) { pendingOps.Add(PendingOp.CreateBufferWriteRequest(buffer)); }
private void SendCommand(MicroMessageCodes code, IPAddress address) { pendingOps[address] = new PendingOp(code); client.Send(new byte[] { (byte)code }, 1, new IPEndPoint(address, 20)); }