[listener] Add an IRC connection and listen to updates. hardcoded testing channel for now

This commit is contained in:
Hylke Bons 2010-09-12 14:00:07 +01:00
parent 301815b695
commit 0ee0fd605b
3 changed files with 137 additions and 62 deletions

View file

@ -25,62 +25,63 @@ namespace SparkleLib {
public class SparkleListener public class SparkleListener
{ {
public SparkleListener () { public IrcClient Client;
public string Server;
public string Channel;
public string Nick;
public int Port;
IrcClient irc_client = new IrcClient (); public SparkleListener (string server, string channel, string nick)
{
//irc_client.OnRawMessage += HandleRawMessage; Server = server;
Channel = channel;
Nick = nick.Replace ("@", "_at_").Replace (".", "_dot_");
Port = 6667;
string [] server = new string [] {"irc.gnome.org"}; // TODO: Remove these hardcoded values
int port = 6667; Channel = "#sparkletest";
string channel = "#sparkletest"; Server = "irc.gnome.org";
irc_client.OnConnected += delegate {
Console.WriteLine ("!!!!!!!!!!11");
};
irc_client.OnChannelMessage += delegate {
Console.WriteLine ("!!!22222222222!!!!!!!11");
};
try{
irc_client.Connect (server, port);
} catch (Exception e) {
Console.WriteLine("Error occured. Lawldongs!");
Console.WriteLine(e);
}
irc_client.Login("SmartIRC", "Stupid Bot");
irc_client.RfcJoin(channel);
irc_client.SendMessage(SendType.Message, channel, "HEllo");
Thread thread = new Thread(new ThreadStart(delegate {
irc_client.Listen ();
}));
thread.Start();
Client = new IrcClient ();
Client.AutoRejoin = true;
Client.AutoRetry = true;
Client.AutoRelogin = true;
} }
void HandleRawMessage (object sender, IrcEventArgs args) // Starts a new thread and listens to the channel
public void Listen ()
{
{ try {
System.Console.WriteLine(args.Data.Nick+": "+args.Data.Message); // Connect to the server
Client.Connect (new string [] {Server}, Port);
} // Login to the server
Client.Login (Nick, Nick);
// Join the channel
Client.RfcJoin (Channel);
Thread thread = new Thread (
new ThreadStart (delegate {
Client.Listen ();
})
);
thread.Start ();
} catch (Exception e) {
Console.WriteLine ("Could not connect: " + e.Message);
}
}
} }

View file

@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using Meebey.SmartIrc4net;
using Mono.Unix; using Mono.Unix;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -32,10 +33,11 @@ namespace SparkleLib {
private FileSystemWatcher Watcher; private FileSystemWatcher Watcher;
private bool HasChanged; private bool HasChanged;
private DateTime LastChange; private DateTime LastChange;
private System.Object ChangeLock = new System.Object(); private System.Object ChangeLock;
private bool HasUnsyncedChanges; private int FetchRequests;
public string Name; public string Name;
public string RemoteName;
public string Domain; public string Domain;
public string Description; public string Description;
public string LocalPath; public string LocalPath;
@ -45,6 +47,9 @@ namespace SparkleLib {
public string UserName; public string UserName;
public bool IsSyncing; public bool IsSyncing;
public bool IsBuffering; public bool IsBuffering;
public bool IsPolling;
public bool IsFetching;
public bool HasUnsyncedChanges;
public delegate void AddedEventHandler (object o, SparkleEventArgs args); public delegate void AddedEventHandler (object o, SparkleEventArgs args);
public delegate void CommitedEventHandler (object o, SparkleEventArgs args); public delegate void CommitedEventHandler (object o, SparkleEventArgs args);
@ -74,9 +79,6 @@ namespace SparkleLib {
public SparkleRepo (string path) public SparkleRepo (string path)
{ {
SparkleListener listener = new SparkleListener ();
LocalPath = path; LocalPath = path;
Name = Path.GetFileName (LocalPath); Name = Path.GetFileName (LocalPath);
@ -94,15 +96,21 @@ namespace SparkleLib {
RemoteOriginUrl = GetRemoteOriginUrl (); RemoteOriginUrl = GetRemoteOriginUrl ();
CurrentHash = GetCurrentHash (); CurrentHash = GetCurrentHash ();
Domain = GetDomain (RemoteOriginUrl); Domain = GetDomain (RemoteOriginUrl);
RemoteName = Path.GetFileNameWithoutExtension (RemoteOriginUrl);
Description = GetDescription (); Description = GetDescription ();
HasUnsyncedChanges = false; HasUnsyncedChanges = false;
IsSyncing = false; IsSyncing = false;
IsBuffering = false; IsBuffering = false;
IsPolling = true;
IsFetching = false;
if (CurrentHash == null) if (CurrentHash == null)
CreateInitialCommit (); CreateInitialCommit ();
HasChanged = false; HasChanged = false;
ChangeLock = new System.Object ();
FetchRequests = 0;
// Watch the repository's folder // Watch the repository's folder
Watcher = new FileSystemWatcher (LocalPath) { Watcher = new FileSystemWatcher (LocalPath) {
@ -116,20 +124,79 @@ namespace SparkleLib {
Watcher.Deleted += new FileSystemEventHandler (OnFileActivity); Watcher.Deleted += new FileSystemEventHandler (OnFileActivity);
Watcher.Renamed += new RenamedEventHandler (OnFileActivity); Watcher.Renamed += new RenamedEventHandler (OnFileActivity);
// Fetch remote changes every minute // Fetch remote changes every minute
RemoteTimer = new Timer () { RemoteTimer = new Timer () {
Interval = 60000 Interval = 60000
}; };
RemoteTimer.Elapsed += delegate { RemoteTimer.Elapsed += delegate {
CheckForRemoteChanges (); CheckForRemoteChanges ();
if (HasUnsyncedChanges) if (HasUnsyncedChanges)
Push (); Push ();
}; };
// Listen to the irc channel on the server
SparkleListener listener = new SparkleListener (Domain, "#" + RemoteName, UserEmail);
// Keep a Local that checks if there are changes and // Stop polling when the connection to the irc channel is succesful
listener.Client.OnConnected += delegate {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Connected. Now listening...");
RemoteTimer.Stop ();
IsPolling = false;
};
// Start polling when the connection to the irc channel is lost
listener.Client.OnDisconnected += delegate {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Lost connection. Falling back to polling...");
RemoteTimer.Start ();
IsPolling = true;
};
// Fetch changes when there is a message in the irc channel
listener.Client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
SparkleHelpers.DebugInfo ("Irc", "[" + Name + "] Was notified of a remote change.");
if (!args.Data.Message.Equals (CurrentHash)) {
FetchRequests++;
if (!IsFetching) {
while (FetchRequests > 0) {
Fetch ();
FetchRequests--;
}
Rebase ();
}
} else {
SparkleHelpers.DebugInfo ("Irc",
"[" + Name + "] False alarm, already up to date. (" + CurrentHash + ")");
}
};
// Start listening
listener.Listen ();
// Keep a timer that checks if there are changes and
// whether they have settled // whether they have settled
LocalTimer = new Timer () { LocalTimer = new Timer () {
Interval = 4000 Interval = 4000
@ -139,7 +206,10 @@ namespace SparkleLib {
CheckForChanges (); CheckForChanges ();
}; };
RemoteTimer.Start ();
if (IsPolling)
RemoteTimer.Start ();
LocalTimer.Start (); LocalTimer.Start ();
@ -150,8 +220,6 @@ namespace SparkleLib {
if (CurrentHash == null) if (CurrentHash == null)
CurrentHash = GetCurrentHash (); CurrentHash = GetCurrentHash ();
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Idling...");
} }
@ -182,6 +250,7 @@ namespace SparkleLib {
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Remote changes found."); SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Remote changes found.");
Fetch (); Fetch ();
Rebase ();
} }
@ -220,7 +289,7 @@ namespace SparkleLib {
} }
// Starts a timerwhen something changes // Starts a timer when something changes
private void OnFileActivity (object o, FileSystemEventArgs fse_args) private void OnFileActivity (object o, FileSystemEventArgs fse_args)
{ {
@ -288,7 +357,9 @@ namespace SparkleLib {
} finally { } finally {
RemoteTimer.Start (); if (IsPolling)
RemoteTimer.Start ();
LocalTimer.Start (); LocalTimer.Start ();
} }
@ -346,7 +417,8 @@ namespace SparkleLib {
public void Fetch () public void Fetch ()
{ {
IsSyncing = true; IsSyncing = true;
IsFetching = true;
RemoteTimer.Stop (); RemoteTimer.Stop ();
@ -378,14 +450,14 @@ namespace SparkleLib {
args = new SparkleEventArgs ("FetchingFinished"); args = new SparkleEventArgs ("FetchingFinished");
IsSyncing = false; IsSyncing = false;
IsFetching = false;
if (FetchingFinished != null) if (FetchingFinished != null)
FetchingFinished (this, args); FetchingFinished (this, args);
Rebase (); if (IsPolling)
RemoteTimer.Start ();
RemoteTimer.Start ();
CurrentHash = GetCurrentHash (); CurrentHash = GetCurrentHash ();
@ -486,7 +558,6 @@ namespace SparkleLib {
} }
Watcher.EnableRaisingEvents = true; Watcher.EnableRaisingEvents = true;
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Idling...");
} }

View file

@ -439,12 +439,15 @@ namespace SparkleShare {
string repo_name = Path.GetFileName (folder_path); string repo_name = Path.GetFileName (folder_path);
foreach (SparkleRepo repo in Repositories) { for (int i = 0; i < Repositories.Count; i++) {
SparkleRepo repo = Repositories [i];
if (repo.Name.Equals (repo_name)) { if (repo.Name.Equals (repo_name)) {
repo.Stop (); repo.Stop ();
Repositories.Remove (repo); Repositories.Remove (repo);
repo = null;
break; break;
} }