GlobalServiceProvider: Using own Client Server remote service (1 Viewer)

Scrounger

Retired Team Member
  • Premium Supporter
  • January 21, 2009
    1,032
    514
    Stuttgart
    Home Country
    Germany Germany
    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:
    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
    [/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
    [/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")

    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?
     

    Users who are viewing this thread

    Top Bottom