[Muine] [PATCH] InfoServer



Attached patch listens for TCP connections on a user-specified port
and sends a simple XML document containing player state information.
The port number can be changed at runtime via gconf, and you can
enable and disable the server with gconf, too.

I think this is a simple and useful way to get information about Muine
from other applications, at least until DBUS support is added.

Here is some sample output, retrieved using netcat:

$ nc localhost 6588
<player state="paused"><song><filename>/home/sam/Music/Mark Farina -
San Francisco Sessions vol. 1/02 Rob Mello -
Happiness.mp3</filename><title>Happiness</title><artist>Rob
Mello</artist><album>San Francisco Sessions - Volume
1</album><duration>3:46</duration></song><position>0:03</position></player>

I haven't used C# or .NET before, so I must apologize if my code seems
messy.  Hopefully someone will find this useful.

--
Sam Stephenson
sstephenson gmail com
diff -Nru muine-0.6.3/data/muine.schemas.in muine-0.6.3-new/data/muine.schemas.in
--- muine-0.6.3/data/muine.schemas.in	2004-05-09 08:28:44.000000000 -0400
+++ muine-0.6.3-new/data/muine.schemas.in	2004-06-21 11:32:27.000000000 -0400
@@ -167,5 +167,26 @@
 	<long>Which Amazon site to use for fetching album images, "us", "uk", "de", or "jp".</long>
       </locale>
     </schema>
+    <schema>
+      <key>/schemas/apps/muine/info_server/enable</key>
+      <applyto>/apps/muine/info_server/enable</applyto>
+      <owner>muine</owner>
+      <type>bool</type>
+      <default>0</default>
+      <locale name="C">
+        <short>Enable Info Server</short>
+	<long>Whether or not the Info Server should listen on the specified port for incoming TCP connections.</long>
+      </locale>
+    </schema>
+    <schema>
+      <key>/schemas/apps/muine/info_server/port</key>
+      <applyto>/apps/muine/info_server/port</applyto>
+      <owner>muine</owner>
+      <type>int</type>
+      <default>6588</default>
+      <locale name="C">
+        <short>Info Server Port</short>
+	<long>TCP port number on which the Info Server, if enabled, will listen for incoming connections.</long>
+      </locale>
   </schemalist>
 </gconfschemafile>
diff -Nru muine-0.6.3/src/InfoServer.cs muine-0.6.3-new/src/InfoServer.cs
--- muine-0.6.3/src/InfoServer.cs	1969-12-31 19:00:00.000000000 -0500
+++ muine-0.6.3-new/src/InfoServer.cs	2004-06-21 12:13:49.000000000 -0400
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2004 Sam Stephenson <sam conio net>
+ *
+ * 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 2 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using GConf;
+
+using System;
+using System.Net.Sockets;
+using System.Threading;
+using System.Xml;
+
+public class InfoServer
+{
+	private Thread thread;
+	private Player player;
+	
+	public class Settings {
+		public const string KEY_BASE   = "/apps/muine/info_server";
+		public const string KEY_ENABLE = KEY_BASE + "/enable";
+		public const string KEY_PORT   = KEY_BASE + "/port";
+		
+		static GConf.Client client = new GConf.Client ();
+
+		public static event GConf.NotifyEventHandler Changed
+		{
+			add {
+				client.AddNotify (KEY_BASE, value);
+			}
+			remove {
+				client.RemoveNotify (KEY_BASE, value);
+			}
+		}
+
+		public static bool Enable
+		{
+			get {
+				return (bool) client.Get (KEY_ENABLE);
+			}
+			set {
+				client.Set (KEY_ENABLE, value);
+			}
+		}
+
+		public static int Port
+		{
+			get {
+				return (int) client.Get (KEY_PORT);
+			}
+			set {
+				client.Set (KEY_PORT, value);
+			}
+		}
+	}
+
+	private void SettingChanged (object obj, NotifyEventArgs args)
+	{
+		Stop ();
+		Start ();
+	}
+
+	public InfoServer (Player player)
+	{
+		this.player = player;
+		Settings.Changed += new NotifyEventHandler (SettingChanged);
+	}
+
+	public void Start ()
+	{
+		if ((thread != null && thread.IsAlive) || !Settings.Enable) {
+			return;
+		}
+		
+		thread = new Thread (new ThreadStart (Serve));
+		thread.Start ();
+	}
+
+	public void Serve ()
+	{
+		try {
+			TcpListener listener = new TcpListener (Settings.Port);
+
+			try {
+				listener.Start ();
+
+				for (;;) {
+					TcpClient client = listener.AcceptTcpClient ();
+					NetworkStream stream = client.GetStream ();
+					Song song = player.Song;
+
+					XmlDocument doc;
+					XmlElement player_elem, song_elem, elem;
+
+					doc = new XmlDocument ();
+					player_elem = doc.CreateElement ("player");
+					string player_state = player.Playing?
+						"playing" : song == null? "stopped" : "paused";
+					player_elem.SetAttribute ("state", player_state);
+					doc.AppendChild (player_elem);
+
+					if (song != null) {
+						song_elem = doc.CreateElement ("song");
+						player_elem.AppendChild (song_elem);
+
+						elem = doc.CreateElement ("filename");
+						elem.InnerText = song.Filename;
+						song_elem.AppendChild (elem);
+
+						elem = doc.CreateElement ("title");
+						elem.InnerText = song.Title;
+						song_elem.AppendChild (elem);
+
+						foreach (string artist in song.Artists) {
+							elem = doc.CreateElement ("artist");
+							elem.InnerText = artist;
+							song_elem.AppendChild (elem);
+						}
+
+						elem = doc.CreateElement ("album");
+						elem.InnerText = song.Album;
+						song_elem.AppendChild (elem);
+
+						elem = doc.CreateElement ("position");
+						elem.InnerText = StringUtils.SecondsToString (player.Position);
+						player_elem.AppendChild (elem);
+
+						elem = doc.CreateElement ("duration");
+						elem.InnerText = StringUtils.SecondsToString (song.Duration);
+						song_elem.AppendChild (elem);
+					}
+
+					if (stream.CanWrite) {
+						XmlWriter writer = new XmlTextWriter (stream, null);
+						doc.WriteTo (writer);
+						writer.Flush ();
+					}
+
+					client.Close ();
+				}
+			} finally {
+				listener.Stop ();
+			}
+		} catch (ThreadAbortException e) {
+		} catch (Exception e) {
+			Settings.Enable = false;
+		}
+	}
+
+	public void Stop ()
+	{
+		if (thread != null && thread.IsAlive) {
+			thread.Abort ();
+			thread = null;
+		}
+	}
+
+}
diff -Nru muine-0.6.3/src/Makefile.am muine-0.6.3-new/src/Makefile.am
--- muine-0.6.3/src/Makefile.am	2004-06-05 14:05:00.000000000 -0400
+++ muine-0.6.3-new/src/Makefile.am	2004-06-21 11:32:58.000000000 -0400
@@ -32,6 +32,7 @@
 	$(srcdir)/MessageConnection.cs		\
 	$(srcdir)/GnomeProxy.cs			\
 	$(srcdir)/InfoWindow.cs			\
+	$(srcdir)/InfoServer.cs			\
 	$(srcdir)/CoverImage.cs			\
 	$(srcdir)/MmKeys.cs			\
 	$(srcdir)/GettextCatalog.cs		\
diff -Nru muine-0.6.3/src/PlaylistWindow.cs muine-0.6.3-new/src/PlaylistWindow.cs
--- muine-0.6.3/src/PlaylistWindow.cs	2004-06-13 09:55:19.000000000 -0400
+++ muine-0.6.3-new/src/PlaylistWindow.cs	2004-06-21 11:40:43.000000000 -0400
@@ -146,6 +146,10 @@
 		/* Add Dashboard support */
 		PlayerChangedSong += DashboardFrontend.PlayerChangedSong;
 
+		/* Add InfoServer support */
+		InfoServer info_server = new InfoServer (player);
+		info_server.Start ();
+
 		/* set up playlist filename */
 		playlist_filename = Gnome.User.DirGet () + "/muine/playlist.m3u";
 		


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]