DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
EmbeddedMessageEnvelope.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 namespace Deveel.Data.Protocol {
22  private static int dispatchCounter = -1;
23 
24  private EmbeddedMessageEnvelope(IDictionary<string, object> metadata, IMessage message) {
25  Message = message;
26  Metadata = metadata;
27 
28  object dispatchId;
29  if (!metadata.TryGetValue("DispatchID", out dispatchId))
30  throw new ArgumentException("Metadata must specify a Dispatch ID");
31 
32  DispatchId = (int) dispatchId;
33  }
34 
35  public IDictionary<string, object> Metadata { get; private set; }
36 
37  public int DispatchId { get; private set; }
38 
39  public IMessage Message { get; private set; }
40 
41  public ServerError Error { get; private set; }
42 
43  public void SetError(Exception error) {
44  // TODO: in another version there will be support for Class and Code of error
45  Error = new ServerError(-1, -1, error.Message);
46  }
47 
48  public static EmbeddedMessageEnvelope Create(int dispatchId, IMessage message) {
49  var metadata = new Dictionary<string, object>();
50  metadata["DispatchID"] = dispatchId;
51  return new EmbeddedMessageEnvelope(metadata, message);
52  }
53 
54  public static EmbeddedMessageEnvelope Create(IDictionary<string, object> metadata, IMessage message) {
55  if (metadata == null)
56  metadata = new Dictionary<string, object>();
57 
58  object dispatchId;
59  if (!metadata.TryGetValue("DispatchID", out dispatchId))
60  dispatchId = dispatchCounter++;
61 
62  metadata["DispatchID"] = dispatchId;
63  return new EmbeddedMessageEnvelope(metadata, message);
64  }
65  }
66 }
EmbeddedMessageEnvelope(IDictionary< string, object > metadata, IMessage message)
static EmbeddedMessageEnvelope Create(int dispatchId, IMessage message)
static EmbeddedMessageEnvelope Create(IDictionary< string, object > metadata, IMessage message)