add invites tcp listener (untested)

Conflicts:

	SparkleShare/Makefile.am
	SparkleShare/SparkleControllerBase.cs
	SparkleShare/SparkleSetup.cs
	SparkleShare/SparkleShare.csproj
This commit is contained in:
Hylke Bons 2011-12-01 21:16:57 +01:00
parent 23fed0359f
commit 179b9d612c
5 changed files with 162 additions and 39 deletions

View file

@ -24,6 +24,7 @@ SOURCES = \
SparkleEventLogController.cs \
SparkleExtensions.cs \
SparkleOptions.cs \
SparkleInvite.cs \
SparklePlugin.cs \
SparkleSetup.cs \
SparkleSetupController.cs \

View file

@ -38,41 +38,41 @@ namespace SparkleShare {
public string FolderSize;
public readonly string SparklePath = SparkleConfig.DefaultConfig.FoldersPath;
public event OnQuitWhileSyncingEventHandler OnQuitWhileSyncing;
public delegate void OnQuitWhileSyncingEventHandler ();
public event OnQuitWhileSyncingHandler OnQuitWhileSyncing;
public delegate void OnQuitWhileSyncingHandler ();
public event FolderFetchedEventHandler FolderFetched;
public delegate void FolderFetchedEventHandler (string [] warnings);
public event FolderFetchErrorEventHandler FolderFetchError;
public delegate void FolderFetchErrorEventHandler (string remote_url);
public event FolderFetchErrorHandler FolderFetchError;
public delegate void FolderFetchErrorHandler (string remote_url);
public event FolderFetchingEventHandler FolderFetching;
public delegate void FolderFetchingEventHandler (double percentage);
public event FolderFetchingHandler FolderFetching;
public delegate void FolderFetchingHandler (double percentage);
public event FolderListChangedEventHandler FolderListChanged;
public delegate void FolderListChangedEventHandler ();
public event FolderListChangedHandler FolderListChanged;
public delegate void FolderListChangedHandler ();
public event FolderSizeChangedEventHandler FolderSizeChanged;
public delegate void FolderSizeChangedEventHandler (string folder_size);
public event FolderSizeChangedHandler FolderSizeChanged;
public delegate void FolderSizeChangedHandler (string folder_size);
public event AvatarFetchedEventHandler AvatarFetched;
public delegate void AvatarFetchedEventHandler ();
public event AvatarFetchedHandler AvatarFetched;
public delegate void AvatarFetchedHandler ();
public event OnIdleEventHandler OnIdle;
public delegate void OnIdleEventHandler ();
public event OnIdleHandler OnIdle;
public delegate void OnIdleHandler ();
public event OnSyncingEventHandler OnSyncing;
public delegate void OnSyncingEventHandler ();
public event OnSyncingHandler OnSyncing;
public delegate void OnSyncingHandler ();
public event OnErrorEventHandler OnError;
public delegate void OnErrorEventHandler ();
public event OnErrorHandler OnError;
public delegate void OnErrorHandler ();
public event OnInvitationEventHandler OnInvitation;
public delegate void OnInvitationEventHandler (string server, string folder, string token);
public event OnInviteHandler OnInvite;
public delegate void OnInviteHandler (SparkleInvite invite);
public event ConflictNotificationRaisedEventHandler ConflictNotificationRaised;
public delegate void ConflictNotificationRaisedEventHandler ();
public event ConflictNotificationRaisedHandler ConflictNotificationRaised;
public delegate void ConflictNotificationRaisedHandler ();
public event NotificationRaisedEventHandler NotificationRaised;
public delegate void NotificationRaisedEventHandler (SparkleChangeSet change_set);
@ -136,24 +136,15 @@ namespace SparkleShare {
};
watcher.Created += delegate (object o, FileSystemEventArgs args) {
SparkleInviteListener invite_listener = new SparkleInviteListener (1986);
// Handle invitations when the user saves an
// invitation into the SparkleShare folder
if (args.Name.EndsWith (".sparkle") && !FirstRun) {
XmlDocument xml_doc = new XmlDocument ();
xml_doc.Load (args.Name);
string server = xml_doc.GetElementsByTagName ("server") [0].InnerText;
string folder = xml_doc.GetElementsByTagName ("folder") [0].InnerText;
string token = xml_doc.GetElementsByTagName ("token") [0].InnerText;
// FIXME: this is broken :\
if (OnInvitation != null)
OnInvitation (server, folder, token);
}
invite_listener.InviteReceived += delegate (SparkleInvite invite) {
if (OnInvite != null && !FirstRun)
OnInvite (invite);
};
invite_listener.Start ();
new Thread (new ThreadStart (PopulateRepositories)).Start ();
}

View file

@ -0,0 +1,132 @@
// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons (hylkebons@gmail.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see (http://www.gnu.org/licenses/).
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SparkleShare {
public class SparkleInvite {
public readonly Uri FullAddress;
public readonly string Token;
public string Host {
get {
return FullAddress.Host;
}
}
public string Path {
get {
return FullAddress.AbsolutePath;
}
}
public SparkleInvite (string host, string path, string token)
{
FullAddress = new Uri (host + "/" + path);
Token = token;
}
}
public class SparkleInviteListener {
public event InviteReceivedHandler InviteReceived;
public delegate void InviteReceivedHandler (SparkleInvite invite);
private Thread thread;
private TcpListener tcp_listener;
private int port;
public SparkleInviteListener (int port)
{
this.port = port;
this.tcp_listener = new TcpListener (IPAddress.Any, port);
this.thread = new Thread(new ThreadStart (Listen));
}
public void Start ()
{
this.thread.Start ();
}
private void Listen ()
{
this.tcp_listener.Start ();
while (true)
{
// Blocks until a client connects
TcpClient client = this.tcp_listener.AcceptTcpClient ();
// Create a thread to handle communications
Thread client_thread = new Thread (HandleClient);
client_thread.Start (client);
}
}
private void HandleClient (object client)
{
TcpClient tcp_client = (TcpClient) client;
NetworkStream client_stream = tcp_client.GetStream ();
byte [] message = new byte[4096];
int bytes_read;
while (true)
{
bytes_read = 0;
try {
// Blocks until the client sends a message
bytes_read = client_stream.Read (message, 0, 4096);
} catch {
Console.WriteLine ("Socket error...");
}
// The client has disconnected
if (bytes_read == 0)
break;
ASCIIEncoding encoding = new ASCIIEncoding ();
string received_message = encoding.GetString (message, 0, bytes_read);
// SparkleShare's protocol format looks like this:
// sparkle://user@host.org/path/token
if (InviteReceived != null)
InviteReceived (new SparkleInvite ("", "", ""));
}
tcp_client.Close();
}
}
}

View file

@ -214,7 +214,6 @@ namespace SparkleShare {
// Update the address field text when the selection changes
tree.CursorChanged += delegate (object sender, EventArgs e) {
Controller.SelectedPluginChanged (tree.SelectedRow);
// TODO: Scroll to selected row when using arrow keys
};

View file

@ -9,7 +9,6 @@
<AssemblyName>SparkleShare</AssemblyName>
<SchemaVersion>2.0</SchemaVersion>
<RootNamespace>SparkleShare</RootNamespace>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -78,5 +77,6 @@
<Compile Include="SparkleControllerBase.cs" />
<Compile Include="SparklePlugin.cs" />
<Compile Include="SparkleOptions.cs" />
<Compile Include="SparkleInvite.cs" />
</ItemGroup>
</Project>