DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
NetworkEnvelope.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 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 using System;
17 using System.Collections.Generic;
18 using System.Runtime.Serialization;
19 
20 namespace Deveel.Data.Protocol {
21  [Serializable]
22  public class NetworkEnvelope : IMessageEnvelope, ISerializable {
23  private readonly Dictionary<string, object> metadata;
24 
25  public NetworkEnvelope(int dispatchId, IMessage message) {
26  Message = message;
27 
28  metadata = new Dictionary<string, object>();
29  }
30 
31  protected NetworkEnvelope(SerializationInfo info, StreamingContext context) {
32  DispatchId = info.GetInt32(NetworkEnvelopeMetadataKeys.DispatchId);
33  IssueDate = new DateTime(info.GetInt64(NetworkEnvelopeMetadataKeys.IssueDate));
34  Error = (ServerError) info.GetValue("Error", typeof (ServerError));
35  Message = (IMessage) info.GetValue("Message", typeof (IMessage));
36  }
37 
38  IDictionary<string, object> IMessageEnvelope.Metadata {
39  get { return metadata; }
40  }
41 
42  protected object GetMetadata(string key) {
43  object value;
44  if (!metadata.TryGetValue(key, out value))
45  return null;
46 
47  return value;
48  }
49 
50  protected void SetMetadata(string key, object value) {
51  metadata[key] = value;
52  }
53 
54  public IMessage Message { get; private set; }
55 
56  public ServerError Error { get; set; }
57 
58  public int DispatchId {
59  get { return (int) GetMetadata(NetworkEnvelopeMetadataKeys.DispatchId); }
60  set { SetMetadata(NetworkEnvelopeMetadataKeys.DispatchId, value); }
61  }
62 
63  public DateTime IssueDate {
64  get { return (DateTime) GetMetadata(NetworkEnvelopeMetadataKeys.IssueDate); }
65  set { SetMetadata(NetworkEnvelopeMetadataKeys.IssueDate, value);}
66  }
67 
68  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
69  info.AddValue(NetworkEnvelopeMetadataKeys.DispatchId, DispatchId);
70  info.AddValue(NetworkEnvelopeMetadataKeys.IssueDate, IssueDate.Ticks);
71  info.AddValue("Error", Error);
72  info.AddValue("Message", Message);
73  }
74  }
75 }
IDictionary< string, object > Metadata
readonly Dictionary< string, object > metadata
void SetMetadata(string key, object value)
NetworkEnvelope(int dispatchId, IMessage message)
NetworkEnvelope(SerializationInfo info, StreamingContext context)