2009-08-31 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / RabbitMQ.Client / src / examples / SendMap.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
41 //   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 //   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 //   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 //   Technologies LLC, and Rabbit Technologies Ltd.
45 //
46 //   Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 //   Ltd. Portions created by Cohesive Financial Technologies LLC are
48 //   Copyright (C) 2007-2009 Cohesive Financial Technologies
49 //   LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 //   (C) 2007-2009 Rabbit Technologies Ltd.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58 using System.Collections;
59 using System.Net;
60 using System.Text;
61
62 using RabbitMQ.Client;
63 using RabbitMQ.Client.Content;
64
65 namespace RabbitMQ.Client.Examples {
66     public class SendMap {
67         public static int Main(string[] args) {
68             try {
69                 bool persistMode = false;
70
71                 int optionIndex = 0;
72                 while (optionIndex < args.Length) {
73                     if (args[optionIndex] == "/persist") {
74                         persistMode = true;
75                     } else {
76                         break;
77                     }
78                     optionIndex++;
79                 }
80
81                 if (((args.Length - optionIndex) < 1) ||
82                     (((args.Length - optionIndex - 1) % 2) == 1))
83                 {
84                     Console.Error.WriteLine("Usage: SendMap [<option> ...] <exchange-uri> [[<key> <value>] ...]");
85                     Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
86                     Console.Error.WriteLine("Exchange-URI: amqp://host[:port]/exchange[/routingkey][?type=exchangetype]");
87                     Console.Error.WriteLine("Keys must start with '+' or with '-'. Those starting with '+' are placed in");
88                     Console.Error.WriteLine("the body of the message, and those starting with '-' are placed in the headers.");
89                     Console.Error.WriteLine("Values must start with a single character typecode and a colon.");
90                     Console.Error.WriteLine("Supported typecodes are:");
91                     Console.Error.WriteLine(" S - string/byte array");
92                     Console.Error.WriteLine(" x - byte array (base-64)");
93                     Console.Error.WriteLine(" t - boolean");
94                     Console.Error.WriteLine(" i - 32-bit integer");
95                     Console.Error.WriteLine(" d - double-precision float");
96                     Console.Error.WriteLine(" D - fixed-point decimal");
97                     Console.Error.WriteLine("Note that some types are valid only in the body of a message, and some are");
98                     Console.Error.WriteLine("valid only in the headers.");
99                     Console.Error.WriteLine("The exchange \"amq.default\" is an alias for the default (\"\") AMQP exchange,");
100                     Console.Error.WriteLine("introduced so that the default exchange can be addressed via URI syntax.");
101                     Console.Error.WriteLine("Available options:");
102                     Console.Error.WriteLine("  /persist     send message in 'persistent' mode");
103                     return 1;
104                 }
105                 
106                 Uri uri = new Uri(args[optionIndex++]);
107                 string exchange = uri.Segments[1].TrimEnd(new char[] { '/' });
108                 string exchangeType = uri.Query.StartsWith("?type=") ? uri.Query.Substring(6) : null;
109                 string routingKey = uri.Segments.Length > 2 ? uri.Segments[2] : "";
110
111                 if (exchange == "amq.default") {
112                     exchange = "";
113                 }
114
115                 using (IConnection conn = new ConnectionFactory().CreateConnection(uri))
116                 {
117                     using (IModel ch = conn.CreateModel()) {
118
119                         if (exchangeType != null) {
120                             ch.ExchangeDeclare(exchange, exchangeType);
121                         }
122
123                         IMapMessageBuilder b = new MapMessageBuilder(ch);
124                         while ((optionIndex + 1) < args.Length) {
125                             string keyAndDiscriminator = args[optionIndex++];
126                             string valueAndType = args[optionIndex++];
127
128                             if (keyAndDiscriminator.Length < 1) {
129                                 Console.Error.WriteLine("Invalid key: '{0}'", keyAndDiscriminator);
130                                 return 1;
131                             }
132                             string key = keyAndDiscriminator.Substring(1);
133                             char discriminator = keyAndDiscriminator[0];
134
135                             IDictionary target;
136                             switch (discriminator) {
137                               case '-':
138                                   target = b.Headers;
139                                   break;
140                               case '+':
141                                   target = b.Body;
142                                   break;
143                               default:
144                                   Console.Error.WriteLine("Invalid key: '{0}'",
145                                                           keyAndDiscriminator);
146                                   return 1;
147                             }
148
149                             if (valueAndType.Length < 2 || valueAndType[1] != ':') {
150                                 Console.Error.WriteLine("Invalid value: '{0}'", valueAndType);
151                                 return 1;
152                             }
153                             string valueStr = valueAndType.Substring(2);
154                             char typeCode = valueAndType[0];
155
156                             object value;
157                             switch (typeCode) {
158                               case 'S':
159                                   value = valueStr;
160                                   break;
161                               case 'x':
162                                   value = new BinaryTableValue(Convert.FromBase64String(valueStr));
163                                   break;
164                               case 't':
165                                   value = (valueStr.ToLower() == "true" ||
166                                            valueStr.ToLower() == "yes" ||
167                                            valueStr.ToLower() == "on" ||
168                                            valueStr == "1");
169                                   break;
170                               case 'i':
171                                   value = int.Parse(valueStr);
172                                   break;
173                               case 'd':
174                                   value = double.Parse(valueStr);
175                                   break;
176                               case 'D':
177                                   value = decimal.Parse(valueStr);
178                                   break;
179                               default:
180                                   Console.Error.WriteLine("Invalid type code: '{0}'", typeCode);
181                                   return 1;
182                             }
183
184                             target[key] = value;
185                         }
186                         if (persistMode) {
187                             ((IBasicProperties) b.GetContentHeader()).DeliveryMode = 2;
188                         }
189                         ch.BasicPublish(exchange,
190                                         routingKey,
191                                         (IBasicProperties) b.GetContentHeader(),
192                                         b.GetContentBody());
193                         return 0;
194                     }
195                 }
196             } catch (Exception e) {
197                 Console.Error.WriteLine(e);
198                 return 2;
199             }
200         }
201     }
202 }