Make listener type configurable from config file

This commit is contained in:
Alex Hudson 2011-06-28 19:36:47 +01:00 committed by Hylke
parent 7a7da66a50
commit b27725ab72
6 changed files with 65 additions and 39 deletions

View file

@ -153,6 +153,20 @@ namespace SparkleLib {
Save (); Save ();
} }
public bool SetFolderOptionalAttribute (string name, string key, string value)
{
XmlNode folder = this.GetFolder(name);
if (folder == null) return false;
if (folder[key] != null) {
folder[key].InnerText = value;
} else {
XmlNode new_node = CreateElement(key);
new_node.InnerText = value;
folder.AppendChild(new_node);
}
return true;
}
public void RemoveFolder (string name) public void RemoveFolder (string name)
{ {

View file

@ -39,31 +39,32 @@ namespace SparkleLib {
private static List<SparkleListenerBase> listeners; private static List<SparkleListenerBase> listeners;
public static SparkleListenerIrc CreateIrcListener (string server, string folder_identifier, public static SparkleListenerBase CreateListener (string uri, string folder_identifier)
string announcements)
{ {
if (listeners == null) if (listeners == null)
listeners = new List<SparkleListenerBase> (); listeners = new List<SparkleListenerBase> ();
// This is SparkleShare's centralized notification service. Uri listen_on = new Uri(uri);
// Don't worry, we only use this server as a backup if you
// don't have your own. All data needed to connect is hashed and
// we don't store any personal information ever
if (announcements == null)
server = "204.62.14.135";
else
server = announcements;
foreach (SparkleListenerBase listener in listeners) { foreach (SparkleListenerBase listener in listeners) {
if (listener.Server.Equals (server)) { if (listener.Server.Equals (uri)) {
SparkleHelpers.DebugInfo ("ListenerFactory", "Refered to existing listener for " + server); SparkleHelpers.DebugInfo ("ListenerFactory", "Refered to existing listener for " + uri);
listener.AlsoListenTo (folder_identifier); listener.AlsoListenTo (folder_identifier);
return (SparkleListenerIrc) listener; return (SparkleListenerBase) listener;
} }
} }
SparkleHelpers.DebugInfo ("ListenerFactory", "Issued new listener for " + server); SparkleHelpers.DebugInfo ("ListenerFactory", "Issued new listener for " + uri);
listeners.Add (new SparkleListenerIrc (server, folder_identifier, announcements)); switch (listen_on.Scheme) {
case "tcp":
listeners.Add (new SparkleListenerTcp (listen_on, folder_identifier));
break;
case "irc":
default:
listeners.Add (new SparkleListenerIrc (listen_on, folder_identifier));
break;
}
return (SparkleListenerIrc) listeners [listeners.Count - 1]; return (SparkleListenerIrc) listeners [listeners.Count - 1];
} }
} }
@ -97,16 +98,17 @@ namespace SparkleLib {
protected List<SparkleAnnouncement> queue_up = new List<SparkleAnnouncement> (); protected List<SparkleAnnouncement> queue_up = new List<SparkleAnnouncement> ();
protected List<SparkleAnnouncement> queue_down = new List<SparkleAnnouncement> (); protected List<SparkleAnnouncement> queue_down = new List<SparkleAnnouncement> ();
protected bool is_connecting; protected bool is_connecting;
protected string server; protected Uri server;
protected Timer reconnect_timer = new Timer { Interval = 60 * 1000, Enabled = true }; protected Timer reconnect_timer = new Timer { Interval = 60 * 1000, Enabled = true };
public SparkleListenerBase (string server, string folder_identifier, string announcements) { public SparkleListenerBase (Uri server, string folder_identifier) {
this.reconnect_timer.Elapsed += delegate { this.reconnect_timer.Elapsed += delegate {
if (!IsConnected && !this.is_connecting) if (!IsConnected && !this.is_connecting)
Reconnect (); Reconnect ();
}; };
this.reconnect_timer.Start (); this.server = server;
this.reconnect_timer.Start ();
} }
@ -185,7 +187,7 @@ namespace SparkleLib {
} }
public string Server { public Uri Server {
get { get {
return this.server; return this.server;
} }

View file

@ -31,11 +31,9 @@ namespace SparkleLib {
private string nick; private string nick;
public SparkleListenerIrc (string server, string folder_identifier, string announcements) : public SparkleListenerIrc (Uri server, string folder_identifier) :
base (server, folder_identifier, announcements) base (server, folder_identifier)
{ {
base.server = server;
// Try to get a uniqueish nickname // Try to get a uniqueish nickname
this.nick = SHA1 (DateTime.Now.ToString ("ffffff") + "sparkles"); this.nick = SHA1 (DateTime.Now.ToString ("ffffff") + "sparkles");
@ -90,9 +88,10 @@ namespace SparkleLib {
this.thread = new Thread ( this.thread = new Thread (
new ThreadStart (delegate { new ThreadStart (delegate {
try { try {
// Connect, login, and join the channel // Connect, login, and join the channel
this.client.Connect (new string [] {base.server}, 6667); int port = base.server.Port;
if (port < 0) port = 6667;
this.client.Connect (base.server.Host, port);
this.client.Login (this.nick, this.nick); this.client.Login (this.nick, this.nick);
foreach (string channel in base.channels) { foreach (string channel in base.channels) {

View file

@ -28,10 +28,9 @@ namespace SparkleLib {
private Thread thread; private Thread thread;
private Socket socket; private Socket socket;
public SparkleListenerTcp (string server, string folder_identifier, string announcements) : public SparkleListenerTcp (Uri server, string folder_identifier) :
base (server, folder_identifier, announcements) base (server, folder_identifier)
{ {
base.server = server;
base.channels.Add (folder_identifier); base.channels.Add (folder_identifier);
this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
/* /*
@ -69,7 +68,7 @@ namespace SparkleLib {
// 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 ()
{ {
SparkleHelpers.DebugInfo ("ListenerTcp", "Connecting to " + Server); SparkleHelpers.DebugInfo ("ListenerTcp", "Connecting to " + Server.Host);
base.is_connecting = true; base.is_connecting = true;
@ -78,7 +77,9 @@ namespace SparkleLib {
try { try {
// Connect and subscribe to the channel // Connect and subscribe to the channel
this.socket.Connect (Server, 9999); int port = Server.Port;
if (port < 0) port = 9999;
this.socket.Connect (Server.Host, port);
base.is_connecting = false; base.is_connecting = false;
foreach (string channel in base.channels) { foreach (string channel in base.channels) {

View file

@ -91,8 +91,6 @@ namespace SparkleLib {
if (CurrentRevision == null) if (CurrentRevision == null)
CreateInitialChangeSet (); CreateInitialChangeSet ();
CreateListener ();
this.local_timer.Elapsed += delegate (object o, ElapsedEventArgs args) { this.local_timer.Elapsed += delegate (object o, ElapsedEventArgs args) {
CheckForChanges (); CheckForChanges ();
}; };
@ -219,10 +217,9 @@ namespace SparkleLib {
} }
private void CreateListener () public void CreateListener (string uri, string folder_name)
{ {
this.listener = SparkleListenerFactory.CreateIrcListener (Domain, Identifier, this.listener = SparkleListenerFactory.CreateListener(uri, this.Identifier);
SparkleConfig.DefaultConfig.GetAnnouncementsForFolder (Name));
// Stop polling when the connection to the irc channel is succesful // Stop polling when the connection to the irc channel is succesful
this.listener.Connected += delegate { this.listener.Connected += delegate {

View file

@ -530,7 +530,6 @@ namespace SparkleShare {
if (backend == null) if (backend == null)
return; return;
SparkleRepoBase repo = null; SparkleRepoBase repo = null;
@ -543,6 +542,20 @@ namespace SparkleShare {
else else
repo = new SparkleRepoGit (folder_path, SparkleBackend.DefaultBackend); repo = new SparkleRepoGit (folder_path, SparkleBackend.DefaultBackend);
string notify_uri = SparkleConfig.DefaultConfig.GetNotificationUrlForFolder (folder_name);
if (notify_uri == null) {
string announcements = SparkleConfig.DefaultConfig.GetAnnouncementsForFolder (folder_name);
if (announcements == null)
// This is SparkleShare's centralized notification service.
// Don't worry, we only use this server as a backup if you
// don't have your own. All data needed to connect is hashed and
// we don't store any personal information ever
announcements = "204.62.14.135";
notify_uri = String.Format("irc://{0}/", announcements);
}
repo.CreateListener(notify_uri, folder_name);
repo.NewChangeSet += delegate (SparkleChangeSet change_set, string repository_path) { repo.NewChangeSet += delegate (SparkleChangeSet change_set, string repository_path) {
string message = FormatMessage (change_set); string message = FormatMessage (change_set);