listener: more code cleanup

This commit is contained in:
Hylke Bons 2012-01-31 23:23:52 +00:00
parent 343fdba74b
commit c50f5c62a9
2 changed files with 44 additions and 39 deletions

View file

@ -64,7 +64,7 @@ namespace SparkleLib {
SparkleHelpers.DebugInfo ("ListenerFactory",
"Refered to existing listener for " + announce_uri);
listener.AlsoListenTo (folder_identifier);
listener.AlsoListenToBase (folder_identifier);
return (SparkleListenerBase) listener;
}
}
@ -103,10 +103,10 @@ namespace SparkleLib {
public abstract void Connect ();
public abstract void Announce (SparkleAnnouncement announcent);
public abstract void AlsoListenTo (string folder_identifier);
public abstract bool IsConnected { get; }
public abstract bool IsConnecting { get; }
protected abstract void Announce (SparkleAnnouncement announcent);
protected abstract void AlsoListenTo (string folder_identifier);
protected List<string> channels = new List<string> ();
@ -171,6 +171,18 @@ namespace SparkleLib {
}
public void AlsoListenToBase (string channel)
{
if (!this.channels.Contains (channel) && IsConnected) {
SparkleHelpers.DebugInfo ("Listener",
"Subscribing to channel " + channel);
this.channels.Add (channel);
AlsoListenTo (channel);
}
}
public void Reconnect ()
{
SparkleHelpers.DebugInfo ("Listener", "Trying to reconnect to " + Server);

View file

@ -145,40 +145,9 @@ namespace SparkleLib {
}
public override void AlsoListenTo (string folder_identifier)
protected override void AlsoListenTo (string folder_identifier)
{
string channel = folder_identifier;
if (!base.channels.Contains (channel)) {
base.channels.Add (channel);
if (IsConnected) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
string to_send = "subscribe " + folder_identifier + "\n";
try {
lock (this.socket_lock) {
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
}
} catch (SocketException e) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
this.is_connected = false;
this.is_connecting = false;
OnDisconnected ();
}
}
}
}
public override void Announce (SparkleAnnouncement announcement)
{
string to_send = "announce " + announcement.FolderIdentifier
+ " " + announcement.Message + "\n";
string to_send = "subscribe " + folder_identifier + "\n";
try {
lock (this.socket_lock) {
@ -186,10 +155,34 @@ namespace SparkleLib {
}
} catch (SocketException e) {
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
SparkleHelpers.DebugInfo ("ListenerTcp",
"Could not connect to " + Server + ": " + e.Message);
this.is_connected = false;
OnDisconnected ();
this.is_connected = false;
this.is_connecting = false;
OnDisconnected ();
}
}
protected override void Announce (SparkleAnnouncement announcement)
{
string to_send = "announce " + announcement.FolderIdentifier
+ " " + announcement.Message + "\n";
try {
lock (this.socket_lock)
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
} catch (SocketException e) {
SparkleHelpers.DebugInfo ("ListenerTcp",
"Could not connect to " + Server + ": " + e.Message);
this.is_connected = false;
this.is_connecting = false;
OnDisconnected ();
}
}