Convert JSON commands to TCP server into XML fragments.

This commit is contained in:
Alex Hudson 2011-06-28 22:58:04 +01:00
parent 236a086e2c
commit 24491508cd

View file

@ -16,10 +16,14 @@
using System; using System;
using System.IO;
using System.Xml;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace SparkleLib { namespace SparkleLib {
@ -52,6 +56,23 @@ namespace SparkleLib {
} }
} }
private void SendCommand(TcpMessagePacket pkt)
{
XmlSerializer serializer = new XmlSerializer(typeof(TcpMessagePacket));
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add(String.Empty, String.Empty);
StringBuilder output = new StringBuilder();
XmlWriter writer = XmlWriter.Create(output,
new XmlWriterSettings { OmitXmlDeclaration = true });
serializer.Serialize(writer, pkt, emptyNamespace);
lock (this.mutex) {
this.socket.Send(Encoding.UTF8.GetBytes(output.ToString()));
}
}
// Starts a new thread and listens to the channel // Starts a new thread and listens to the channel
public override void Connect () public override void Connect ()
@ -75,9 +96,7 @@ namespace SparkleLib {
foreach (string channel in base.channels) { foreach (string channel in base.channels) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel); SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
byte [] message = Encoding.UTF8.GetBytes ( this.SendCommand(new TcpMessagePacket(channel, "subscribe"));
"{\"repo\": \"" + channel + "\", \"command\": \"subscribe\"}");
this.socket.Send (message);
} }
byte [] bytes = new byte [4096]; byte [] bytes = new byte [4096];
@ -118,9 +137,7 @@ namespace SparkleLib {
if (IsConnected) { if (IsConnected) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel); SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
byte [] message = Encoding.UTF8.GetBytes ( this.SendCommand(new TcpMessagePacket(channel, "subscribe"));
"{\"folder\": \"" + channel + "\", \"command\": \"subscribe\"}");
this.socket.Send (message);
} }
} }
} }
@ -128,10 +145,7 @@ namespace SparkleLib {
public override void Announce (SparkleAnnouncement announcement) public override void Announce (SparkleAnnouncement announcement)
{ {
string channel = announcement.FolderIdentifier; this.SendCommand(new TcpMessagePacket(announcement.FolderIdentifier, "publish"));
byte [] message = Encoding.UTF8.GetBytes (
"{\"folder\": \"" + channel + "\", \"command\": \"publish\"}");
this.socket.Send (message);
// Also announce to ourselves for debugging purposes // Also announce to ourselves for debugging purposes
// base.OnAnnouncement (announcement); // base.OnAnnouncement (announcement);
@ -145,4 +159,22 @@ namespace SparkleLib {
base.Dispose (); base.Dispose ();
} }
} }
[Serializable,XmlRoot("packet")]
public class TcpMessagePacket
{
public string repo { get; set; }
public string command { get; set; }
public string readable { get; set; }
public TcpMessagePacket(string repo, string command) {
this.repo = repo;
this.command = command;
}
public TcpMessagePacket() {
this.repo = "none";
this.command = "invalid";
}
}
} }