Skip to content

Commit

Permalink
Resolve Issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmurph committed Feb 16, 2018
1 parent 76a390f commit b0e260e
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 21 deletions.
20 changes: 18 additions & 2 deletions PlexService/PlexMediaServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text;
using PlexServiceCommon;
using PlexServiceWCF;
using System.Threading;

namespace PlexService
{
Expand All @@ -31,6 +32,8 @@ public partial class PlexMediaServerService : ServiceBase

private PlexServiceCommon.Interface.ITrayInteraction _plexService;

private AutoResetEvent _stopped = new AutoResetEvent(false);

public PlexMediaServerService()
{
InitializeComponent();
Expand Down Expand Up @@ -121,6 +124,11 @@ protected override void OnStop()
if (_plexService != null)
{
_plexService.Stop();
//wait for plex to stop for 10 seconds
if(!_stopped.WaitOne(10000))
{
TrayInteraction.WriteToLog("Timed out waiting for plex to stop");
}
Disconnect();
}
}
Expand Down Expand Up @@ -153,16 +161,24 @@ private void Connect()
plexServiceBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(1);
//Generate the endpoint from the local settings
var plexServiceEndpoint = new EndpointAddress(_address);

TrayCallback callback = new TrayCallback();
callback.Stopped += (s,e) => _stopped.Set();
var client = new TrayInteractionClient(callback, plexServiceBinding, plexServiceEndpoint);

//Make a channel factory so we can create the link to the service
var plexServiceChannelFactory = new ChannelFactory<PlexServiceCommon.Interface.ITrayInteraction>(plexServiceBinding, plexServiceEndpoint);
//var plexServiceChannelFactory = new ChannelFactory<PlexServiceCommon.Interface.ITrayInteraction>(plexServiceBinding, plexServiceEndpoint);

_plexService = null;

try
{
_plexService = plexServiceChannelFactory.CreateChannel();
_plexService = client.ChannelFactory.CreateChannel();

_plexService.Subscribe();
//If we lose connection to the service, set the object to null so we will know to reconnect the next time the tray icon is clicked
((ICommunicationObject)_plexService).Faulted += (s, e) => _plexService = null;
((ICommunicationObject)_plexService).Closed += (s, e) => _plexService = null;
}
catch
{
Expand Down
4 changes: 2 additions & 2 deletions PlexService/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
3 changes: 3 additions & 0 deletions PlexServiceCommon/Interface/ITrayCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public interface ITrayCallback
{
[OperationContract]
void OnPlexStateChange(PlexState state);

[OperationContract]
void OnPlexStopped();
}
}
2 changes: 2 additions & 0 deletions PlexServiceCommon/PlexServiceCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.cs" />
<Compile Include="StatusChangeEventArgs.cs" />
<Compile Include="TrayCallback.cs" />
<Compile Include="TrayInteractionClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
4 changes: 2 additions & 2 deletions PlexServiceCommon/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Text;
using PlexServiceCommon;

namespace PlexServiceTray
namespace PlexServiceCommon
{
class TrayCallback : PlexServiceCommon.Interface.ITrayCallback
public class TrayCallback : Interface.ITrayCallback
{
public void OnPlexStateChange(PlexState state)
{
Expand Down Expand Up @@ -40,5 +40,21 @@ protected void OnStateChange(string message)

#endregion

public void OnPlexStopped()
{
OnStopped();
}

#region StateChange

public event EventHandler<EventArgs> Stopped;

protected void OnStopped()
{
Stopped?.Invoke(this, new EventArgs());
}

#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.ServiceModel.Channels;
using System.Text;

namespace PlexServiceTray
namespace PlexServiceCommon
{
public class TrayInteractionClient:DuplexClientBase<ITrayInteraction>
{
Expand Down
4 changes: 2 additions & 2 deletions PlexServiceInstaller/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">
<Product Id="*" Name="Plex Service" Language="1033" Version="1.1.4" Manufacturer="cjmurph" UpgradeCode="daa483bb-c7e1-4c79-a4d6-3c2c4c3d9daf">
<Product Id="*" Name="Plex Service" Language="1033" Version="1.1.5" Manufacturer="cjmurph" UpgradeCode="daa483bb-c7e1-4c79-a4d6-3c2c4c3d9daf">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" SummaryCodepage="1252" />
<Binary Id="success_bmp" SourceFile="$(sys.SOURCEFILEDIR)success.bmp" />
<Binary Id="error_bmp" SourceFile="$(sys.SOURCEFILEDIR)error.bmp" />
Expand All @@ -21,7 +21,7 @@
<Condition Message="This application requires .NET Framework 4.0 to run. Please install the .NET Framework then run this installer again.">
<![CDATA[Installed OR NETFRAMEWORK40FULL]]>
</Condition>
<Feature Id="PlexServiceFeature" Title="Plex Service" Description="Plex Service 1.1.4" Level="1" ConfigurableDirectory='INSTALLDIR'>
<Feature Id="PlexServiceFeature" Title="Plex Service" Description="Plex Service 1.1.5" Level="1" ConfigurableDirectory='INSTALLDIR'>
<ComponentRef Id="PlexServiceComponent" />
<ComponentRef Id="PlexServiceWCFComponent" />
<ComponentRef Id="PlexServiceCommonComponent" />
Expand Down
4 changes: 2 additions & 2 deletions PlexServiceTray/NotifyIconApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ protected override void Dispose(bool disposing)

public NotifyIconApplicationContext()
{
initializeContext();
InitializeContext();
Connect();
}

/// <summary>
/// Setup our tray icon
/// </summary>
private void initializeContext()
private void InitializeContext()
{
_components = new System.ComponentModel.Container();
_notifyIcon = new NotifyIcon(_components);
Expand Down
2 changes: 0 additions & 2 deletions PlexServiceTray/PlexServiceTray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
</Compile>
<Compile Include="SettingsWindowViewModel.cs" />
<Compile Include="Themes\Metro\Light\CheckBoxTransformConverter.cs" />
<Compile Include="TrayCallback.cs" />
<Compile Include="TrayInteractionClient.cs" />
<Compile Include="Validation\UniqueAuxAppNameAttribute.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down
4 changes: 2 additions & 2 deletions PlexServiceTray/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
2 changes: 2 additions & 0 deletions PlexServiceWCF/PmsMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ private void EndPlex()
//remove event hook
appMonitor.StatusChange -= OnPlexStatusChange;
});

OnPlexStop(this, new EventArgs());
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions PlexServiceWCF/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
22 changes: 20 additions & 2 deletions PlexServiceWCF/TrayInteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@ public TrayInteraction()
_pms = new PmsMonitor();
_pms.PlexStatusChange += OnPlexEvent;
_pms.StateChange += PlexStateChange;
_pms.PlexStop += PlexStopped;
///Start plex
Start();
}

private void PlexStopped(object sender, EventArgs e)
{
if (_pms != null)
{
CallbackChannels.ForEach(callback =>
{
if (callback != null)
{
try
{
callback.OnPlexStopped();
}
catch { }
}
});
}
}

private void PlexStateChange(object sender, EventArgs e)
{
if (_pms != null)
Expand Down Expand Up @@ -65,8 +84,7 @@ public void Start()
/// </summary>
public void Stop()
{
//do this in the calling thread so it only returns upon completion of stop
_pms.Stop();
Task.Factory.StartNew(() => _pms.Stop());
}

/// <summary>
Expand Down

0 comments on commit b0e260e

Please sign in to comment.