Annotation of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 17 - (view) (download)
| 1 : | jhurliman | 17 | /* |
| 2 : | * Copyright (c) 2008 Intel Corporation | ||
| 3 : | * All rights reserved. | ||
| 4 : | * Redistribution and use in source and binary forms, with or without | ||
| 5 : | * modification, are permitted provided that the following conditions | ||
| 6 : | * are met: | ||
| 7 : | * | ||
| 8 : | * -- Redistributions of source code must retain the above copyright | ||
| 9 : | * notice, this list of conditions and the following disclaimer. | ||
| 10 : | * -- Redistributions in binary form must reproduce the above copyright | ||
| 11 : | * notice, this list of conditions and the following disclaimer in the | ||
| 12 : | * documentation and/or other materials provided with the distribution. | ||
| 13 : | * -- Neither the name of the Intel Corporation nor the names of its | ||
| 14 : | * contributors may be used to endorse or promote products derived from | ||
| 15 : | * this software without specific prior written permission. | ||
| 16 : | * | ||
| 17 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 18 : | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 19 : | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
| 20 : | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
| 21 : | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 22 : | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 23 : | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 24 : | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 25 : | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 26 : | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 : | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 : | */ | ||
| 29 : | |||
| 30 : | jhurliman | 8 | using System; |
| 31 : | using System.Net; | ||
| 32 : | using System.IO; | ||
| 33 : | using OpenMetaverse; | ||
| 34 : | |||
| 35 : | namespace AssetClient | ||
| 36 : | { | ||
| 37 : | public static class AssetClient | ||
| 38 : | { | ||
| 39 : | const string ASSET_SERVER_URL = "http://localhost:9001"; | ||
| 40 : | |||
| 41 : | jhurliman | 11 | public static bool TryGetAsset(UUID assetID, string assetURL, out Asset asset) |
| 42 : | jhurliman | 8 | { |
| 43 : | asset = null; | ||
| 44 : | |||
| 45 : | try | ||
| 46 : | { | ||
| 47 : | jhurliman | 11 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(assetURL); |
| 48 : | jhurliman | 8 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
| 49 : | |||
| 50 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 51 : | { | ||
| 52 : | int contentLength = (int)response.ContentLength; | ||
| 53 : | byte[] assetData = new byte[contentLength]; | ||
| 54 : | BinaryReader reader = new BinaryReader(response.GetResponseStream()); | ||
| 55 : | int pos = 0; | ||
| 56 : | |||
| 57 : | // Loop until we have read all of the bytes from the response stream | ||
| 58 : | while (pos < contentLength) | ||
| 59 : | pos += reader.Read(assetData, pos, contentLength - pos); | ||
| 60 : | |||
| 61 : | reader.Close(); | ||
| 62 : | |||
| 63 : | switch (response.ContentType) | ||
| 64 : | { | ||
| 65 : | case "image/jp2": | ||
| 66 : | asset = new AssetTexture(assetID, assetData); | ||
| 67 : | if (((AssetTexture)asset).DecodeLayerBoundaries()) | ||
| 68 : | return true; | ||
| 69 : | else | ||
| 70 : | Logger.Log("Failed to decode texture layer boundaries", Helpers.LogLevel.Error); | ||
| 71 : | break; | ||
| 72 : | case "audio/ogg": | ||
| 73 : | asset = new AssetSound(assetID, assetData); | ||
| 74 : | return true; | ||
| 75 : | default: | ||
| 76 : | Logger.Log("Unhandled asset content type: " + response.ContentType, Helpers.LogLevel.Error); | ||
| 77 : | break; | ||
| 78 : | } | ||
| 79 : | } | ||
| 80 : | } | ||
| 81 : | catch (Exception ex) | ||
| 82 : | { | ||
| 83 : | Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); | ||
| 84 : | } | ||
| 85 : | |||
| 86 : | return false; | ||
| 87 : | } | ||
| 88 : | |||
| 89 : | public static bool TryGetMetadata(UUID assetID, out byte[] metadata) | ||
| 90 : | { | ||
| 91 : | return TryGetMetadata(assetID, ASSET_SERVER_URL, out metadata); | ||
| 92 : | } | ||
| 93 : | |||
| 94 : | public static bool TryGetMetadata(UUID assetID, string assetServerURL, out byte[] metadata) | ||
| 95 : | { | ||
| 96 : | metadata = null; | ||
| 97 : | assetServerURL = assetServerURL.TrimEnd('/'); | ||
| 98 : | string requestURL = String.Format("{0}/{1}/metadata", assetServerURL, assetID); | ||
| 99 : | |||
| 100 : | try | ||
| 101 : | { | ||
| 102 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 103 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 104 : | |||
| 105 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 106 : | { | ||
| 107 : | int contentLength = (int)response.ContentLength; | ||
| 108 : | metadata = new byte[contentLength]; | ||
| 109 : | BinaryReader reader = new BinaryReader(response.GetResponseStream()); | ||
| 110 : | int pos = 0; | ||
| 111 : | |||
| 112 : | // Loop until we have read all of the bytes from the response stream | ||
| 113 : | while (pos < contentLength) | ||
| 114 : | pos += reader.Read(metadata, pos, contentLength - pos); | ||
| 115 : | |||
| 116 : | reader.Close(); | ||
| 117 : | return true; | ||
| 118 : | } | ||
| 119 : | } | ||
| 120 : | catch (Exception ex) | ||
| 121 : | { | ||
| 122 : | Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); | ||
| 123 : | } | ||
| 124 : | |||
| 125 : | return false; | ||
| 126 : | } | ||
| 127 : | jhurliman | 10 | |
| 128 : | public static UUID CreateAsset(Asset asset) | ||
| 129 : | { | ||
| 130 : | // FIXME: Implement this | ||
| 131 : | return UUID.Zero; | ||
| 132 : | } | ||
| 133 : | jhurliman | 8 | } |
| 134 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

