internal CodecResponse(CodecRequest request, HttpsClientResponse response) { _request = request; _response = response; if (_response.ContentLength == 0) { return; } try { #if DEBUG Debug.WriteNormal(Debug.AnsiBlue + _xml + Debug.AnsiReset); #endif if (response.Code == 200) { var reader = new XmlReader(response.ContentString); _xml = XDocument.Load(reader); } } catch (Exception e) { CloudLog.Exception(e, "Error parsing xml document from codec"); } }
public int GetXmlAsync(string path, CodecHttpClientRequestCallback callback) { var r = new CodecRequest(_address, string.Format("/getxml?location={0}", path.StartsWith("/") ? path : "/" + path), _sessionId); DispatchAsync(r, callback); return(r.Id); }
public int PutXmlAsync(string xml, CodecHttpClientRequestCallback callback) { var r = new CodecRequest(_address, "/putxml", _sessionId) { RequestType = RequestType.Post, ContentString = xml }; DispatchAsync(r, callback); return(r.Id); }
public CodecResponse GetXml(string path) { var r = new CodecRequest(_address, string.Format("/getxml?location={0}", path.StartsWith("/") ? path : "/" + path), _sessionId); var response = Dispatch(r); if (response.Code == 401) { StartSession(); } return(response); }
private void DispatchAsync(CodecRequest request, CodecHttpClientRequestCallback callback) { Lock.Enter(); Callbacks[request] = callback; Lock.Leave(); //CrestronConsole.PrintLine("Queuing request id:{0}", request.Id); RequestQueue.Enqueue(request); if (_dispatchThread == null || _dispatchThread.ThreadState == Thread.eThreadStates.ThreadFinished) { _dispatchThread = new Thread(DispatchThreadProcess, null) { Name = "CodecHttpClient Process Thread", Priority = Thread.eThreadPriority.HighPriority }; } }
private CodecResponse Dispatch(CodecRequest request) { try { var r = _client.Dispatch(request.Request); #if DEBUG Debug.WriteSuccess("Received HTTPS Response", "{0}, {1} bytes", r.Code, r.ContentLength); #endif return(new CodecResponse(request, r)); } catch (Exception e) { if (e is ThreadAbortException) { return(new CodecResponse(request, e)); } //CloudLog.Exception(e, "Error dispatching request ID {0} to codec", request.Id); return(null); } }
public CodecResponse PutXml(string xml) { var r = new CodecRequest(_address, "/putxml", _sessionId) { RequestType = RequestType.Post, ContentString = xml }; #if DEBUG Debug.WriteInfo("Dispatching request", "https://{0}/putxml", _address); Debug.WriteNormal(xml); #endif var response = Dispatch(r); if (response.Code == 401) { StartSession(); } return(response); }
internal bool StartSession() { CodecRequest request; CodecResponse response; CloudLog.Info("Cisco Codec StartSession() called"); if (!string.IsNullOrEmpty(_sessionId)) { try { CloudLog.Debug("Codec has session ID already... attempting to close it"); request = new CodecRequest(_address, "/xmlapi/session/end", _sessionId) { RequestType = RequestType.Post }; response = Dispatch(request); #if DEBUG Debug.WriteInfo("Received headers for session end:\r\n" + response.Header); #endif CloudLog.Debug("Session close request result: {0}", response.Code); } catch (Exception e) { Debug.WriteError("Error trying to close session on codec", "Session ID to close: {0}, Error: {1}\r\nStackTrace: {2}", _sessionId, e.Message, e.StackTrace); CloudLog.Error("Error trying to close the codec session, {0}", e.Message); } } try { request = new CodecRequest(_address, "/xmlapi/session/begin", _username, _password) { RequestType = RequestType.Post }; response = Dispatch(request); #if DEBUG Debug.WriteInfo("Received headers for session begin:\r\n" + response.Header); #endif if (response.Code == 401) { CloudLog.Error("Error logging into Cisco Codec at \"{0}\" 401 - Unauthorized", _address); return(false); } if (response.Code == 204) { if (!response.Header.ContainsHeaderValue("Set-Cookie")) { CloudLog.Error("Received 204 for Session ID but response headers contained no Cookie"); return(false); } Debug.WriteSuccess("Codec Set-Cookie received", response.Header["Set-Cookie"]); CloudLog.Notice("Codec Set-Cookie received\r\n{0}", response.Header["Set-Cookie"]); var match = Regex.Match(response.Header["Set-Cookie"].Value, @"(.*?)=(.*?);"); if (match.Groups[1].Value != "SecureSessionId") { CloudLog.Error("Received 204 for Session ID but response headers contained no SessionId"); return(false); } _sessionId = match.Groups[2].Value; CloudLog.Info("Codec received new Session Id OK"); return(true); } } catch (Exception e) { CloudLog.Error("Error trying to get a session ID from Cisco Codec, {0}", e.Message); return(false); } return(false); }
internal CodecResponse(CodecRequest request, Exception e) { _request = request; Exception = e; }