/// <summary>
        /// Deserialize the message
        /// </summary>
        /// <param name="map">An <see cref="OSDMap"/> containing the data</param>
        public void Deserialize(OSDMap map)
        {
            if (map.ContainsKey("item_id"))
                Request = new UpdateAgentInventoryRequestMessage();
            else if (map.ContainsKey("state") && map["state"].AsString().Equals("upload"))
                Request = new UploaderRequestUpload();
            else if (map.ContainsKey("state") && map["state"].AsString().Equals("complete"))
                Request = new UploaderRequestComplete();
            else
                Logger.Log("Unable to deserialize UpdateGestureAgentInventory: No message handler exists: " + map.AsString(), Helpers.LogLevel.Warning);

            if (Request != null)
                Request.Deserialize(map);
        }
Example #2
0
        public static string ServiceOSDRequest(string url, OSDMap data, string method, int timeout)
        {
            // MainConsole.Instance.DebugFormat("[WEB UTIL]: <{0}> start osd request for {1}, method {2}",reqnum,url,method);

            //lock (EndPointLock(url))
            {
                string errorMessage = "unknown error";
                int tickstart = Util.EnvironmentTickCount();
                int tickdata = 0;
                int tickserialize = 0;
                byte[] buffer = data != null ? Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(data, true)) : null;
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = method;
                    request.Timeout = timeout;
                    request.KeepAlive = false;
                    request.MaximumAutomaticRedirections = 10;
                    request.ReadWriteTimeout = timeout / 4;

                    // If there is some input, write it into the request
                    if (buffer != null && buffer.Length > 0)
                    {
                        request.ContentType = "application/json";
                        request.ContentLength = buffer.Length; //Count bytes to send
                        using (Stream requestStream = request.GetRequestStream())
                            requestStream.Write(buffer, 0, buffer.Length); //Send it
                    }

                    // capture how much time was spent writing, this may seem silly
                    // but with the number concurrent requests, this often blocks
                    tickdata = Util.EnvironmentTickCountSubtract(tickstart);

                    using (WebResponse response = request.GetResponse())
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            // capture how much time was spent writing, this may seem silly
                            // but with the number concurrent requests, this often blocks
                            tickserialize = Util.EnvironmentTickCountSubtract(tickstart) - tickdata;
                            string responseStr = responseStream.GetStreamString();
                            // MainConsole.Instance.DebugFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr);
                            return responseStr;
                        }
                    }
                }
                catch (WebException we)
                {
                    errorMessage = we.Message;
                    if (we.Status == WebExceptionStatus.ProtocolError)
                    {
                        HttpWebResponse webResponse = (HttpWebResponse)we.Response;
                        if (webResponse.StatusCode == HttpStatusCode.BadRequest)
                            MainConsole.Instance.WarnFormat("[WebUtils]: bad request to {0}, data {1}", url,
                                             data != null ? OSDParser.SerializeJsonString(data) : "");
                        else
                            MainConsole.Instance.WarnFormat("[WebUtils]: {0} to {1}, data {2}, response {3}", webResponse.StatusCode, url,
                                             data != null ? OSDParser.SerializeJsonString(data) : "", webResponse.StatusDescription);
                        return "";
                    }
                    if (request != null)
                        request.Abort();
                }
                catch (Exception ex)
                {
                    if (ex is System.UriFormatException)
                        errorMessage = ex.ToString() + " " + new System.Diagnostics.StackTrace().ToString();
                    else
                        errorMessage = ex.Message;
                    if (request != null)
                        request.Abort();
                }
                finally
                {
                    if (MainConsole.Instance != null)
                    {
                        if (errorMessage == "unknown error")
                        {
                            // This just dumps a warning for any operation that takes more than 500 ms
                            int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
                            if (MainConsole.Instance.IsEnabled(Level.All))
                            {
                                System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();

                                MainConsole.Instance.TraceFormat(
                                    "[WebUtils]: osd request (URI:{0}, METHOD:{1}, UPSTACK(4):{5}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                    url, method, tickdiff, tickdata, tickserialize, stackTrace.GetFrame(4).GetMethod().Name + " @ " + stackTrace.GetFrame(4).GetFileName() + ":" + stackTrace.GetFrame(4).GetFileLineNumber());
                            }
                            else
                                MainConsole.Instance.TraceFormat(
                                    "[WebUtils]: osd request (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                    url, method, tickdiff, tickdata, tickserialize);
                            if (tickdiff > 5000)
                                MainConsole.Instance.InfoFormat(
                                    "[WebUtils]: osd request took too long (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                                    url, method, tickdiff, tickdata, tickserialize);
                        }
                    }
                }

                if (MainConsole.Instance != null)
                    MainConsole.Instance.WarnFormat("[WebUtils] osd request failed: {0} to {1}, data {2}", errorMessage, url,
                                     data != null ? data.AsString() : "");
                return "";
            }
        }
        /// <summary>
        /// Deserialize the message
        /// </summary>
        /// <param name="map">An <see cref="OSDMap"/> containing the data</param>
        public void Deserialize(OSDMap map)
        {
            if (map.ContainsKey("parcel_id"))
                Request = new RemoteParcelRequestReply();
            else if (map.ContainsKey("location"))
                Request = new RemoteParcelRequestRequest();
            else
                Logger.Log("Unable to deserialize RemoteParcelRequest: No message handler exists for method: " + map.AsString(), Helpers.LogLevel.Warning);

            if (Request != null)
                Request.Deserialize(map);
        }
        /// <summary>
        /// Deserialize the message
        /// </summary>
        /// <param name="map">An <see cref="OSDMap"/> containing the data</param>
        public void Deserialize(OSDMap map)
        {
            if (map.ContainsKey("verb"))
            {
                if (map["verb"].AsString() == "GET")
                    Request = new ObjectMediaRequest();
                else if (map["verb"].AsString() == "UPDATE")
                    Request = new ObjectMediaUpdate();
            }
            else if (map.ContainsKey("object_media_version"))
                Request = new ObjectMediaResponse();
            else
                Logger.Log("Unable to deserialize ObjectMedia: No message handler exists for method: " + map.AsString(), Helpers.LogLevel.Warning);

            if (Request != null)
                Request.Deserialize(map);
        }
Example #5
0
        public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout)
        {
            int reqnum = m_requestNumber++;
            // m_log.DebugFormat("[WEB UTIL]: <{0}> start osd request for {1}, method {2}",reqnum,url,method);

            string errorMessage = "unknown error";
            int tickstart = Util.EnvironmentTickCount();
            int tickdata = 0;

            if(url == "")
                return ErrorResponseMap("No URL given.");

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method;
                request.Timeout = timeout;
                request.KeepAlive = false;
                request.MaximumAutomaticRedirections = 10;
                request.ReadWriteTimeout = timeout / 4;
                request.Headers[OSHeaderRequestID] = reqnum.ToString();

                // If there is some input, write it into the request
                if (data != null)
                {
                    string strBuffer = OSDParser.SerializeJsonString(data);
                    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);

                    if (buffer.Length <= 0)
                    {
                    }
                    else
                    {
                        request.ContentType = "application/json";
                        request.ContentLength = buffer.Length;   //Count bytes to send
                        using (Stream requestStream = request.GetRequestStream())
                            requestStream.Write(buffer, 0, buffer.Length);         //Send it
                    }
                }

                // capture how much time was spent writing, this may seem silly
                // but with the number concurrent requests, this often blocks
                tickdata = Util.EnvironmentTickCountSubtract(tickstart);

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        string responseStr = null;
                        responseStr = responseStream.GetStreamString();
                        // m_log.DebugFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr);
                        return CanonicalizeResults(responseStr);
                    }
                }
            }
            catch (WebException we)
            {
                errorMessage = we.Message;
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse webResponse = (HttpWebResponse)we.Response;
                    errorMessage = String.Format("[{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription);
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            finally
            {
                // This just dumps a warning for any operation that takes more than 100 ms
                int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
                if (tickdiff > LongCallTime)
                    m_log.InfoFormat("[WebUtils]: osd request <{0}> (URI:{1}, METHOD:{2}) took {3}ms overall, {4}ms writing",
                                     reqnum, url, method, tickdiff, tickdata);
            }

            m_log.WarnFormat("[WebUtils] <{0}> osd request failed: {1} to {2}, data {3}", reqnum, errorMessage, url, data != null ? data.AsString() : "");
            return ErrorResponseMap(errorMessage);
        }
        public static string ServiceOSDRequest(string url, OSDMap data, string method, int timeout)
        {
            // MainConsole.Instance.DebugFormat("[WEB UTIL]: <{0}> start osd request for {1}, method {2}",reqnum,url,method);

            string errorMessage = "unknown error";
            int tickstart = Util.EnvironmentTickCount();
            int tickdata = 0;
            int tickserialize = 0;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method;
                request.Timeout = timeout;
                request.KeepAlive = false;
                request.MaximumAutomaticRedirections = 10;
                request.ReadWriteTimeout = timeout / 2;

                // If there is some input, write it into the request
                if (data != null)
                {
                    string strBuffer = OSDParser.SerializeJsonString(data, true);
                    byte[] buffer = Encoding.UTF8.GetBytes(strBuffer);

                    if (buffer.Length <= 0)
                    {
                    }
                    else
                    {
                        request.ContentType = "application/json";
                        request.ContentLength = buffer.Length; //Count bytes to send
                        using (Stream requestStream = request.GetRequestStream())
                            requestStream.Write(buffer, 0, buffer.Length); //Send it
                    }
                }

                // capture how much time was spent writing, this may seem silly
                // but with the number concurrent requests, this often blocks
                tickdata = Util.EnvironmentTickCountSubtract(tickstart);

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        // capture how much time was spent writing, this may seem silly
                        // but with the number concurrent requests, this often blocks
                        tickserialize = Util.EnvironmentTickCountSubtract(tickstart) - tickdata;
                        string responseStr = responseStream.GetStreamString();
                        // MainConsole.Instance.DebugFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr);
                        return responseStr;
                    }
                }
            }
            catch (WebException we)
            {
                errorMessage = we.Message;
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse webResponse = (HttpWebResponse)we.Response;
                    errorMessage = String.Format("[{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription);
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            finally
            {
                if (errorMessage == "unknown error")
                {
                    // This just dumps a warning for any operation that takes more than 500 ms
                    int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
                    MainConsole.Instance.TraceFormat(
                        "[WebUtils]: osd request took too long (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                        url, method, tickdiff, tickdata, tickserialize);
                    if (tickdiff > 5000)
                        MainConsole.Instance.InfoFormat(
                            "[WebUtils]: osd request took too long (URI:{0}, METHOD:{1}) took {2}ms overall, {3}ms writing, {4}ms deserializing",
                            url, method, tickdiff, tickdata, tickserialize);
                }
            }

            MainConsole.Instance.WarnFormat("[WebUtils] osd request failed: {0} to {1}, data {2}", errorMessage, url,
                             data != null ? data.AsString() : "");
            return "";
        }
Example #7
0
        public static async Task<string> ServiceOSDRequest(string url, OSDMap data, string method, int timeout)
        {
            string errorMessage = "", response = null;
            int tickstart = Util.EnvironmentTickCount(), tickelapsed = 0;
            byte[] buffer = data != null ? Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(data, true)) : null;
            try
            {
                HttpClient client = new HttpClient();
                client.Timeout = TimeSpan.FromMilliseconds(timeout);

                HttpResponseMessage httpresponse;
                switch (method)
                {
                    case "PUT":
                        httpresponse = await client.PutAsync(url, new ByteArrayContent(buffer));
                        break;
                    case "DELETE":
                        httpresponse = await client.DeleteAsync(url);
                        break;
                    case "POST":
                        httpresponse = await client.PostAsync(url, new ByteArrayContent(buffer));
                        break;
                    case "GET":
                        httpresponse = await client.GetAsync(url);
                        break;
                    default:
                        httpresponse = await client.SendAsync(new HttpRequestMessage(new HttpMethod(method), url) { Content = new ByteArrayContent(buffer) });
                        break;
                }
                httpresponse.EnsureSuccessStatusCode();

                response = await httpresponse.Content.ReadAsStringAsync();

                tickelapsed = Util.EnvironmentTickCountSubtract(tickstart);

                if (MainConsole.Instance != null)
                {
                    if (errorMessage == "")//No error
                    {
                        // This just dumps a warning for any operation that takes more than 500 ms
                        if (MainConsole.Instance.IsDebugEnabled)
                        {
                            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();

                            MainConsole.Instance.Debug(
                                string.Format("[WebUtils]: osd request (URI:{0}, METHOD:{1}, UPSTACK(4):{3}) took {2}ms",
                                url, method, tickelapsed,
                                stackTrace.GetFrame(3).GetMethod().Name));
                        }
                        if (tickelapsed > 5000)
                            MainConsole.Instance.Info(
                                string.Format("[WebUtils]: osd request took too long (URI:{0}, METHOD:{1}) took {2}ms",
                                url, method, tickelapsed));
                    }
                }
            }
            catch (Exception ex)
            {
                if (MainConsole.Instance != null)
                    MainConsole.Instance.WarnFormat("[WebUtils] osd request failed: {0} to {1}, data {2}", ex.ToString(),
                                                    url,
                                                    data != null ? data.AsString() : "");
            }
            return response;
        }