2011-06-18 21:42:34 +00:00
|
|
|
// 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;
|
2011-06-28 21:58:04 +00:00
|
|
|
using System.IO;
|
2011-06-18 21:42:34 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
using System.Security.Cryptography;
|
2011-06-28 21:58:04 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Xml.Serialization;
|
2011-06-18 21:42:34 +00:00
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public class SparkleListenerTcp : SparkleListenerBase {
|
|
|
|
|
|
|
|
private Thread thread;
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
// these are shared
|
|
|
|
private readonly Object mutex = new Object();
|
2011-06-18 21:42:34 +00:00
|
|
|
private Socket socket;
|
2011-06-28 21:13:28 +00:00
|
|
|
private bool connected;
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-06-28 18:36:47 +00:00
|
|
|
public SparkleListenerTcp (Uri server, string folder_identifier) :
|
|
|
|
base (server, folder_identifier)
|
2011-06-18 21:42:34 +00:00
|
|
|
{
|
|
|
|
base.channels.Add (folder_identifier);
|
2011-06-28 21:13:28 +00:00
|
|
|
this.connected = false;
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool IsConnected {
|
|
|
|
get {
|
2011-06-28 21:13:28 +00:00
|
|
|
bool result = false;
|
2011-07-24 18:22:17 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
lock (this.mutex) {
|
|
|
|
result = this.connected;
|
|
|
|
}
|
2011-07-24 18:22:17 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
return result;
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 22:38:32 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
// Starts a new thread and listens to the channel
|
|
|
|
public override void Connect ()
|
|
|
|
{
|
2011-06-28 18:36:47 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Connecting to " + Server.Host);
|
2011-06-18 21:42:34 +00:00
|
|
|
|
|
|
|
base.is_connecting = true;
|
|
|
|
|
|
|
|
this.thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
try {
|
|
|
|
// Connect and subscribe to the channel
|
2011-06-28 18:36:47 +00:00
|
|
|
int port = Server.Port;
|
|
|
|
if (port < 0) port = 9999;
|
2011-07-24 18:22:17 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
lock (this.mutex) {
|
2011-10-25 16:16:29 +00:00
|
|
|
this.socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
this.socket.Connect (Server.Host, port);
|
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
base.is_connecting = false;
|
|
|
|
this.connected = true;
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-07-24 18:22:17 +00:00
|
|
|
OnConnected ();
|
|
|
|
|
2011-07-02 01:13:33 +00:00
|
|
|
foreach (string channel in base.channels) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
|
2011-07-02 01:24:12 +00:00
|
|
|
this.socket.Send (Encoding.UTF8.GetBytes ("subscribe " + channel + "\n"));
|
2011-07-02 01:13:33 +00:00
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte [] bytes = new byte [4096];
|
|
|
|
|
|
|
|
// List to the channels, this blocks the thread
|
|
|
|
while (this.socket.Connected) {
|
2011-06-28 21:13:28 +00:00
|
|
|
int bytes_read = this.socket.Receive (bytes);
|
2011-07-24 18:22:17 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
if (bytes_read > 0) {
|
2011-07-02 01:13:33 +00:00
|
|
|
string received = Encoding.UTF8.GetString (bytes);
|
2011-11-07 15:39:33 +00:00
|
|
|
string line = received.Substring (0, received.IndexOf ("\n"));
|
|
|
|
if (!line.Contains ("!"))
|
|
|
|
continue;
|
|
|
|
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
|
|
|
|
string message = this.CleanMessage (line.Substring (line.IndexOf ("!") + 1));
|
|
|
|
if (!folder_identifier.Equals("debug") &&
|
|
|
|
!String.IsNullOrEmpty(message))
|
2011-11-05 21:09:09 +00:00
|
|
|
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
|
2011-06-28 21:13:28 +00:00
|
|
|
} else {
|
2011-06-28 22:38:32 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Error on socket");
|
2011-07-24 18:22:17 +00:00
|
|
|
|
2011-06-28 21:13:28 +00:00
|
|
|
lock (this.mutex) {
|
2011-07-24 18:22:17 +00:00
|
|
|
this.socket.Close ();
|
2011-06-28 21:13:28 +00:00
|
|
|
this.connected = false;
|
2011-07-24 18:22:17 +00:00
|
|
|
|
|
|
|
OnDisconnected ();
|
2011-06-28 21:13:28 +00:00
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2011-06-28 22:38:32 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Disconnected from " + Server.Host);
|
2011-11-05 21:09:09 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
} catch (SocketException e) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
|
2011-10-03 12:39:26 +00:00
|
|
|
|
|
|
|
OnDisconnected ();
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void AlsoListenTo (string folder_identifier)
|
|
|
|
{
|
|
|
|
string channel = folder_identifier;
|
2011-11-13 00:57:00 +00:00
|
|
|
|
2011-06-18 21:42:34 +00:00
|
|
|
if (!base.channels.Contains (channel)) {
|
|
|
|
base.channels.Add (channel);
|
|
|
|
|
|
|
|
if (IsConnected) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Subscribing to channel " + channel);
|
|
|
|
|
2011-07-02 01:24:12 +00:00
|
|
|
string to_send = "subscribe " + folder_identifier + "\n";
|
2011-07-02 01:13:33 +00:00
|
|
|
|
2011-10-03 12:39:26 +00:00
|
|
|
try {
|
|
|
|
lock (this.mutex) {
|
|
|
|
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
|
|
|
|
}
|
2011-11-13 00:57:00 +00:00
|
|
|
|
2011-10-03 12:39:26 +00:00
|
|
|
} catch (SocketException e) {
|
2011-11-13 00:57:00 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
|
|
|
|
OnDisconnected ();
|
2011-07-02 01:13:33 +00:00
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Announce (SparkleAnnouncement announcement)
|
|
|
|
{
|
2011-07-02 01:24:12 +00:00
|
|
|
string to_send = "announce " + announcement.FolderIdentifier
|
|
|
|
+ " " + announcement.Message + "\n";
|
2011-06-18 21:42:34 +00:00
|
|
|
|
2011-10-03 12:39:26 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
lock (this.mutex) {
|
|
|
|
this.socket.Send (Encoding.UTF8.GetBytes (to_send));
|
|
|
|
}
|
|
|
|
} catch (SocketException e) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerTcp", "Could not connect to " + Server + ": " + e.Message);
|
|
|
|
|
|
|
|
OnDisconnected ();
|
2011-07-02 01:13:33 +00:00
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Dispose ()
|
|
|
|
{
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
|
|
|
base.Dispose ();
|
|
|
|
}
|
2011-11-05 21:09:09 +00:00
|
|
|
|
|
|
|
private string CleanMessage(string message)
|
|
|
|
{
|
|
|
|
return message.Trim ().Replace ("\n", "").Replace ("\0", "");
|
|
|
|
}
|
2011-06-18 21:42:34 +00:00
|
|
|
}
|
|
|
|
}
|