home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 1
Development
General Development (no feature request here!)
GlobalServiceProvider: Using own Client Server remote service
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Scrounger" data-source="post: 1034032" data-attributes="member: 87610"><p>Hi,</p><p></p><p>in the last days i tried to create a TvServer remote service to send commands from Client to TvServer using the GlobalServiceProvider. That works all well.</p><p></p><p>Here is the code:</p><p></p><p>libary classes (used with Client and Server plugin)</p><p>[collapse]</p><p>[CODE]Imports TVMLib.MediaPortal.TvDatabase</p><p>Namespace RemoteControl</p><p> Public Interface ITVMControl</p><p> Sub writeToServerLog(ByVal msg As String, ByVal ParamArray arg As Object())</p><p> Sub StartImport()</p><p> Function SearchLocalImages() As String</p><p> End Interface</p><p>End Namespace[/CODE]</p><p></p><p>[CODE]Imports TVMLib.log</p><p>Imports TVMLib.Settings</p><p>Imports TVMLib.MediaPortal.TvServer</p><p></p><p>Namespace RemoteControl</p><p> ''' <summary></p><p> ''' Class which holds the connection with powerscheduler in the tvengine</p><p> ''' </summary></p><p> Public Class TVMControl</p><p>#Region "Variables"</p><p></p><p> ''' <summary></p><p> ''' IPowerController singleton</p><p> ''' </summary></p><p> Private Shared _TVMController As ITVMControl</p><p></p><p> Private Shared _hostName As String = "localhost"</p><p></p><p>#End Region</p><p></p><p>#Region "Public Properties"</p><p></p><p> ''' <summary></p><p> ''' Gets or sets the name the hostname of the master tv-server.</p><p> ''' </summary></p><p> ''' <value>The name of the host.</value></p><p> Public Shared Property HostName() As String</p><p> Get</p><p> Return _hostName</p><p> End Get</p><p> Set(ByVal value As String)</p><p> If _hostName <> value Then</p><p> _TVMController = Nothing</p><p> _hostName = value</p><p></p><p> If _hostName = "localhost" Then</p><p> Instance.writeToServerLog("Registered TVM Client: localhost")</p><p> Else</p><p> Dim _ClientName As String = System.Net.Dns.GetHostName()</p><p> Dim _ClientIP As String = System.Net.Dns.GetHostByName(_ClientName).AddressList(0).ToString()</p><p> Instance.writeToServerLog("Registered TVM Client: {0} ({1})", _ClientName, _ClientIP)</p><p> End If</p><p> End If</p><p> End Set</p><p> End Property</p><p></p><p> ''' <summary></p><p> ''' Returns the one and only instance of the IPowerController (PowerScheduler)</p><p> ''' </summary></p><p> Public Shared ReadOnly Property Instance() As ITVMControl</p><p> Get</p><p> Try</p><p> If _TVMController IsNot Nothing Then</p><p> Return _TVMController</p><p> End If</p><p> Try</p><p> _TVMController = DirectCast(Activator.GetObject(GetType(ITVMControl), [String].Format("http://{0}:31457/TVMRemoteControl", _hostName)), ITVMControl)</p><p> Catch ex As Exception</p><p> MyLog.Error(ex.Message)</p><p> End Try</p><p> Return _TVMController</p><p> Catch generatedExceptionName As Exception</p><p> Return _TVMController</p><p> End Try</p><p> End Get</p><p> End Property</p><p></p><p> ''' <summary></p><p> ''' Is the RemotePowerControl client connected to the server?</p><p> ''' </summary></p><p> Public Shared ReadOnly Property Isconnected() As Boolean</p><p> Get</p><p> Try</p><p> If _TVMController Is Nothing Then</p><p> Return False</p><p> End If</p><p> Return myTvServer.isConnected</p><p> Catch generatedExceptionName As Exception</p><p> Return False</p><p> End Try</p><p> End Get</p><p> End Property</p><p></p><p>#End Region</p><p></p><p>#Region "Public methods"</p><p></p><p> ''' <summary></p><p> ''' Reinitializes the IPowercontroller singleton</p><p> ''' </summary></p><p> Public Shared Sub Clear()</p><p> _TVMController = Nothing</p><p> End Sub</p><p></p><p>#End Region</p><p> End Class</p><p>End Namespace[/CODE]</p><p>[/collapse]</p><p></p><p>server class to register the service on TvServer (Server plugin):</p><p>[collapse]</p><p>[CODE]Imports System.Collections</p><p>Imports System.Collections.Generic</p><p>Imports System.Collections.Specialized</p><p>Imports System.Diagnostics</p><p>Imports System.Runtime.CompilerServices</p><p>Imports System.Runtime.Remoting</p><p>Imports System.Runtime.Remoting.Channels</p><p>Imports System.Runtime.Remoting.Channels.Http</p><p>Imports System.Threading</p><p></p><p>Imports TvControl</p><p>Imports TvDatabase</p><p>Imports TvLibrary.Interfaces</p><p>Imports TvEngine.Interfaces</p><p>Imports TVMLib.log</p><p>Imports TVMLib</p><p>Imports TVMLib.MediaPortal.TvDatabase</p><p>Imports TVMLib.RemoteControl</p><p>Imports TVMLib.Settings</p><p></p><p>Imports TVMServer.TvEngine.TVMServerSetup</p><p></p><p></p><p>Public Class myRemoteControl</p><p> Inherits MarshalByRefObject</p><p> Implements ITVMControl</p><p></p><p>#Region "Variables"</p><p></p><p> ''' <summary></p><p> ''' Reference to tvservice's TVController</p><p> ''' </summary></p><p> Private _controller As IController</p><p> Private _remotingStarted As Boolean = False</p><p> Private _TVMEPGUpdateThread As Thread</p><p>#End Region</p><p></p><p>#Region "Constructor"</p><p></p><p> ''' <summary></p><p> ''' Creates a new PowerScheduler plugin and performs the one-time initialization</p><p> ''' </summary></p><p> Public Sub New()</p><p> ' Add ourselves to the GlobalServiceProvider</p><p> If GlobalServiceProvider.Instance.IsRegistered(Of ITVMControl)() Then</p><p> GlobalServiceProvider.Instance.Remove(Of ITVMControl)()</p><p> End If</p><p> GlobalServiceProvider.Instance.Add(Of ITVMControl)(Me)</p><p> MyLog.Info(String.Empty)</p><p> MyLog.Info("-------------Remote Control---------------------")</p><p> MyLog.Info("Registered TVM service to GlobalServiceProvider")</p><p> End Sub</p><p></p><p> Protected Overrides Sub Finalize()</p><p> Try</p><p> If GlobalServiceProvider.Instance.IsRegistered(Of ITVMControl)() Then</p><p> GlobalServiceProvider.Instance.Remove(Of ITVMControl)()</p><p> MyLog.Debug("UnRegistered TVM service to GlobalServiceProvider")</p><p> End If</p><p> Finally</p><p> MyBase.Finalize()</p><p> End Try</p><p> End Sub</p><p></p><p>#End Region</p><p></p><p>#Region "Start/Stop methods"</p><p></p><p> ''' <summary></p><p> ''' Called by the PowerSchedulerPlugin to start the PowerScheduler</p><p> ''' </summary></p><p> ''' <param name="controller">TVController from the tvservice</param></p><p> <CLSCompliant(False)> _</p><p> Public Sub Start(ByVal controller As IController)</p><p> Try</p><p> Dim threadname As String = Thread.CurrentThread.Name</p><p> If String.IsNullOrEmpty(threadname) Then</p><p> Thread.CurrentThread.Name = "TVMServer"</p><p> End If</p><p></p><p> ' Save controller</p><p> _controller = controller</p><p></p><p> ' Configure remoting if not already done</p><p> StartRemoting()</p><p></p><p> Catch ex As Exception</p><p> MyLog.[Error]("myRemoteControl: Error in Start(): {0}", ex.Message)</p><p> Throw</p><p> End Try</p><p> End Sub</p><p></p><p> ''' <summary></p><p> ''' Called by the PowerSchedulerPlugin to stop the PowerScheduler</p><p> ''' </summary></p><p> Public Sub [Stop]()</p><p> ' Stop the global timer responsible for standby checking and refreshing settings</p><p> If Not _TVMEPGUpdateThread Is Nothing Then</p><p> _TVMEPGUpdateThread.Abort()</p><p> End If</p><p> MyLog.Info("myRemoteControl: TVM Stopped")</p><p> End Sub</p><p></p><p></p><p></p><p>#Region "MarshalByRefObject overrides"</p><p></p><p> ''' <summary></p><p> ''' Make sure SAO never expires</p><p> ''' </summary></p><p> ''' <returns></returns></p><p> Public Overrides Function InitializeLifetimeService() As Object</p><p> Return Nothing</p><p> End Function</p><p></p><p>#End Region</p><p></p><p>#End Region</p><p></p><p>#Region "Remoting"</p><p></p><p> ''' <summary></p><p> ''' Configure remoting for power control from MP</p><p> ''' </summary></p><p> Private Sub StartRemoting()</p><p> If _remotingStarted Then</p><p> Return</p><p> End If</p><p> Try</p><p> Dim channelProperties As New ListDictionary()</p><p> channelProperties.Add("port", 31457)</p><p> channelProperties.Add("exclusiveAddressUse", False)</p><p> Dim channel As New HttpChannel(channelProperties, New SoapClientFormatterSinkProvider(), New SoapServerFormatterSinkProvider())</p><p> ChannelServices.RegisterChannel(channel, False)</p><p> Catch generatedExceptionName As RemotingException</p><p> Catch generatedExceptionName As System.Net.Sockets.SocketException</p><p> End Try</p><p> ' RemotingConfiguration.RegisterWellKnownServiceType(typeof(PowerScheduler), "PowerControl", WellKnownObjectMode.Singleton);</p><p> Dim objref As ObjRef = RemotingServices.Marshal(Me, "TVMRemoteControl")</p><p> MyLog.Info("Registered TVM as remoting service")</p><p> MyLog.Info("-------------------------------------------------------")</p><p> MyLog.Info(String.Empty)</p><p> _remotingStarted = True</p><p> End Sub</p><p></p><p>#End Region</p><p></p><p>#Region "Remote functions"</p><p></p><p> Public Sub StartImport() Implements TVMLib.RemoteControl.ITVMControl.StartImport</p><p> _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SavedSettings.start)</p><p> _TVMEPGUpdateThread.Start()</p><p> _TVMEPGUpdateThread.Join()</p><p> End Sub</p><p></p><p> Public Function SearchLocalImages() As String Implements TVMLib.RemoteControl.ITVMControl.SearchLocalImages</p><p> If _TVMEPGUpdateThread Is Nothing Then</p><p> _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SearchLocalImages.start)</p><p> _TVMEPGUpdateThread.Start()</p><p> Return "started"</p><p> Else</p><p> If Not _TVMEPGUpdateThread.IsAlive Then</p><p> _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SearchLocalImages.start)</p><p> _TVMEPGUpdateThread.Start()</p><p> Return "started"</p><p> Else</p><p> Return "still running"</p><p> End If</p><p> End If</p><p> End Function</p><p></p><p> Private Sub start_EnrichEPG(ByVal mode As enrichEPG)</p><p> 'If IO.File.Exists(TB_TVMPath.Text & "\EPGupdate.exe") Then</p><p></p><p> ' ProgressBar.Style = ProgressBarStyle.Marquee</p><p> ' TVMLib.TVM.ExternalImportStatus.clear()</p><p></p><p> ' Try</p><p> ' If Not TVMEPGUpdateThread.IsAlive Then</p><p> ' TVMEPGUpdateThread = New Thread(AddressOf mode.start)</p><p> ' TVMEPGUpdateThread.Start()</p><p> ' Else</p><p> ' MsgBox("Import still running !", MsgBoxStyle.Information)</p><p> ' End If</p><p> ' Catch ex As Exception</p><p> ' TVMEPGUpdateThread = New Thread(AddressOf mode.start)</p><p> ' TVMEPGUpdateThread.Start()</p><p> ' End Try</p><p></p><p> 'Else</p><p> ' MsgBox("TVM EPGupdate.exe not found!" & vbNewLine & "Check the path: " & mySettings.TVMPath, MsgBoxStyle.Critical, "Error")</p><p> 'End If</p><p> End Sub</p><p>#End Region</p><p></p><p> Public Sub writeToServerLog(ByVal msg As String, ByVal ParamArray arg() As Object) Implements TVMLib.RemoteControl.ITVMControl.writeToServerLog</p><p> MyLog.Info(msg, arg)</p><p> End Sub</p><p>End Class[/CODE]</p><p>[/collapse]</p><p></p><p>Send command from client to TvServer:</p><p>[CODE]TvMControl.HostName = TvDatabase.Server.ListAll.ToList.Find(Function(x) x.IsMaster).HostName</p><p>TvMControl.Instance.writeToServerLog("send message to TvServer log file")[/CODE]</p><p></p><p>Now my question. I would like to check form client side, if the remote service is registered on TvServer. This is nessaccary because i would like to send some remote commands to TvServer on MP Client startup.</p><p></p><p>Hope any can give me the decisive hint?</p></blockquote><p></p>
[QUOTE="Scrounger, post: 1034032, member: 87610"] Hi, in the last days i tried to create a TvServer remote service to send commands from Client to TvServer using the GlobalServiceProvider. That works all well. Here is the code: libary classes (used with Client and Server plugin) [collapse] [CODE]Imports TVMLib.MediaPortal.TvDatabase Namespace RemoteControl Public Interface ITVMControl Sub writeToServerLog(ByVal msg As String, ByVal ParamArray arg As Object()) Sub StartImport() Function SearchLocalImages() As String End Interface End Namespace[/CODE] [CODE]Imports TVMLib.log Imports TVMLib.Settings Imports TVMLib.MediaPortal.TvServer Namespace RemoteControl ''' <summary> ''' Class which holds the connection with powerscheduler in the tvengine ''' </summary> Public Class TVMControl #Region "Variables" ''' <summary> ''' IPowerController singleton ''' </summary> Private Shared _TVMController As ITVMControl Private Shared _hostName As String = "localhost" #End Region #Region "Public Properties" ''' <summary> ''' Gets or sets the name the hostname of the master tv-server. ''' </summary> ''' <value>The name of the host.</value> Public Shared Property HostName() As String Get Return _hostName End Get Set(ByVal value As String) If _hostName <> value Then _TVMController = Nothing _hostName = value If _hostName = "localhost" Then Instance.writeToServerLog("Registered TVM Client: localhost") Else Dim _ClientName As String = System.Net.Dns.GetHostName() Dim _ClientIP As String = System.Net.Dns.GetHostByName(_ClientName).AddressList(0).ToString() Instance.writeToServerLog("Registered TVM Client: {0} ({1})", _ClientName, _ClientIP) End If End If End Set End Property ''' <summary> ''' Returns the one and only instance of the IPowerController (PowerScheduler) ''' </summary> Public Shared ReadOnly Property Instance() As ITVMControl Get Try If _TVMController IsNot Nothing Then Return _TVMController End If Try _TVMController = DirectCast(Activator.GetObject(GetType(ITVMControl), [String].Format("http://{0}:31457/TVMRemoteControl", _hostName)), ITVMControl) Catch ex As Exception MyLog.Error(ex.Message) End Try Return _TVMController Catch generatedExceptionName As Exception Return _TVMController End Try End Get End Property ''' <summary> ''' Is the RemotePowerControl client connected to the server? ''' </summary> Public Shared ReadOnly Property Isconnected() As Boolean Get Try If _TVMController Is Nothing Then Return False End If Return myTvServer.isConnected Catch generatedExceptionName As Exception Return False End Try End Get End Property #End Region #Region "Public methods" ''' <summary> ''' Reinitializes the IPowercontroller singleton ''' </summary> Public Shared Sub Clear() _TVMController = Nothing End Sub #End Region End Class End Namespace[/CODE] [/collapse] server class to register the service on TvServer (Server plugin): [collapse] [CODE]Imports System.Collections Imports System.Collections.Generic Imports System.Collections.Specialized Imports System.Diagnostics Imports System.Runtime.CompilerServices Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Http Imports System.Threading Imports TvControl Imports TvDatabase Imports TvLibrary.Interfaces Imports TvEngine.Interfaces Imports TVMLib.log Imports TVMLib Imports TVMLib.MediaPortal.TvDatabase Imports TVMLib.RemoteControl Imports TVMLib.Settings Imports TVMServer.TvEngine.TVMServerSetup Public Class myRemoteControl Inherits MarshalByRefObject Implements ITVMControl #Region "Variables" ''' <summary> ''' Reference to tvservice's TVController ''' </summary> Private _controller As IController Private _remotingStarted As Boolean = False Private _TVMEPGUpdateThread As Thread #End Region #Region "Constructor" ''' <summary> ''' Creates a new PowerScheduler plugin and performs the one-time initialization ''' </summary> Public Sub New() ' Add ourselves to the GlobalServiceProvider If GlobalServiceProvider.Instance.IsRegistered(Of ITVMControl)() Then GlobalServiceProvider.Instance.Remove(Of ITVMControl)() End If GlobalServiceProvider.Instance.Add(Of ITVMControl)(Me) MyLog.Info(String.Empty) MyLog.Info("-------------Remote Control---------------------") MyLog.Info("Registered TVM service to GlobalServiceProvider") End Sub Protected Overrides Sub Finalize() Try If GlobalServiceProvider.Instance.IsRegistered(Of ITVMControl)() Then GlobalServiceProvider.Instance.Remove(Of ITVMControl)() MyLog.Debug("UnRegistered TVM service to GlobalServiceProvider") End If Finally MyBase.Finalize() End Try End Sub #End Region #Region "Start/Stop methods" ''' <summary> ''' Called by the PowerSchedulerPlugin to start the PowerScheduler ''' </summary> ''' <param name="controller">TVController from the tvservice</param> <CLSCompliant(False)> _ Public Sub Start(ByVal controller As IController) Try Dim threadname As String = Thread.CurrentThread.Name If String.IsNullOrEmpty(threadname) Then Thread.CurrentThread.Name = "TVMServer" End If ' Save controller _controller = controller ' Configure remoting if not already done StartRemoting() Catch ex As Exception MyLog.[Error]("myRemoteControl: Error in Start(): {0}", ex.Message) Throw End Try End Sub ''' <summary> ''' Called by the PowerSchedulerPlugin to stop the PowerScheduler ''' </summary> Public Sub [Stop]() ' Stop the global timer responsible for standby checking and refreshing settings If Not _TVMEPGUpdateThread Is Nothing Then _TVMEPGUpdateThread.Abort() End If MyLog.Info("myRemoteControl: TVM Stopped") End Sub #Region "MarshalByRefObject overrides" ''' <summary> ''' Make sure SAO never expires ''' </summary> ''' <returns></returns> Public Overrides Function InitializeLifetimeService() As Object Return Nothing End Function #End Region #End Region #Region "Remoting" ''' <summary> ''' Configure remoting for power control from MP ''' </summary> Private Sub StartRemoting() If _remotingStarted Then Return End If Try Dim channelProperties As New ListDictionary() channelProperties.Add("port", 31457) channelProperties.Add("exclusiveAddressUse", False) Dim channel As New HttpChannel(channelProperties, New SoapClientFormatterSinkProvider(), New SoapServerFormatterSinkProvider()) ChannelServices.RegisterChannel(channel, False) Catch generatedExceptionName As RemotingException Catch generatedExceptionName As System.Net.Sockets.SocketException End Try ' RemotingConfiguration.RegisterWellKnownServiceType(typeof(PowerScheduler), "PowerControl", WellKnownObjectMode.Singleton); Dim objref As ObjRef = RemotingServices.Marshal(Me, "TVMRemoteControl") MyLog.Info("Registered TVM as remoting service") MyLog.Info("-------------------------------------------------------") MyLog.Info(String.Empty) _remotingStarted = True End Sub #End Region #Region "Remote functions" Public Sub StartImport() Implements TVMLib.RemoteControl.ITVMControl.StartImport _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SavedSettings.start) _TVMEPGUpdateThread.Start() _TVMEPGUpdateThread.Join() End Sub Public Function SearchLocalImages() As String Implements TVMLib.RemoteControl.ITVMControl.SearchLocalImages If _TVMEPGUpdateThread Is Nothing Then _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SearchLocalImages.start) _TVMEPGUpdateThread.Start() Return "started" Else If Not _TVMEPGUpdateThread.IsAlive Then _TVMEPGUpdateThread = New Thread(AddressOf enrichEPG.SearchLocalImages.start) _TVMEPGUpdateThread.Start() Return "started" Else Return "still running" End If End If End Function Private Sub start_EnrichEPG(ByVal mode As enrichEPG) 'If IO.File.Exists(TB_TVMPath.Text & "\EPGupdate.exe") Then ' ProgressBar.Style = ProgressBarStyle.Marquee ' TVMLib.TVM.ExternalImportStatus.clear() ' Try ' If Not TVMEPGUpdateThread.IsAlive Then ' TVMEPGUpdateThread = New Thread(AddressOf mode.start) ' TVMEPGUpdateThread.Start() ' Else ' MsgBox("Import still running !", MsgBoxStyle.Information) ' End If ' Catch ex As Exception ' TVMEPGUpdateThread = New Thread(AddressOf mode.start) ' TVMEPGUpdateThread.Start() ' End Try 'Else ' MsgBox("TVM EPGupdate.exe not found!" & vbNewLine & "Check the path: " & mySettings.TVMPath, MsgBoxStyle.Critical, "Error") 'End If End Sub #End Region Public Sub writeToServerLog(ByVal msg As String, ByVal ParamArray arg() As Object) Implements TVMLib.RemoteControl.ITVMControl.writeToServerLog MyLog.Info(msg, arg) End Sub End Class[/CODE] [/collapse] Send command from client to TvServer: [CODE]TvMControl.HostName = TvDatabase.Server.ListAll.ToList.Find(Function(x) x.IsMaster).HostName TvMControl.Instance.writeToServerLog("send message to TvServer log file")[/CODE] Now my question. I would like to check form client side, if the remote service is registered on TvServer. This is nessaccary because i would like to send some remote commands to TvServer on MP Client startup. Hope any can give me the decisive hint? [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 1
Development
General Development (no feature request here!)
GlobalServiceProvider: Using own Client Server remote service
Contact us
RSS
Top
Bottom