From: <sam...@us...> - 2007-02-20 13:40:55
|
Revision: 20 http://svn.sourceforge.net/mp-webinterface/?rev=20&view=rev Author: samuel337 Date: 2007-02-20 05:40:41 -0800 (Tue, 20 Feb 2007) Log Message: ----------- Added client discovery code to ECP2Assembly; removed and recoded bits in preparation for client/server configuration. Modified Paths: -------------- trunk/source/ECP2Assembly/ECP2Assembly.csproj trunk/source/ECP2Assembly/clsMPHandler.cs trunk/source/ECP2Plugin/ECP2Plugin.csproj trunk/source/ECP2Plugin/clsMain.cs trunk/source/ECP2Plugin/frmSetup.cs Added Paths: ----------- trunk/source/ECP2Assembly/clsClientDiscovery.cs trunk/source/ECP2Assembly/clsUdpCommunication.cs trunk/source/ECP2Assembly/clsUdpMCHelper.cs Modified: trunk/source/ECP2Assembly/ECP2Assembly.csproj =================================================================== --- trunk/source/ECP2Assembly/ECP2Assembly.csproj 2006-09-09 17:41:13 UTC (rev 19) +++ trunk/source/ECP2Assembly/ECP2Assembly.csproj 2007-02-20 13:40:41 UTC (rev 20) @@ -38,6 +38,7 @@ </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> + <Reference Include="System.Drawing" /> <Reference Include="System.Runtime.Remoting" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> @@ -45,15 +46,23 @@ <SpecificVersion>False</SpecificVersion> <HintPath>..\..\..\..\MediaPortal Source\mediaportal\TVCapture\bin\Debug\TVCapture.dll</HintPath> </Reference> + <Reference Include="Utils, Version=1.0.2563.41060, Culture=neutral, processorArchitecture=x86"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\..\..\MediaPortal Source\mediaportal\Utils\bin\Debug\Utils.dll</HintPath> + </Reference> <Reference Include="WindowPlugins, Version=1.0.2231.40336, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\..\..\..\MediaPortal Source\mediaportal\WindowPlugins\bin\Debug\WindowPlugins.dll</HintPath> </Reference> </ItemGroup> <ItemGroup> - <Compile Include="AssemblyResolve.cs" /> - <Compile Include="AssemblySettings.cs" /> + <None Include="AssemblyResolve.cs" /> + <None Include="AssemblySettings.cs" /> + <Compile Include="clsClientDiscovery.cs" /> + <Compile Include="clsCustomMPObjects.cs" /> + <Compile Include="clsUdpCommunication.cs" /> <Compile Include="clsMPHandler.cs" /> + <Compile Include="clsUdpMCHelper.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> Added: trunk/source/ECP2Assembly/clsClientDiscovery.cs =================================================================== --- trunk/source/ECP2Assembly/clsClientDiscovery.cs (rev 0) +++ trunk/source/ECP2Assembly/clsClientDiscovery.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ECP2Assembly +{ + class ClientDiscovery : IDisposable + { + public List<string> ClientList; + private UdpMCHelper udpComm; + + private System.Net.IPAddress GroupIP = System.Net.IPAddress.Parse("225.2.100.1"); + private int GroupUdpPort = 3333; + private int GroupTTL = 2; + + public ClientDiscovery() + { + } + + public ClientDiscovery(System.Net.IPAddress groupIP, int groupUdpPort, int groupTTL) + { + GroupIP = groupIP; + GroupUdpPort = groupUdpPort; + GroupTTL = groupTTL; + } + + public void IssueClientPing() + { + if (udpComm != null) + { + udpComm = new UdpMCHelper(GroupIP, GroupTTL); + udpComm.Start(GroupUdpPort); + + udpComm.ReceiveEvent += new UdpMCHelper.ReceiveEventHandler(UdpComm_ReceiveEvent); + } + + ClientList.Clear(); + + udpComm.Send(GroupUdpPort, UdpCommunication.MessageTypes.PING_REQUEST, "", DateTime.Now); + } + + private void UdpComm_ReceiveEvent(UdpMCHelper.ReceiveEventArgs e) + { + if (e.Type == UdpCommunication.MessageTypes.PING_RESPONSE) + { + lock (ClientList) + { + ClientList.Add(e.HostName); + } + } + } + + public void StopWaitingForResponses() + { + if (udpComm != null) + { + udpComm.Stop(); + udpComm = null; + } + } + + #region IDisposable Members + + void IDisposable.Dispose() + { + if (udpComm != null) + { + udpComm.Stop(); + udpComm = null; + } + } + + #endregion + } +} Modified: trunk/source/ECP2Assembly/clsMPHandler.cs =================================================================== --- trunk/source/ECP2Assembly/clsMPHandler.cs 2006-09-09 17:41:13 UTC (rev 19) +++ trunk/source/ECP2Assembly/clsMPHandler.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -35,55 +35,58 @@ #endregion #region Global variables, settings and instance code - static string sMPComputerName; - static string sConfigFileName; - static bool ResolveEventAttached = false; + //CLEANUP: remove commented code below when satisfied with changes + // AssemblySettings & AssemblyResolve already disabled, remove when done - public static string ConfigFileName - { - get - { - if (sConfigFileName==null) - { - sConfigFileName = AssemblySettings.GetDefaultConfigPath(); - } + //static string sMPComputerName; + //static string sConfigFileName; + //static bool ResolveEventAttached = false; - return sConfigFileName; - } - set - { - sConfigFileName = value; - } - } + //public static string ConfigFileName + //{ + // get + // { + // if (sConfigFileName==null) + // { + // sConfigFileName = AssemblySettings.GetDefaultConfigPath(); + // } - public static string MPComputerName - { - get - { - if (sMPComputerName==null) - { - AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); - sMPComputerName = AssemblyConfig.GetSetting("MPComputerAddress"); - } + // return sConfigFileName; + // } + // set + // { + // sConfigFileName = value; + // } + //} - return sMPComputerName; - } - set - { - //prevent unnecessary writing to file and also fixes web service errors - if (sMPComputerName!=value) - { - sMPComputerName = value; - AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); - AssemblyConfig.SaveSetting("MPComputerAddress", sMPComputerName ); - } - } - } + //public static string MPComputerName + //{ + // get + // { + // if (sMPComputerName==null) + // { + // AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); + // sMPComputerName = AssemblyConfig.GetSetting("MPComputerAddress"); + // } + // return sMPComputerName; + // } + // set + // { + // //prevent unnecessary writing to file and also fixes web service errors + // if (sMPComputerName!=value) + // { + // sMPComputerName = value; + // AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); + // AssemblyConfig.SaveSetting("MPComputerAddress", sMPComputerName ); + // } + // } + //} + public static void WriteLog(string msg) { string logFileName; - logFileName = ConfigFileName + ".log"; + logFileName = System.Reflection.Assembly.GetExecutingAssembly().FullName + ".log"; System.IO.FileStream Wfs = new System.IO.FileStream(logFileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); System.IO.StreamWriter w = new System.IO.StreamWriter(Wfs); @@ -94,49 +97,62 @@ Wfs.Close(); } - public static MPHandler Instance - { - get - { - if (ResolveEventAttached == false) - { - AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ECP2Assembly.AssemblyResolve.MyResolveEventHandler); - ResolveEventAttached = true; - } + //public static MPHandler Instance + //{ + // get + // { + // if (ResolveEventAttached == false) + // { + // AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ECP2Assembly.AssemblyResolve.MyResolveEventHandler); + // ResolveEventAttached = true; + // } - try - { - System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(),false); - } - catch - {} + // try + // { + // System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(),false); + // } + // catch + // {} - AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); + // AssemblySettings AssemblyConfig = new AssemblySettings(ConfigFileName); - string AssemblyDir = AssemblyConfig.GetSetting("MPDirectory"); + // string AssemblyDir = AssemblyConfig.GetSetting("MPDirectory"); - //see if can connect to MPLaunchInfo to get MP path, if not, the return nothing and exit sub - try - { - ECP2Assembly.MPLaunchInfo eMPLI; - eMPLI = (MPLaunchInfo)Activator.GetObject(typeof(MPLaunchInfo), "tcp://" + MPComputerName + ":3334/MPLaunchInfo"); + // //see if can connect to MPLaunchInfo to get MP path + // try + // { + // ECP2Assembly.MPLaunchInfo eMPLI; + // eMPLI = (MPLaunchInfo)Activator.GetObject(typeof(MPLaunchInfo), "tcp://" + MPComputerName + ":3334/MPLaunchInfo"); - //save the path if it doesn't match the currently saved path - if (AssemblyDir!=eMPLI.MediaPortalDir) - { - AssemblyDir = eMPLI.MediaPortalDir; - if (System.IO.Directory.Exists(AssemblyDir) == true) - { - AssemblyConfig.SaveSetting("MPDirectory",AssemblyDir); - } - } - } - catch - {} + // //save the path if it doesn't match the currently saved path + // if (AssemblyDir!=eMPLI.MediaPortalDir) + // { + // AssemblyDir = eMPLI.MediaPortalDir; + // if (System.IO.Directory.Exists(AssemblyDir) == true) + // { + // AssemblyConfig.SaveSetting("MPDirectory",AssemblyDir); + // } + // } + // } + // catch + // {} - //otherwise, return an instance of the remoted assembly - return (MPHandler)Activator.GetObject(typeof(MPHandler), "tcp://" + MPComputerName + ":3334/MPHandler"); + // //otherwise, return an instance of the remoted assembly + // return (MPHandler)Activator.GetObject(typeof(MPHandler), "tcp://" + MPComputerName + ":3334/MPHandler"); + // } + //} + + public static MPHandler Instance(string clientAddress) + { + try + { + System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(), false); } + catch + { } + + //return an instance of the remoted assembly + return (MPHandler)Activator.GetObject(typeof(MPHandler), "tcp://" + clientAddress + ":3334/MPHandler"); } //variable to hold BurnerThread for transcoding @@ -150,7 +166,7 @@ MediaPortal.GUI.Library.Action act = new MediaPortal.GUI.Library.Action(); act.wID = (MediaPortal.GUI.Library.Action.ActionType)aID; - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2: SendActionByID actionID {0}", act.wID.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2: SendActionByID actionID {0}", act.wID.ToString()); //check that action is valid in the current window if (MediaPortal.GUI.Library.ActionTranslator.GetActionDetail(MediaPortal.GUI.Library.GUIWindowManager.ActiveWindow, act) == false) @@ -174,7 +190,7 @@ msg.Message = (MediaPortal.GUI.Library.GUIMessage.MessageType)mID; msg.Label = label; - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2: SendMessageByID messageID {0}, Label {1}", msg.Message.ToString(), msg.Label); + MediaPortal.GUI.Library.Log.Debug("ECP2: SendMessageByID messageID {0}, Label {1}", msg.Message.ToString(), msg.Label); //send message MediaPortal.GUI.Library.GUIWindowManager.SendMessage(msg); @@ -188,7 +204,7 @@ msg.Message = (MediaPortal.GUI.Library.GUIMessage.MessageType)Enum.Parse(typeof(MediaPortal.GUI.Library.GUIMessage.MessageType), mEnum); msg.Label = label; - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2: SendThreadMessageByID messageID {0}, Label {1}", msg.Message.ToString(), msg.Label); + MediaPortal.GUI.Library.Log.Debug("ECP2: SendThreadMessageByID messageID {0}, Label {1}", msg.Message.ToString(), msg.Label); //send message MediaPortal.GUI.Library.GUIWindowManager.SendThreadMessage(msg); @@ -275,19 +291,19 @@ if (methodInfo==null) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2Assembly - GetRecorderProps: Method/Property, {0}, not found",PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetRecorderProps: Method/Property, {0}, not found", PropName); return null; } if (methodInfo.ReturnType == System.Type.GetType("System.Void")) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2Assembly - GetRecorderProps: Method {0} does not return anything - not a property; ", PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetRecorderProps: Method {0} does not return anything - not a property; ", PropName); return null; } if (methodInfo.GetParameters().Length > 0) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2Assembly - GetRecorderProps: Method {0} has parameters, thus cannot be accessed.", PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetRecorderProps: Method {0} has parameters, thus cannot be accessed.", PropName); throw new Exception("ECP2Assembly - GetRecorderProps; This method cannot be accessed because it has parameters"); } @@ -297,7 +313,7 @@ { if (propInfo.GetIndexParameters().Length > 0) { - MediaPortal.GUI.Library.Log.Write("ECP2Assembly - GetRecorderProps: Property {0} has parameters so it cannot be accessed.", PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetRecorderProps: Property {0} has parameters so it cannot be accessed.", PropName); throw new Exception("ECP2Assembly - GetRecorderProps; This property cannot be accessed because it has parameters."); } @@ -306,6 +322,7 @@ } catch (Exception ex) { + MediaPortal.GUI.Library.Log.Error("ECP2Assembly - GetRecorderProps exception: {0}", ex.ToString()); throw new Exception("ECP2Assembly - GetRecorderProps exception: " + ex.ToString()); } } @@ -365,14 +382,14 @@ //if not found, exit if (propInfo==null) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2Assembly - GetgPlayerProps: No property found with the name {0}", PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetgPlayerProps: No property found with the name {0}", PropName); return null; } //exit if it contains parameters if (propInfo.GetIndexParameters().Length>0) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2Assembly - GetgPlayerProps: Properties with parameters cannot be accessed; {0}", PropName); + MediaPortal.GUI.Library.Log.Warn("ECP2Assembly - GetgPlayerProps: Properties with parameters cannot be accessed; {0}", PropName); throw new Exception("ECP2Assembly - GetgPlayerProps; This property cannot be accessed because it has parameters"); } @@ -380,6 +397,7 @@ } catch (Exception ex) { + MediaPortal.GUI.Library.Log.Error("ECP2Assembly - GetgPlayerProps exception: {0}", ex.ToString()); throw new Exception("ECP2Assembly - GetgPlayerProps exception: " + ex.ToString()); } } @@ -470,7 +488,7 @@ } finally { - MediaPortal.GUI.Library.Log.Write("ECP2Assembly - RefreshRecordings executed."); + MediaPortal.GUI.Library.Log.Debug("ECP2Assembly - RefreshRecordings executed."); } } @@ -487,12 +505,12 @@ try { MediaPortal.TV.Database.TVDatabase.AddRecording(ref rec); - MediaPortal.GUI.Library.Log.Write("ECP2 - AddRecording: recording ({0}, {1}, {2}) added.", rec.Title, rec.Channel, rec.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - AddRecording: recording ({0}, {1}, {2}) added.", rec.Title, rec.Channel, rec.StartTime.ToString()); return true; } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - AddRecording: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - AddRecording: " + ex.ToString()); return false; } } @@ -502,12 +520,12 @@ try { MediaPortal.TV.Database.TVDatabase.UpdateRecording(rec, changeReason); - MediaPortal.GUI.Library.Log.Write("ECP2 - UpdateRecording: recording ({0}, {1}, {2}) updated.", rec.Title, rec.Channel, rec.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - UpdateRecording: recording ({0}, {1}, {2}) updated.", rec.Title, rec.Channel, rec.StartTime.ToString()); return true; } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - UpdateRecording: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - UpdateRecording: " + ex.ToString()); return false; } } @@ -536,13 +554,13 @@ if (ScheduleToDel ==true) { MediaPortal.TV.Database.TVDatabase.RemoveRecording(rec); - MediaPortal.GUI.Library.Log.Write("ECP2 - DelRecording: recording ({0}, {1}, {2}) deleted.", rec.Title, rec.Channel, rec.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - DelRecording: recording ({0}, {1}, {2}) deleted.", rec.Title, rec.Channel, rec.StartTime.ToString()); return true; } } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2 - DelRecording: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - DelRecording: " + ex.ToString()); } return false; @@ -573,14 +591,14 @@ if (ScheduleToDel==true) { MediaPortal.TV.Database.TVDatabase.RemoveRecording(rec); - MediaPortal.GUI.Library.Log.Write("ECP2 - DelRecording: recording ({0}, {1}, {2}) deleted.", rec.Title, rec.Channel, rec.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - DelRecording: recording ({0}, {1}, {2}) deleted.", rec.Title, rec.Channel, rec.StartTime.ToString()); return true; } } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2 - DelRecording: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - DelRecording: " + ex.ToString()); } return false; @@ -617,7 +635,7 @@ } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log,"ECP2 - DelRecording: exception - " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - DelRecording: exception - " + ex.ToString()); } return false; @@ -769,12 +787,12 @@ } MediaPortal.TV.Database.TVDatabase.AddNotify(notify); - MediaPortal.GUI.Library.Log.Write("ECP2 - AddTVNotify: TVNotify ({0}, {1}) added.",notify.Program.Title,notify.Program.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - AddTVNotify: TVNotify ({0}, {1}) added.",notify.Program.Title,notify.Program.StartTime.ToString()); return true; } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - AddTVNotify: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - AddTVNotify: " + ex.ToString()); return false; } } @@ -791,12 +809,12 @@ } MediaPortal.TV.Database.TVDatabase.DeleteNotify(notify); - MediaPortal.GUI.Library.Log.Write("ECP2 - DeleteTVNotify: TVNotify ({0}, {1}) deleted.", notify.Program.Title, notify.Program.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - DeleteTVNotify: TVNotify ({0}, {1}) deleted.", notify.Program.Title, notify.Program.StartTime.ToString()); return true; } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - DeleteTVNotify: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - DeleteTVNotify: " + ex.ToString()); return false; } } @@ -813,12 +831,12 @@ } MediaPortal.TV.Database.TVDatabase.DeleteNotify(notify); - MediaPortal.GUI.Library.Log.Write("ECP2 - DeleteTVNotify: TVNotify ({0}, {1}) deleted.", notify.Program.Title, notify.Program.StartTime.ToString()); + MediaPortal.GUI.Library.Log.Debug("ECP2 - DeleteTVNotify: TVNotify ({0}, {1}) deleted.", notify.Program.Title, notify.Program.StartTime.ToString()); return true; } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - DeleteTVNotify: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - DeleteTVNotify: " + ex.ToString()); return false; } } @@ -840,7 +858,7 @@ } catch (Exception ex) { - MediaPortal.GUI.Library.Log.Write("ECP2 - GetNotify: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - GetNotify: " + ex.ToString()); return null; } } @@ -875,7 +893,7 @@ } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2 - GetTVNotify: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - GetTVNotify: " + ex.ToString()); return null; } } @@ -926,7 +944,7 @@ } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2 - GetTVGroup: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - GetTVGroup: " + ex.ToString()); return null; } } @@ -961,7 +979,7 @@ } catch (Exception ex) { - MediaPortal.GUI.Library.Log.WriteFile(Log.LogType.Log, "ECP2 - GetTVGroup: " + ex.ToString()); + MediaPortal.GUI.Library.Log.Error("ECP2 - GetTVGroup: " + ex.ToString()); return null; } } Added: trunk/source/ECP2Assembly/clsUdpCommunication.cs =================================================================== --- trunk/source/ECP2Assembly/clsUdpCommunication.cs (rev 0) +++ trunk/source/ECP2Assembly/clsUdpCommunication.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ECP2Assembly +{ + public class UdpCommunication + { + public static UdpMCHelper GetDefaultUdpMCHelper() + { + System.Net.IPAddress GroupIP = System.Net.IPAddress.Parse("225.2.100.1"); + int GroupUdpPort = 3333; + int GroupTTL = 2; + + UdpMCHelper udpComm; + udpComm = new UdpMCHelper(GroupIP, GroupTTL); + udpComm.Start(GroupUdpPort); + + return udpComm; + } + + //class to hold message type values + public class MessageTypes + { + public static string PLUGIN_START + { + get { return "PLUGIN_START"; } + } + + public static string PLUGIN_STOP + { + get { return "PLUGIN_STOP"; } + } + + public static string PING_REQUEST + { + get { return "PING_REQUEST"; } + } + + public static string PING_RESPONSE + { + get { return "PING_RESPONSE"; } + } + } + } +} Added: trunk/source/ECP2Assembly/clsUdpMCHelper.cs =================================================================== --- trunk/source/ECP2Assembly/clsUdpMCHelper.cs (rev 0) +++ trunk/source/ECP2Assembly/clsUdpMCHelper.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -0,0 +1,279 @@ +//UDP unicast code taken from MP; modified to multicast and redirected calls to use UdpClient. + +#region Copyright (C) 2005-2007 Team MediaPortal + +/* + * Copyright (C) 2005-2007 Team MediaPortal + * http://www.team-mediaportal.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 2, 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 GNU Make; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#endregion + +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Net.NetworkInformation; +using System.Text; + +namespace ECP2Assembly +{ + public class UdpMCHelper : IDisposable + { + UdpClient udpClient; + + IPAddress groupIP = null; + int TTL = 2; + + #region Logging + public delegate void LogEventHandler(LogEventArgs e); + public event LogEventHandler LogEvent; + + public class LogEventArgs : EventArgs + { + string m_LogLevel; + string m_LogMessage; + + public LogEventArgs() + { + } + + public LogEventArgs(string logLevel, string logMessage) + { + LogLevel = logLevel; + LogMessage = logMessage; + } + + public string LogLevel + { + get { return m_LogLevel; } + set { m_LogLevel = value; } + } + + public string LogMessage + { + get { return m_LogMessage; } + set { m_LogMessage = value; } + } + } + + private void Log(string logLabel, string logMessageFormat, params string[] logMessageParams) + { + if (LogEvent == null) return; + + string logMessage; + logMessage = string.Format(logMessageFormat, logMessageParams); + + LogEventArgs e = new LogEventArgs(logLabel, logMessage); + LogEvent(e); + } + + #endregion + + #region ReceiveEvent + public delegate void ReceiveEventHandler(ReceiveEventArgs e); + public event ReceiveEventHandler ReceiveEvent; + + public class ReceiveEventArgs : EventArgs + { + string m_Type; + string m_Message; + string m_HostName; + string m_Timestamp; + + public ReceiveEventArgs() + { + } + + public ReceiveEventArgs(string type, string message, string hostName, string timestamp) + { + Type = type; + Message = message; + HostName = hostName; + Timestamp = timestamp; + } + + public string Type + { + get { return m_Type; } + set { m_Type = value; } + } + + public string Message + { + get { return m_Message; } + set { m_Message = value; } + } + + public string HostName + { + get { return m_HostName; } + set { m_HostName = value; } + } + + public string Timestamp + { + get { return m_Timestamp; } + set { m_Timestamp = value; } + } + + } + + protected virtual void OnReceive(string strReceive) + { + //check to see if entire message is received + if (!strReceive.EndsWith("~")) + { + //if not, ignore message + return; + } + + if (ReceiveEvent != null) + { + ReceiveEventArgs e = new ReceiveEventArgs(); + + //strip off check character + strReceive = strReceive.Remove(strReceive.Length - 2); + + string[] strReceiveArray = strReceive.Split("|".ToCharArray()); + + if (strReceiveArray.Length < 4) { return; } + + e.Type = strReceiveArray[0]; + e.Message = strReceiveArray[1]; + e.HostName = strReceiveArray[2]; + e.Timestamp = strReceiveArray[3]; + + ReceiveEvent(e); + } + } + #endregion + + class UdpState + { + public IPEndPoint EndPoint; + public UdpClient UdpClient; + } + + public UdpMCHelper(IPAddress groupAddress) + { + groupIP = groupAddress; + } + + public UdpMCHelper(IPAddress groupAddress, int timeToLive) + { + groupIP = groupAddress; + TTL = timeToLive; + } + + public bool Start(int udpPort) + { + try + { + Log("Info","UDPHelper: Starting listener on port {0}", udpPort.ToString()); + + // Port already used? + IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); + TcpConnectionInformation[] connections = properties.GetActiveTcpConnections(); + foreach (TcpConnectionInformation c in connections) + { + if (c.RemoteEndPoint.Port == udpPort) + { + Log("Error", "UDPHelper: UDP port {0} is already in use", udpPort.ToString()); + return false; + } + } + + IPEndPoint endPoint = new IPEndPoint(groupIP, udpPort); + udpClient = new UdpClient(udpPort, AddressFamily.InterNetwork); + udpClient.JoinMulticastGroup(groupIP, TTL); + + UdpState state = new UdpState(); + state.EndPoint = endPoint; + state.UdpClient = udpClient; + udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), state); + Log("Info","UDPHelper: Listening for messages on port {0}", udpPort.ToString()); + return true; + } + catch (SocketException se) + { + Log("Error","UDPHelper: Start port {0}: {1} - {2}", udpPort.ToString(), se.ErrorCode.ToString(), se.Message); + return false; + } + } + + public void Stop() + { + try + { + udpClient.DropMulticastGroup(groupIP); + udpClient.Close(); + } + catch (System.NullReferenceException) + { } + udpClient = null; + } + + public bool Send(int udpPort, string strType, string strSend, DateTime timeStamp) + { + try + { + byte[] sendbuf = Encoding.UTF8.GetBytes(string.Format("{0}|{1}|{2}|{3}~", strType, strSend, System.Net.Dns.GetHostName(), timeStamp.ToBinary())); + + IPEndPoint endPoint = new IPEndPoint(groupIP, udpPort); + udpClient.Send(sendbuf, sendbuf.Length, endPoint); + return true; + } + catch (SocketException se) + { + Log("Error","UDPHelper: Send port {0}: {1} - {2}", udpPort.ToString(), se.ErrorCode.ToString(), se.Message); + return false; + } + } + + public void ReceiveCallback(IAsyncResult ar) + { + UdpClient udpClientLoc = (UdpClient)((UdpState)(ar.AsyncState)).UdpClient; + IPEndPoint endPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).EndPoint; + + try + { + Byte[] bytesReceived = udpClientLoc.EndReceive(ar, ref endPoint); + string strReceived = Encoding.UTF8.GetString(bytesReceived); + OnReceive(strReceived); + udpClientLoc.BeginReceive(new AsyncCallback(ReceiveCallback), (UdpState)(ar.AsyncState)); + } + catch (System.ObjectDisposedException) + { + } + catch (SocketException) + { + } + } + + #region IDisposable Members + + void IDisposable.Dispose() + { + Stop(); + } + + #endregion +} +} Modified: trunk/source/ECP2Plugin/ECP2Plugin.csproj =================================================================== --- trunk/source/ECP2Plugin/ECP2Plugin.csproj 2006-09-09 17:41:13 UTC (rev 19) +++ trunk/source/ECP2Plugin/ECP2Plugin.csproj 2007-02-20 13:40:41 UTC (rev 20) @@ -38,6 +38,10 @@ <Reference Include="System.Runtime.Remoting" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> + <Reference Include="Utils, Version=1.0.2423.4606, Culture=neutral, processorArchitecture=x86"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\..\..\..\MediaPortal Source\mediaportal\Utils\bin\Debug\Utils.dll</HintPath> + </Reference> </ItemGroup> <ItemGroup> <Compile Include="clsCustomSink.cs" /> Modified: trunk/source/ECP2Plugin/clsMain.cs =================================================================== --- trunk/source/ECP2Plugin/clsMain.cs 2006-09-09 17:41:13 UTC (rev 19) +++ trunk/source/ECP2Plugin/clsMain.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using ECP2Assembly; namespace ECP2Plugin { @@ -13,12 +14,17 @@ //track activity and disallows shutdown depending on time lapsed private static DateTime lastActivity; + private UdpMCHelper UdpComm; + private int UdpCommPort; + #region IPlugin Members void MediaPortal.GUI.Library.IPlugin.Start() { //define HTTPchannel and setup remoting - System.Runtime.Remoting.RemotingConfiguration.Configure("MediaPortal.exe.config", false); + //removed config file parameter as it was causing issues (not needed anyway) + //System.Runtime.Remoting.RemotingConfiguration.Configure("MediaPortal.exe.config", false); + System.Runtime.Remoting.RemotingConfiguration.Configure(null, false); //turn to RemoteOnly for production version! System.Runtime.Remoting.RemotingConfiguration.CustomErrorsMode = System.Runtime.Remoting.CustomErrorsModes.Off; System.Runtime.Remoting.Channels.Tcp.TcpServerChannel channel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel("MediaPortal ECP2", 3334, new ServerActivitySinkProvider()); @@ -27,17 +33,37 @@ System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(ECP2Assembly.MPLaunchInfo), "MPLaunchInfo", System.Runtime.Remoting.WellKnownObjectMode.SingleCall); //get settings - MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings("mediaportal.xml"); + MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings(MediaPortal.Configuration.Config.GetFile(MediaPortal.Configuration.Config.Dir.Config, "MediaPortal.xml")); shutdownDelay = MPSettings.GetValueAsInt("ECP2", "ShutdownDelay", 10); ResetLastActivityTime(); - MediaPortal.GUI.Library.Log.Write("ECP2: Remoting initialised."); + MediaPortal.GUI.Library.Log.Info("ECP2: Remoting initialised."); + + //load UDP communication + System.Net.IPAddress GroupAddress = System.Net.IPAddress.Parse(MPSettings.GetValueAsString("ECP2", "GroupAddress", "225.2.100.1")); + UdpComm = new UdpMCHelper(GroupAddress, MPSettings.GetValueAsInt("ECP2", "GroupTTL", 2)); + + UdpComm.LogEvent += new UdpMCHelper.LogEventHandler(UdpComm_LogEvent); + + UdpCommPort = MPSettings.GetValueAsInt("ECP2", "GroupPort", 3333); + UdpComm.Start(UdpCommPort); + + UdpComm.ReceiveEvent += new UdpMCHelper.ReceiveEventHandler(UdpComm_ReceiveEvent); + + //send startup message + UdpComm.Send(UdpCommPort, UdpCommunication.MessageTypes.PLUGIN_START, "", DateTime.Now); + } void MediaPortal.GUI.Library.IPlugin.Stop() { - + //send shutdown message + UdpComm.Send(UdpCommPort, UdpCommunication.MessageTypes.PLUGIN_STOP, "", DateTime.Now); + + //stop udpcomm + UdpComm.Stop(); + UdpComm = null; } #endregion @@ -126,6 +152,34 @@ { //MediaPortal.GUI.Library.Log.Write("ECP2: LastActivityTimer resetted."); lastActivity = DateTime.Now; - } + } + + #region UdpComm + private void UdpComm_LogEvent(UdpMCHelper.LogEventArgs e) + { + switch (e.LogLevel) + { + case "Error": + MediaPortal.GUI.Library.Log.Error(e.LogMessage); + break; + + default: + MediaPortal.GUI.Library.Log.Info(e.LogMessage); + break; + } + } + + private void UdpComm_ReceiveEvent(UdpMCHelper.ReceiveEventArgs e) + { + //respond to PING requests + if (e.Type == UdpCommunication.MessageTypes.PING_REQUEST) + { + lock (UdpComm) + { + UdpComm.Send(UdpCommPort, UdpCommunication.MessageTypes.PING_RESPONSE, "", DateTime.Now); + } + } + } + #endregion } } Modified: trunk/source/ECP2Plugin/frmSetup.cs =================================================================== --- trunk/source/ECP2Plugin/frmSetup.cs 2006-09-09 17:41:13 UTC (rev 19) +++ trunk/source/ECP2Plugin/frmSetup.cs 2007-02-20 13:40:41 UTC (rev 20) @@ -22,14 +22,14 @@ private void cmdSaveSettings_Click(object sender, EventArgs e) { - MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings("mediaportal.xml"); + MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings(MediaPortal.Configuration.Config.GetFile(MediaPortal.Configuration.Config.Dir.Config, "MediaPortal.xml")); MPSettings.SetValue("ECP2", "ShutdownDelay", NUDShutdownDelay.Value); this.Close(); } private void frmSetup_Load(object sender, EventArgs e) { - MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings("mediaportal.xml"); + MediaPortal.Profile.Settings MPSettings = new MediaPortal.Profile.Settings(MediaPortal.Configuration.Config.GetFile(MediaPortal.Configuration.Config.Dir.Config, "MediaPortal.xml")); NUDShutdownDelay.Value = MPSettings.GetValueAsInt("ECP2", "ShutdownDelay", 10); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |