FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 8f8ad008 authored by Dr Adam Thorn's avatar Dr Adam Thorn
Browse files

Strip out all WCF messaging stuff and start again

All of this was cobbled together copy-pasta. Lets try again
at desiging the communication properly now I've experimented a bit.
parent 9e9e8a52
No related branches found
No related tags found
No related merge requests found
using System.ServiceModel;
namespace WpkgInstaller
{
public interface IWpkgMessageCallbackHandlerContract
{
[OperationContract(IsOneWay = true)]
void HandleResponse(WpkgMessageResponse response);
[OperationContract(IsOneWay = true)]
void HandleMessage(WpkgMessageState status);
}
}
using System.ServiceModel;
namespace WpkgInstaller
{
public delegate WpkgMessageRequestType WpkgEventCallbackHandler(object sender, WpkgMessageRequest e);
[ServiceContract(Namespace = "https://www.ch.cam.ac.uk/WpkgNotifier",
SessionMode = SessionMode.Required,
CallbackContract = typeof(IWpkgMessageCallbackHandlerContract))]
public interface IWpkgMessageRequestHandlerContract
{
[OperationContract(IsOneWay = true)]
void HandleMessage(WpkgMessageRequestType type);
}
}
......@@ -34,7 +34,6 @@ namespace WpkgInstaller
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(System.IntPtr handle, ref ServiceStatus serviceStatus);
private IWpkgMessageCallbackHandlerContract notifierCallbackHandler;
public WpkgInstaller()
{
InitializeComponent();
......@@ -88,14 +87,12 @@ namespace WpkgInstaller
private void RunWpkgSync()
{
notifierCallbackHandler.HandleMessage(WpkgMessageState.SYNCING);
eventLog.WriteEntry("Starting sync", EventLogEntryType.Information);
RunWpkgCommand("/synchronize");
}
private void RunWpkgQuery()
{
notifierCallbackHandler.HandleMessage(WpkgMessageState.QUERYING);
eventLog.WriteEntry("Starting query", EventLogEntryType.Information);
RunWpkgCommand("/query:m");
}
......@@ -127,15 +124,13 @@ namespace WpkgInstaller
process.BeginOutputReadLine();
process.WaitForExit();
notifierCallbackHandler.HandleMessage(WpkgMessageState.IDLE);
if(process.ExitCode == 0)
{
notifierCallbackHandler.HandleMessage(WpkgMessageState.SUCCESS);
// notify clients of success
}
else
{
notifierCallbackHandler.HandleMessage(WpkgMessageState.ERROR);
// notify clients of error
}
String msg = String.Format("wpkg command finished, with exit code {0}", process.ExitCode);
eventLog.WriteEntry(msg, EventLogEntryType.Information);
......@@ -146,8 +141,7 @@ namespace WpkgInstaller
{
if (e != null && e.Data != null)
{
notifierCallbackHandler.HandleResponse(new WpkgMessageResponse(e.Data));
//eventLog.WriteEntry(e.Data, EventLogEntryType.Information);
// sent e.Data back to clients
}
}
......@@ -170,15 +164,7 @@ namespace WpkgInstaller
{
try
{
Uri BaseAddress = new Uri("net.pipe://localhost/www.ch.cam.ac.uk/WpkgNotifier");
NetNamedPipeBinding binding = new NetNamedPipeBinding();
WpkgInstallerMessageService svc = new WpkgInstallerMessageService();
ServiceHost SvcHost = new ServiceHost(svc, BaseAddress);
SvcHost.AddServiceEndpoint(typeof(IWpkgMessageRequestHandlerContract), binding, "");
SvcHost.Open();
(SvcHost.SingletonInstance as WpkgInstallerMessageService).WpkgEventCallback += new WpkgEventCallbackHandler(ProcessMessage);
// open message channel
}
catch (Exception e)
{
......@@ -189,10 +175,6 @@ namespace WpkgInstaller
private WpkgMessageRequestType ProcessMessage(object sender, WpkgMessageRequest e)
{
if (notifierCallbackHandler == null)
{
notifierCallbackHandler = OperationContext.Current.GetCallbackChannel<IWpkgMessageCallbackHandlerContract>();
}
eventLog.WriteEntry(string.Format("Got {0}", e.Type));
switch(e.Type)
......
......@@ -70,7 +70,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IWpkgMessageCallbackHandlerContract.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
......@@ -85,8 +84,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WpkgInstallerMessageService.cs" />
<Compile Include="IWpkgMessageRequestHandlerContract.cs" />
<Compile Include="WpkgMessageRequest.cs" />
<Compile Include="WpkgMessageResponse.cs" />
<Compile Include="WpkgMessageState.cs" />
......
using System.ServiceModel;
namespace WpkgInstaller
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class WpkgInstallerMessageService : IWpkgMessageRequestHandlerContract
{
public event WpkgEventCallbackHandler WpkgEventCallback = delegate { return WpkgMessageRequestType.DONE; };
public void HandleMessage(WpkgMessageRequestType type)
{
WpkgEventCallback(this, new WpkgMessageRequest(type));
}
}
}
......@@ -8,10 +8,10 @@ using WpkgInstaller;
namespace WpkgNotifier
{
class TrayNotifier : ApplicationContext, IWpkgMessageCallbackHandlerContract
class TrayNotifier : ApplicationContext
{
private NotifyIcon notifyIcon;
private IWpkgMessageRequestHandlerContract messageChannel;
private EventLog eventLog;
private WpkgMessageState state;
......@@ -69,14 +69,7 @@ namespace WpkgNotifier
eventLog.WriteEntry("Trying to open channel");
try
{
string address = "net.pipe://localhost/www.ch.cam.ac.uk/WpkgNotifier";
NetNamedPipeBinding binding = new NetNamedPipeBinding();
InstanceContext context = new InstanceContext(this);
DuplexChannelFactory<IWpkgMessageRequestHandlerContract> factory = new DuplexChannelFactory<IWpkgMessageRequestHandlerContract>(context, binding, address);
messageChannel = factory.CreateChannel();
eventLog.WriteEntry("channel created");
// this method should probably be 'RegisterWithService'
}
catch (Exception e)
{
......@@ -89,13 +82,13 @@ namespace WpkgNotifier
void RequestSync(object sender, EventArgs e)
{
notifyIcon.ContextMenu.MenuItems[0].Enabled = false;
messageChannel.HandleMessage(WpkgMessageRequestType.DO_SYNC);
// send DO_SYNC request to server
}
void RequestQuery(object sender, EventArgs e)
{
notifyIcon.ContextMenu.MenuItems[0].Enabled = false;
messageChannel.HandleMessage(WpkgMessageRequestType.RUN_QUERY);
// send RUN_QUERY request to server
}
void ManageSoftware(object sender, EventArgs e)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment