150 lines
4.8 KiB
C#
150 lines
4.8 KiB
C#
using System;
|
|
using System.Runtime.Serialization;
|
|
using System.Collections;
|
|
using System.Reflection;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
|
|
using BCG;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BCG {
|
|
// Thhis "wraps" around the following 4 PubSub meta objects, serializes it on the client and
|
|
// deserialize it on the pubsub
|
|
// PubMetaData
|
|
// SubMetaData
|
|
// TransactionMetaData
|
|
// UnSubMetaData
|
|
public class MetaData : ICloneable {
|
|
private IDictionary _name_value_map = new Dictionary<string, string>();
|
|
int mySize = -1;
|
|
|
|
public MetaData() { }
|
|
|
|
public MetaData(object o) { copyFrom(o); }
|
|
|
|
public MetaData(Stream s) {
|
|
long initPos = s.Position;
|
|
load(s);
|
|
mySize = Convert.ToInt32(s.Position - initPos);
|
|
}
|
|
|
|
public string this[string name] {
|
|
get { return (string)_name_value_map[name]; }
|
|
set {
|
|
if (value != null)
|
|
_name_value_map[name] = value;
|
|
}
|
|
}
|
|
|
|
private void _add_string_array(string name, string[] values) {
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (string s in values) {
|
|
if (sb.Length != 0)
|
|
sb.Append(" ");
|
|
sb.Append(s);
|
|
}
|
|
_name_value_map[name] = sb.ToString();
|
|
}
|
|
|
|
private int getAsInt(string name) { return Int32.Parse(this[name]); }
|
|
|
|
private string[] _get_as_string_array(string name) {
|
|
if (this[name] != "" && this[name] != null)
|
|
return this[name].Split(' ');
|
|
else
|
|
return new string[0];
|
|
}
|
|
|
|
public int size() {
|
|
if (mySize < 0) {
|
|
MemoryStream ms = new MemoryStream();
|
|
this.save(ms);
|
|
return mySize = (int)ms.Position;
|
|
} else
|
|
return mySize;
|
|
}
|
|
|
|
private bool isEqual(MetaData md) {
|
|
if (md._name_value_map.Keys.Count != _name_value_map.Keys.Count)
|
|
return false;
|
|
|
|
foreach (string k in _name_value_map.Keys)
|
|
if (!_name_value_map[k].Equals(md._name_value_map[k]))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
// loads the content of the stream into this instance's map
|
|
private void load(Stream s) {
|
|
_name_value_map = new Hashtable();
|
|
string[] state =
|
|
new BinaryReader(s).ReadString().Split('\n');
|
|
|
|
for (int i = 0; i < state.Length - 1; i += 2)
|
|
_name_value_map[state[i]] = state[i + 1];
|
|
}
|
|
|
|
// writes the content of this instance to stream s
|
|
public void save(Stream s) {
|
|
new BinaryWriter(s).Write(this.ToString());
|
|
}
|
|
|
|
public override string ToString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (string key in _name_value_map.Keys)
|
|
sb.AppendFormat("{0}\n{1}\n", key, _name_value_map[key]);
|
|
return sb.ToString();
|
|
}
|
|
|
|
// iterates through the fields and enters the name, values into _hash
|
|
public void copyFrom(object o) {
|
|
this["op-code"] = o.GetType().Name;
|
|
foreach (FieldInfo fi in o.GetType().GetFields()) {
|
|
if (fi.GetValue(o) == null)
|
|
continue;
|
|
else if (fi.GetValue(o) is String[])
|
|
_add_string_array(fi.Name, (string[])fi.GetValue(o));
|
|
else
|
|
_name_value_map[fi.Name] = fi.GetValue(o).ToString();
|
|
|
|
}
|
|
}
|
|
// copies the name value map into object o
|
|
public void copyTo(object o) {
|
|
foreach (FieldInfo fi in o.GetType().GetFields()) {
|
|
string value = this[fi.Name];
|
|
if (value == null) {
|
|
} else {
|
|
if (fi.FieldType.Equals(typeof(string)))
|
|
fi.SetValue(o, value);
|
|
else if (fi.FieldType.Equals(typeof(int)))
|
|
fi.SetValue(o, Int32.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(long)))
|
|
fi.SetValue(o, long.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(double)))
|
|
fi.SetValue(o, double.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(DateTime)))
|
|
fi.SetValue(o, DateTime.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(Boolean)))
|
|
fi.SetValue(o, Boolean.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(TimeSpan)))
|
|
fi.SetValue(o, TimeSpan.Parse(value));
|
|
else if (fi.FieldType.Equals(typeof(String[])))
|
|
fi.SetValue(o, value.Split(' '));
|
|
else
|
|
throw new Exception("Field type not supported");
|
|
}
|
|
}
|
|
}
|
|
|
|
public object Clone() {
|
|
MemoryStream ms = new MemoryStream();
|
|
this.save(ms);
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|
return new MetaData(ms);
|
|
}
|
|
}
|
|
}
|