Annotation of /trunk/AssetClient/Program.cs
Parent Directory
|
Revision Log
Revision 52 - (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 : | using System; | ||
| 31 : | jhurliman | 8 | using System.Collections.Generic; |
| 32 : | using System.IO; | ||
| 33 : | jhurliman | 40 | using System.Text; |
| 34 : | jhurliman | 8 | using OpenMetaverse; |
| 35 : | jhurliman | 11 | using OpenMetaverse.StructuredData; |
| 36 : | misterblue | 52 | using AssetServer; |
| 37 : | jhurliman | 40 | using LitJson; |
| 38 : | jhurliman | 8 | |
| 39 : | namespace AssetClient | ||
| 40 : | { | ||
| 41 : | public class Program | ||
| 42 : | { | ||
| 43 : | jhurliman | 47 | static readonly Uri DEFAULT_SERVER_URL = new Uri("http://localhost:9001"); |
| 44 : | jhurliman | 42 | |
| 45 : | jhurliman | 8 | static int verbosity; |
| 46 : | static NDesk.Options.OptionSet argParser; | ||
| 47 : | |||
| 48 : | static void Main(string[] args) | ||
| 49 : | { | ||
| 50 : | bool showhelp = false; | ||
| 51 : | jhurliman | 47 | Uri serverUrl = DEFAULT_SERVER_URL; |
| 52 : | Uri claimedIdentifier = null; | ||
| 53 : | UUID assetID = UUID.Zero; | ||
| 54 : | UUID authToken = UUID.Zero; | ||
| 55 : | jhurliman | 8 | string inputFile = null; |
| 56 : | string operation = null; | ||
| 57 : | string outputFile = null; | ||
| 58 : | jhurliman | 47 | string firstName = null; |
| 59 : | string lastName = null; | ||
| 60 : | string password = null; | ||
| 61 : | jhurliman | 8 | |
| 62 : | argParser = new NDesk.Options.OptionSet() | ||
| 63 : | jhurliman | 47 | .Add("s|server=", "URL of the asset server (default is 'http://localhost:9001/')", delegate(string v) { showhelp = !Uri.TryCreate(v, UriKind.Absolute, out serverUrl); }) |
| 64 : | .Add("a|assetid=", "asset ID to fetch data or metadata for", delegate(string v) { showhelp = !UUID.TryParse(v, out assetID); }) | ||
| 65 : | .Add("i|identity=", "OpenID identity to authenticate as", delegate(string v) { showhelp = !Uri.TryCreate(v, UriKind.Absolute, out claimedIdentifier); }) | ||
| 66 : | .Add("f|first=", "first name of the avatar to authenticate as", delegate(string v) { firstName = v; }) | ||
| 67 : | .Add("l|last=", "last name of the avatar to authenticate as", delegate(string v) { lastName = v; }) | ||
| 68 : | .Add("p|pass=", "password of the avatar to authenticate as", delegate(string v) { password = v; }) | ||
| 69 : | jhurliman | 8 | .Add("h|?|help", delegate(string v) { showhelp = (v != null); }) |
| 70 : | jhurliman | 47 | .Add("I|input-file=", "asset data to upload with createasset", delegate(string v) { inputFile = v; }) |
| 71 : | jhurliman | 8 | .Add("o|operation=", "data|metadata|createasset (default is 'data')", delegate(string v) { operation = v; }) |
| 72 : | .Add("O|output-file=", "file to write fetched data to. If 'default', the asset ID will be used as the filename", delegate(string v) { outputFile = v; }) | ||
| 73 : | .Add("v|verbose", delegate(string v) { if (v != null) ++verbosity; }); | ||
| 74 : | argParser.Parse(args); | ||
| 75 : | |||
| 76 : | if (!showhelp) | ||
| 77 : | { | ||
| 78 : | jhurliman | 47 | if (claimedIdentifier != null) |
| 79 : | { | ||
| 80 : | bool success = false; | ||
| 81 : | jhurliman | 8 | |
| 82 : | jhurliman | 47 | if (!String.IsNullOrEmpty(firstName) && !String.IsNullOrEmpty(lastName) && !String.IsNullOrEmpty(password)) |
| 83 : | { | ||
| 84 : | success = AssetClient.TryGetAuthToken(claimedIdentifier, firstName, lastName, password, serverUrl, out authToken); | ||
| 85 : | } | ||
| 86 : | else | ||
| 87 : | { | ||
| 88 : | ShowHelp(); | ||
| 89 : | return; | ||
| 90 : | } | ||
| 91 : | |||
| 92 : | if (!success) | ||
| 93 : | { | ||
| 94 : | Console.WriteLine("[ERROR] Failed to claim the identifier {0}", claimedIdentifier); | ||
| 95 : | return; | ||
| 96 : | } | ||
| 97 : | else if (verbosity > 0) | ||
| 98 : | { | ||
| 99 : | Console.WriteLine("Authenticated, authToken=" + authToken.ToString()); | ||
| 100 : | } | ||
| 101 : | } | ||
| 102 : | |||
| 103 : | jhurliman | 8 | if (operation == null) |
| 104 : | operation = "data"; | ||
| 105 : | |||
| 106 : | switch (operation) | ||
| 107 : | { | ||
| 108 : | case "data": | ||
| 109 : | jhurliman | 47 | if (assetID != UUID.Zero) |
| 110 : | FetchMetadataForData(assetID, authToken, serverUrl, outputFile); | ||
| 111 : | jhurliman | 8 | else |
| 112 : | ShowHelp(); | ||
| 113 : | break; | ||
| 114 : | case "metadata": | ||
| 115 : | jhurliman | 47 | if (assetID != UUID.Zero) |
| 116 : | FetchMetadata(assetID, authToken, serverUrl, outputFile); | ||
| 117 : | jhurliman | 8 | else |
| 118 : | ShowHelp(); | ||
| 119 : | break; | ||
| 120 : | case "createasset": | ||
| 121 : | jhurliman | 47 | CreateAsset(assetID, authToken, serverUrl, inputFile); |
| 122 : | jhurliman | 8 | break; |
| 123 : | default: | ||
| 124 : | Console.WriteLine("Unrecognized operation: " + operation); | ||
| 125 : | break; | ||
| 126 : | } | ||
| 127 : | } | ||
| 128 : | else | ||
| 129 : | { | ||
| 130 : | ShowHelp(); | ||
| 131 : | } | ||
| 132 : | } | ||
| 133 : | |||
| 134 : | jhurliman | 47 | static void FetchMetadataForData(UUID assetID, UUID authToken, Uri serverUrl, string outputFile) |
| 135 : | jhurliman | 8 | { |
| 136 : | jhurliman | 23 | if (verbosity > 0) |
| 137 : | Console.WriteLine(String.Format("Starting metadata fetch for asset {0}", assetID)); | ||
| 138 : | |||
| 139 : | jhurliman | 40 | // Fetch the metadata first to locate this asset, parse the metadata, then fetch the actual asset |
| 140 : | byte[] metadataBytes; | ||
| 141 : | jhurliman | 47 | if (AssetClient.TryGetMetadata(assetID, authToken, serverUrl, out metadataBytes)) |
| 142 : | jhurliman | 11 | { |
| 143 : | jhurliman | 40 | string jsonString = System.Text.Encoding.UTF8.GetString(metadataBytes); |
| 144 : | jhurliman | 11 | |
| 145 : | jhurliman | 40 | if (!String.IsNullOrEmpty(jsonString)) |
| 146 : | jhurliman | 25 | { |
| 147 : | jhurliman | 40 | OSD metadata = OSDParser.DeserializeJson(jsonString); |
| 148 : | OSDMap map = metadata as OSDMap; | ||
| 149 : | jhurliman | 20 | |
| 150 : | jhurliman | 40 | if (map != null && map.ContainsKey("methods")) |
| 151 : | { | ||
| 152 : | OSDMap methods = map["methods"] as OSDMap; | ||
| 153 : | Uri assetLocation = methods["data"].AsUri(); | ||
| 154 : | |||
| 155 : | if (assetLocation != null) | ||
| 156 : | { | ||
| 157 : | if (verbosity > 0) | ||
| 158 : | Console.WriteLine("Metadata retrieved, fetching asset data from " + assetLocation); | ||
| 159 : | |||
| 160 : | jhurliman | 47 | FetchData(assetID, authToken, assetLocation, outputFile); |
| 161 : | jhurliman | 40 | } |
| 162 : | else | ||
| 163 : | { | ||
| 164 : | Console.WriteLine("Metadata did not contain a method for retrieving asset data:"); | ||
| 165 : | Console.WriteLine(jsonString); | ||
| 166 : | } | ||
| 167 : | } | ||
| 168 : | else | ||
| 169 : | { | ||
| 170 : | Console.WriteLine("Failed parsing metadata:"); | ||
| 171 : | Console.WriteLine(jsonString); | ||
| 172 : | } | ||
| 173 : | jhurliman | 25 | } |
| 174 : | else | ||
| 175 : | { | ||
| 176 : | jhurliman | 40 | Console.WriteLine("Empty metadata returned from the server"); |
| 177 : | jhurliman | 25 | } |
| 178 : | jhurliman | 11 | } |
| 179 : | } | ||
| 180 : | |||
| 181 : | jhurliman | 47 | static void FetchData(UUID assetID, UUID authToken, Uri assetUrl, string outputFile) |
| 182 : | jhurliman | 11 | { |
| 183 : | jhurliman | 8 | if (verbosity > 0) |
| 184 : | jhurliman | 47 | Console.WriteLine(String.Format("Starting data fetch for asset {0} from {1}", assetID, assetUrl)); |
| 185 : | jhurliman | 8 | |
| 186 : | jhurliman | 20 | byte[] assetData; |
| 187 : | jhurliman | 47 | if (AssetClient.TryGetAsset(assetID, authToken, assetUrl, out assetData)) |
| 188 : | jhurliman | 8 | { |
| 189 : | if (verbosity > 0) | ||
| 190 : | jhurliman | 20 | Console.WriteLine(String.Format("Downloaded a {0} byte asset", assetData.Length)); |
| 191 : | jhurliman | 8 | |
| 192 : | try | ||
| 193 : | { | ||
| 194 : | Stream outStream; | ||
| 195 : | |||
| 196 : | if (outputFile == "default") | ||
| 197 : | jhurliman | 20 | outputFile = assetID.ToString(); |
| 198 : | jhurliman | 8 | |
| 199 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 200 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 201 : | else | ||
| 202 : | outStream = Console.OpenStandardOutput(); | ||
| 203 : | |||
| 204 : | int pos = 0; | ||
| 205 : | jhurliman | 20 | while (pos < assetData.Length) |
| 206 : | jhurliman | 8 | { |
| 207 : | jhurliman | 40 | int len = (assetData.Length - pos < 4096) ? assetData.Length - pos : 4096; |
| 208 : | jhurliman | 20 | outStream.Write(assetData, pos, len); |
| 209 : | jhurliman | 8 | pos += len; |
| 210 : | } | ||
| 211 : | |||
| 212 : | outStream.Close(); | ||
| 213 : | } | ||
| 214 : | catch (Exception ex) | ||
| 215 : | { | ||
| 216 : | Console.WriteLine("Error writing the asset: " + ex.Message); | ||
| 217 : | } | ||
| 218 : | } | ||
| 219 : | else | ||
| 220 : | { | ||
| 221 : | Console.WriteLine("Failed fetching asset " + assetID.ToString()); | ||
| 222 : | return; | ||
| 223 : | } | ||
| 224 : | } | ||
| 225 : | |||
| 226 : | jhurliman | 47 | static void FetchMetadata(UUID assetID, UUID authToken, Uri serverUrl, string outputFile) |
| 227 : | jhurliman | 8 | { |
| 228 : | if (verbosity > 0) | ||
| 229 : | Console.WriteLine("Starting metadata fetch for asset " + assetID.ToString()); | ||
| 230 : | |||
| 231 : | byte[] metadata; | ||
| 232 : | jhurliman | 47 | if (AssetClient.TryGetMetadata(assetID, authToken, serverUrl, out metadata)) |
| 233 : | jhurliman | 8 | { |
| 234 : | if (verbosity > 0) | ||
| 235 : | Console.WriteLine(String.Format("Downloaded {0} bytes of metadata", metadata.Length)); | ||
| 236 : | |||
| 237 : | try | ||
| 238 : | { | ||
| 239 : | Stream outStream; | ||
| 240 : | |||
| 241 : | if (outputFile == "default") | ||
| 242 : | jhurliman | 40 | outputFile = assetID.ToString() + ".txt"; |
| 243 : | jhurliman | 8 | |
| 244 : | if (!String.IsNullOrEmpty(outputFile)) | ||
| 245 : | outStream = File.Open(outputFile, FileMode.Create, FileAccess.Write); | ||
| 246 : | else | ||
| 247 : | outStream = Console.OpenStandardOutput(); | ||
| 248 : | |||
| 249 : | int pos = 0; | ||
| 250 : | while (pos < metadata.Length) | ||
| 251 : | { | ||
| 252 : | int len = (metadata.Length - pos < 1024) ? metadata.Length - pos : 1024; | ||
| 253 : | outStream.Write(metadata, pos, len); | ||
| 254 : | pos += len; | ||
| 255 : | } | ||
| 256 : | |||
| 257 : | outStream.Close(); | ||
| 258 : | } | ||
| 259 : | catch (Exception ex) | ||
| 260 : | { | ||
| 261 : | Console.WriteLine("Error writing the metadata: " + ex.Message); | ||
| 262 : | } | ||
| 263 : | } | ||
| 264 : | else | ||
| 265 : | { | ||
| 266 : | Console.WriteLine("Failed fetching metadata for asset " + assetID.ToString()); | ||
| 267 : | return; | ||
| 268 : | } | ||
| 269 : | } | ||
| 270 : | |||
| 271 : | jhurliman | 47 | static void CreateAsset(UUID assetID, UUID authToken, Uri serverUrl, string inputFile) |
| 272 : | jhurliman | 20 | { |
| 273 : | if (verbosity > 0) | ||
| 274 : | jhurliman | 23 | { |
| 275 : | if (assetID != UUID.Zero) | ||
| 276 : | jhurliman | 47 | Console.WriteLine(String.Format("Starting asset creation for asset {0} from file {1}", assetID, inputFile)); |
| 277 : | jhurliman | 23 | else |
| 278 : | Console.WriteLine("Starting asset creation from file " + inputFile); | ||
| 279 : | } | ||
| 280 : | jhurliman | 20 | |
| 281 : | byte[] assetData; | ||
| 282 : | try { assetData = File.ReadAllBytes(inputFile); } | ||
| 283 : | catch (Exception ex) | ||
| 284 : | { | ||
| 285 : | Console.WriteLine(ex.Message); | ||
| 286 : | return; | ||
| 287 : | } | ||
| 288 : | |||
| 289 : | string name = Path.GetFileNameWithoutExtension(inputFile); | ||
| 290 : | string extension = Path.GetExtension(inputFile).TrimStart('.').ToLower(); | ||
| 291 : | jhurliman | 41 | string contentType = AssetServer.Utils.ExtensionToContentType(extension); |
| 292 : | jhurliman | 20 | |
| 293 : | jhurliman | 47 | assetID = AssetClient.CreateAsset(name, "Created with AssetClient", contentType, false, authToken, |
| 294 : | serverUrl, assetData, assetID); | ||
| 295 : | jhurliman | 20 | |
| 296 : | if (assetID != UUID.Zero) | ||
| 297 : | Console.WriteLine("Created asset " + assetID.ToString()); | ||
| 298 : | else | ||
| 299 : | Console.WriteLine("Failed to create asset"); | ||
| 300 : | } | ||
| 301 : | |||
| 302 : | jhurliman | 8 | static void ShowHelp() |
| 303 : | { | ||
| 304 : | jhurliman | 43 | Console.WriteLine("Usage: AssetClient.exe [OPTION]..."); |
| 305 : | jhurliman | 8 | Console.WriteLine("A non-interactive client for the asset server"); |
| 306 : | Console.WriteLine("Options:"); | ||
| 307 : | argParser.WriteOptionDescriptions(Console.Out); | ||
| 308 : | } | ||
| 309 : | } | ||
| 310 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

