Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Communication.Data/Message.cs @ 1094

Last change on this file since 1094 was 704, checked in by abeham, 16 years ago

[TICKET #297] communication restructuring

File size: 5.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7
8namespace HeuristicLab.Communication.Data {
9  public class Message : ItemBase {
10    public string Protocol;
11    public string Source;
12    public string Destination;
13    public DateTime Timestamp;
14    public IList<IVariable> Give;
15    public IList<IVariable> Expect;
16
17    public Message()
18      : base() {
19      Protocol = "unknown";
20      Source = "unknown";
21      Destination = "unknown";
22      Timestamp = DateTime.Now;
23      Give = new List<IVariable>();
24      Expect = new List<IVariable>();
25    }
26
27    #region clone & persistence
28    public override object Clone(IDictionary<Guid, object> clonedObjects) {
29      Message clone = new Message();
30      clonedObjects.Add(Guid, clone);
31      clone.Protocol = (string)Protocol.Clone();
32      clone.Source = (string)Source.Clone();
33      clone.Destination = (string)Destination.Clone();
34      clone.Timestamp = new DateTime(Timestamp.Ticks);
35      foreach (IVariable var in Give)
36        clone.Give.Add((IVariable)var.Clone(clonedObjects));
37      foreach (IVariable var in Expect)
38        clone.Expect.Add((IVariable)var.Clone(clonedObjects));
39      return clone;
40    }
41
42    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
43      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
44      XmlAttribute protoAttrib = document.CreateAttribute("Protocol");
45      protoAttrib.Value = (string)Protocol.Clone();
46      XmlAttribute sourceAttrib = document.CreateAttribute("Source");
47      sourceAttrib.Value = (string)Source.Clone();
48      XmlAttribute destinationAttrib = document.CreateAttribute("Destination");
49      destinationAttrib.Value = (string)Destination.Clone();
50      XmlAttribute timeStampAttrib = document.CreateAttribute("Timestamp");
51      StringBuilder tsBuilder = new StringBuilder();
52      tsBuilder.Append(Timestamp.Year);tsBuilder.Append("/");
53      tsBuilder.Append(Timestamp.Month);tsBuilder.Append("/");
54      tsBuilder.Append(Timestamp.Day);tsBuilder.Append(" ");
55      tsBuilder.Append(Timestamp.Hour);tsBuilder.Append(":");
56      tsBuilder.Append(Timestamp.Minute);tsBuilder.Append(":");
57      tsBuilder.Append(Timestamp.Second);tsBuilder.Append(":");
58      tsBuilder.Append(Timestamp.Millisecond);
59      timeStampAttrib.Value = tsBuilder.ToString();
60
61      node.Attributes.Append(protoAttrib);
62      node.Attributes.Append(sourceAttrib);
63      node.Attributes.Append(destinationAttrib);
64      node.Attributes.Append(timeStampAttrib);
65
66      XmlNode dataNode = document.CreateNode(XmlNodeType.Element, "Data", null);
67      XmlNode giveNode = document.CreateNode(XmlNodeType.Element, "Give", null);
68      foreach (IVariable var in Give) {
69        XmlNode varNode = document.CreateNode(XmlNodeType.Element, "Parameter", null);
70        XmlAttribute varNameAttrib = document.CreateAttribute("Name");
71        varNameAttrib.Value = (string)var.Name.Clone();
72        varNode.Attributes.Append(varNameAttrib);
73        varNode.AppendChild(var.Value.GetXmlNode("Value", document, persistedObjects));
74        giveNode.AppendChild(varNode);
75      }
76      dataNode.AppendChild(giveNode);
77      XmlNode expectNode = document.CreateNode(XmlNodeType.Element, "Expect", null);
78      foreach (IVariable var in Expect) {
79        XmlNode varNode = document.CreateNode(XmlNodeType.Element, "Parameter", null);
80        XmlAttribute varNameAttrib = document.CreateAttribute("Name");
81        varNameAttrib.Value = (string)var.Name.Clone();
82        varNode.Attributes.Append(varNameAttrib);
83        varNode.AppendChild(var.Value.GetXmlNode("Value", document, persistedObjects));
84        expectNode.AppendChild(varNode);
85      }
86      dataNode.AppendChild(expectNode);
87      node.AppendChild(dataNode);
88      return node;
89    }
90
91    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
92      base.Populate(node, restoredObjects);
93      Protocol = node.Attributes.GetNamedItem("Protocol").Value;
94      Source = node.Attributes.GetNamedItem("Source").Value;
95      Destination = node.Attributes.GetNamedItem("Destination").Value;
96      string ts = node.Attributes.GetNamedItem("Timestamp").Value;
97      string[] tsTokens = ts.Split(new string[] { "/", " ", ":" }, StringSplitOptions.RemoveEmptyEntries);
98      int year = int.Parse(tsTokens[0]);
99      int month = int.Parse(tsTokens[1]);
100      int day = int.Parse(tsTokens[2]);
101      int hour = int.Parse(tsTokens[3]);
102      int minute = int.Parse(tsTokens[4]);
103      int seconds = int.Parse(tsTokens[5]);
104      int milliseconds = int.Parse(tsTokens[6]);
105      Timestamp = new DateTime(year, month, day, hour, minute, seconds, milliseconds);
106      XmlNode dataNode = node.SelectSingleNode("Data");
107      XmlNode giveNode = dataNode.SelectSingleNode("Give");
108      XmlNodeList giveParams = giveNode.SelectNodes("Parameter");
109      Give = new List<IVariable>();
110      foreach (XmlNode n in giveParams) {
111        Give.Add(new Variable(n.Attributes.GetNamedItem("Name").Value, (IItem)PersistenceManager.Restore(n.SelectSingleNode("Value"), restoredObjects)));
112      }
113      XmlNode expectNode = dataNode.SelectSingleNode("Expect");
114      XmlNodeList expectParams = expectNode.SelectNodes("Parameter");
115      Expect = new List<IVariable>();
116      foreach (XmlNode n in expectParams) {
117        Expect.Add(new Variable(n.Attributes.GetNamedItem("Name").Value, (IItem)PersistenceManager.Restore(n.SelectSingleNode("Value"), restoredObjects)));
118      }
119    }
120    #endregion
121  }
122}
Note: See TracBrowser for help on using the repository browser.