HttpWebResponse Stage1(ServiceAction action, WebHeaderCollection headers, int retry, Stream stream)
 {
     if (fallback.OmitMan) {
         return Stage1 (action, headers, retry, stream, CreateRequestWithoutMan, CreateRequestWithMan);
     } else {
         return Stage1 (action, headers, retry, stream, CreateRequestWithMan, CreateRequestWithoutMan);
     }
 }
 HttpWebResponse Stage1(ServiceAction action, WebHeaderCollection headers, int retry, Stream stream,
     RequestProvider requestProvider1, RequestProvider requestProvider2)
 {
     var response = Stage2 (requestProvider1, action, headers, retry, stream);
     if (response.StatusCode == HttpStatusCode.MethodNotAllowed) {
         response = Stage2 (requestProvider2, action, headers, retry, stream);
     }
     return response;
 }
 HttpWebResponse GetResponse(ServiceAction action, WebHeaderCollection headers,
     int retry, Stream stream)
 {
     var response = Stage1 (action, headers, retry, stream);
     if (response.StatusCode == HttpStatusCode.NotImplemented || response.StatusDescription == "Not Extended") {
         // FIXME is this the right exception type?
         throw new UpnpException ("The SOAP request failed.");
     } else {
         return response;
     }
 }
        ActionResult Invoke(ServiceAction action, IDictionary<string, string> arguments,
            Encoding encoding, int retry, bool isFallback)
        {
            // Because we may do several fallbacks, we serialize to a memory stream and copy that
            // to the network stream. That way we only have to serialize once (unless we have to
            // try a different encoding scheme).
            using (var stream = new MemoryStream ()) {
                try {
                    var headers = new WebHeaderCollection ();
                    var settings = new XmlWriterSettings { Encoding = encoding };
                    using (var writer = XmlWriter.Create (stream, settings)) {
                        action.SerializeRequest (arguments, headers, writer);
                    }

                    using (var response = GetResponse (action, headers, retry, stream)) {
                        if (response.StatusCode == HttpStatusCode.OK) {
                            return action.DeserializeResponse (response);
                        } else if (response.StatusCode == HttpStatusCode.InternalServerError) {
                            if (isFallback) {
                                action.DeserializeResponseFault (response);
                            }
                            return null;
                        } else if (response.StatusCode == HttpStatusCode.BadRequest) {
                            return null;
                        } else {
                            throw new UpnpException (string.Format (
                                "There was an unknown error while invoking the action: " +
                                "the service returned status code {0}.", response.StatusCode));
                        }
                    }
                } catch (Exception e) {
                    throw new UpnpException ("There was an unknown error while invoking the action.", e);
                }
            }
        }
 internal ActionResult Invoke(ServiceAction action, IDictionary<string, string> arguments, int retry)
 {
     if (soap_adapter == null) {
         if (service_description.ControlUrl == null) {
             throw new InvalidOperationException (
                 string.Format ("{0} does not have a control URL.", service_description));
         }
         soap_adapter = new SoapInvoker (service_description.ControlUrl);
     }
     try {
         return soap_adapter.Invoke (action, arguments, retry);
     } catch (WebException e) {
         if (e.Status == WebExceptionStatus.Timeout) {
             service_description.CheckDisposed ();
         }
         throw e;
     }
 }
 HttpWebResponse Final(HttpWebRequest request, ServiceAction action, int retry, Stream stream)
 {
     stream.Seek (0, SeekOrigin.Begin);
     if (!fallback.Chuncked) {
         request.ContentLength = stream.Length;
     }
     using (var output = request.GetRequestStream ()) {
         var buffer = new byte[1024];
         int count;
         while ((count = stream.Read (buffer, 0, 1024)) > 0) {
             output.Write (buffer, 0, count);
         }
     }
     return Helper.GetResponse (request, retry);
 }
 private void WriteMethodSig(CodeMonkey monkey, ServiceAction action, string modifiers, string name, bool includeAttributes)
 {
     monkey.WriteLine (modifiers);
     monkey.Write (action.ReturnArgument != null ? GetTypeName (action.ReturnArgument.RelatedStateVariable.Type) : "void");
     monkey.Write (" {0} (", name);
     WriteMethodParameters (monkey, action, true, includeAttributes);
     monkey.Write (")");
 }
 HttpWebResponse Stage3Unchunked(HttpWebRequest request, ServiceAction action,
     int retry, Stream stream)
 {
     fallback.Chuncked = false;
     request.ProtocolVersion = new Version (1, 0);
     request.SendChunked = false;
     return Final (request, action, retry, stream);
 }
 private void WriteMethod(CodeMonkey monkey, ServiceAction action)
 {
     monkey.WriteLine ("[UpnpAction]");
     WriteArgumentAttribute (monkey, action.ReturnArgument);
     WriteMethodSig (monkey, action, "public ", action.Name, true);
     monkey.StartWriteBlock ();
     monkey.WriteLine ("{0}{1}Core (", action.ReturnArgument == null ? "" : "return ", action.Name);
     WriteMethodParameters (monkey, action, false, false);
     monkey.Write (");");
     monkey.EndWriteBlock ();
     monkey.WriteLine ();
     WriteMethodSig (monkey, action, "public abstract ", action.Name + "Core", false);
     monkey.Write (";");
     monkey.WriteLine ();
 }
 private void WriteMethodParameters(CodeMonkey monkey, ServiceAction action, bool definition, bool includeAttributes)
 {
     bool first = true;
     foreach (Argument argument in Concat (action.InArguments.Values, action.OutArguments.Values)) {
         if (first) {
             first = false;
         } else {
             monkey.Write (", ");
         }
         if (definition) {
             if (includeAttributes) {
                 WriteArgumentAttribute (monkey, argument);
             }
             WriteMethodParameter (monkey, argument);
         } else {
             monkey.Write ("{0}{1}", argument.Direction == ArgumentDirection.Out ? "out " : "", ToCamelCase (argument.Name));
         }
     }
 }
        protected internal Argument(ServiceAction action)
        {
            if (action == null) throw new ArgumentNullException ("action");

            this.action = action;
        }
 public static void AssertEquality (ServiceAction sourceAction, ServiceAction targetAction)
 {
     Assert.AreEqual (sourceAction.Name, targetAction.Name);
     var source_arguments = sourceAction.Arguments.Values.GetEnumerator ();
     var target_arguments = targetAction.Arguments.Values.GetEnumerator ();
     while (source_arguments.MoveNext ()) {
         Assert.IsTrue (target_arguments.MoveNext ());
         AssertEquality (source_arguments.Current, target_arguments.Current);
     }
     Assert.IsFalse (target_arguments.MoveNext ());
 }
 protected void AddAction(ServiceAction action)
 {
     if (action == null) throw new ArgumentNullException ("action");
     actions.SetItem (action.Name, action);
 }
 HttpWebResponse Stage2(RequestProvider requestProvider, ServiceAction action,
     WebHeaderCollection headers, int retry, Stream stream)
 {
     var request = requestProvider (action, headers);
     if (fallback.Chuncked) {
         return Stage2 (request, action, retry, stream, Stage3Chuncked, Stage3Unchunked);
     } else {
         return Stage2 (request, action, retry, stream, Stage3Unchunked, Stage3Chuncked);
     }
 }
 HttpWebRequest CreateRequestWithMan(ServiceAction action, WebHeaderCollection headers)
 {
     fallback.OmitMan = false;
     var request = CreateRequest (headers);
     request.Method = "M-POST";
     request.Headers.Add ("MAN", string.Format (@"""{0}""; ns=01", Protocol.SoapEnvelopeSchema));
     request.Headers.Add ("01-SOAPACTION", string.Format (
         @"""{0}#{1}""", action.Controller.Description.Type, action.Name));
     return request;
 }
 HttpWebResponse Stage2(HttpWebRequest request, ServiceAction action, int retry, Stream stream,
     RequestHandler requestHandler1, RequestHandler requestHandler2)
 {
     try {
         var response = requestHandler1 (request, action, retry, stream);
         if (response.StatusCode == HttpStatusCode.HttpVersionNotSupported) {
             throw new ProtocolViolationException ();
         }
         return response;
     } catch (ProtocolViolationException) {
         return requestHandler2 (request, action, retry, stream);
     }
 }
 HttpWebRequest CreateRequestWithoutMan(ServiceAction action, WebHeaderCollection headers)
 {
     fallback.OmitMan = true;
     var request = CreateRequest (headers);
     request.Method = "POST";
     request.Headers.Add ("SOAPACTION", string.Format (
         @"""{0}#{1}""", action.Controller.Description.Type, action.Name));
     return request;
 }
 public ActionResult Invoke(ServiceAction action, IDictionary<string, string> arguments, int retry)
 {
     Encoding encoding, fallback_encoding;
     if (fallback.UseDefaultUtf8) {
         encoding = Encoding.UTF8;
         fallback_encoding = fallback_utf8;
     } else {
         encoding = fallback_utf8;
         fallback_encoding = Encoding.UTF8;
     }
     var result = Invoke (action, arguments, encoding, retry, false);
     if (result == null) {
         fallback.UseDefaultUtf8 = !fallback.UseDefaultUtf8;
         result = Invoke (action, arguments, fallback_encoding, retry, true);
     }
     return result;
 }
 public ActionInvocationWindow (ServiceController service, ServiceAction action)
     : base (WindowType.Toplevel)
 {
     this.action = action;
     
     this.Build ();
     
     name.Markup = string.Format ("<big><b>{0}</b></big>", action.Name);
     
     var arguments = new List<Argument> ();
     
     foreach (var argument in action.Arguments) {
         if (argument.Value.Direction == ArgumentDirection.In) {
             arguments.Add (argument.Value);
         }
     }
     
     table = new Table ((uint)2, (uint)arguments.Count, false);
     var row = (uint)0;
     
     foreach (var argument in arguments) {
         table.Attach (new Label (argument.Name), (uint)0, (uint)1, row, row + 1);
         Widget widget;
         var related_state_variable = service.StateVariables[argument.RelatedStateVariable];
         if (related_state_variable.AllowedValues != null) {
             var combobox = ComboBox.NewText ();
             var i = 0;
             var index = i;
             foreach (var allowed_value in related_state_variable.AllowedValues) {
                 combobox.AppendText (allowed_value);
                 if (allowed_value == related_state_variable.DefaultValue) {
                     index = i;
                 }
                 i++;
             }
             combobox.Active = index;
             widget = combobox;
         } else if (related_state_variable.AllowedValueRange != null) {
             widget = new SpinButton (
                 double.Parse (related_state_variable.AllowedValueRange.Minimum),
                 double.Parse (related_state_variable.AllowedValueRange.Maximum),
                 double.Parse (related_state_variable.AllowedValueRange.Step));
         } else if (related_state_variable.DataType == "i4") {
             widget = new SpinButton (int.MinValue, int.MaxValue, 1.0) {
                 Value = related_state_variable.DefaultValue != null
                     ? double.Parse (related_state_variable.DefaultValue) : 0
             };
         } else if (related_state_variable.DataType == "ui4") {
             widget = new SpinButton (int.MinValue, int.MaxValue, 1.0) {
                 Value = related_state_variable.DefaultValue != null
                     ? double.Parse (related_state_variable.DefaultValue) : 0
             };
         } else {
             widget = new Entry {
                 Text = related_state_variable.DefaultValue != null
                     ? related_state_variable.DefaultValue : ""
             };
         }
         table.Attach (widget, (uint)1, (uint)2, row, row + 1);
         row++;
     }
     
     inputsBox.PackStart (table, true, true, 0);
     inputsBox.ShowAll ();
 }