5 minute pausing works, need more console logging and service status check mechanism.
parent
1da5ab43e5
commit
e4424fbea1
|
@ -29,6 +29,7 @@ namespace WifiSitter
|
||||||
private volatile bool _paused;
|
private volatile bool _paused;
|
||||||
private static WifiSitterIpc _wsIpc;
|
private static WifiSitterIpc _wsIpc;
|
||||||
private Action<object, XDMessageEventArgs> _handleMsgRecv;
|
private Action<object, XDMessageEventArgs> _handleMsgRecv;
|
||||||
|
private System.Timers.Timer _pauseTimer;
|
||||||
|
|
||||||
#endregion // fields
|
#endregion // fields
|
||||||
|
|
||||||
|
@ -90,10 +91,17 @@ namespace WifiSitter
|
||||||
LogLine("Version: {0}", v.ToString());
|
LogLine("Version: {0}", v.ToString());
|
||||||
|
|
||||||
// Setup IPC
|
// Setup IPC
|
||||||
|
// TODO make this tunable, cmd argument?
|
||||||
LogLine("Initializing IPC...");
|
LogLine("Initializing IPC...");
|
||||||
_handleMsgRecv = new Action<object, XDMessageEventArgs>(HandleMsgReceived);
|
_handleMsgRecv = new Action<object, XDMessageEventArgs>(HandleMsgReceived);
|
||||||
_wsIpc = new WifiSitterIpc(_handleMsgRecv);
|
_wsIpc = new WifiSitterIpc(_handleMsgRecv);
|
||||||
|
|
||||||
|
// Initialize timeout timer
|
||||||
|
_pauseTimer = new System.Timers.Timer();
|
||||||
|
_pauseTimer.AutoReset = false;
|
||||||
|
_pauseTimer.Interval = 5 * 60 * 1000; // five minutes
|
||||||
|
_pauseTimer.Elapsed += (o, e) => { _paused = false; };
|
||||||
|
|
||||||
// Check if there are any interfaces not detected by GetAllNetworkInterfaces()
|
// Check if there are any interfaces not detected by GetAllNetworkInterfaces()
|
||||||
// That method will not show disabled interfaces
|
// That method will not show disabled interfaces
|
||||||
_ignoreNics = ReadNicWhitelist();
|
_ignoreNics = ReadNicWhitelist();
|
||||||
|
@ -260,6 +268,9 @@ namespace WifiSitter
|
||||||
while (!_shutdownEvent.WaitOne(0)) {
|
while (!_shutdownEvent.WaitOne(0)) {
|
||||||
|
|
||||||
if (_paused) {
|
if (_paused) {
|
||||||
|
|
||||||
|
// TODO do network state checks while paused so state can be sent to client, if client has polled in the last 90 seconds
|
||||||
|
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -332,6 +343,9 @@ namespace WifiSitter
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResetNicState (NetworkState netstate) {
|
private void ResetNicState (NetworkState netstate) {
|
||||||
|
if (netstate == null)
|
||||||
|
return;
|
||||||
|
|
||||||
var taskList = new List<Task>();
|
var taskList = new List<Task>();
|
||||||
foreach (var n in netstate.OriginalNicState) {
|
foreach (var n in netstate.OriginalNicState) {
|
||||||
var id = n[0];
|
var id = n[0];
|
||||||
|
@ -370,16 +384,32 @@ namespace WifiSitter
|
||||||
WifiSitterIpcMessage _msg = null;
|
WifiSitterIpcMessage _msg = null;
|
||||||
try { _msg = Newtonsoft.Json.JsonConvert.DeserializeObject<WifiSitterIpcMessage>(e.DataGram.Message); }
|
try { _msg = Newtonsoft.Json.JsonConvert.DeserializeObject<WifiSitterIpcMessage>(e.DataGram.Message); }
|
||||||
catch { LogLine("Deserialize to WifiSitterIpcMessage failed."); }
|
catch { LogLine("Deserialize to WifiSitterIpcMessage failed."); }
|
||||||
|
WifiSitterIpcMessage response;
|
||||||
if (_msg != null) {
|
if (_msg != null) {
|
||||||
if (_msg.Request == "get_netstate") {
|
switch (_msg.Request) {
|
||||||
|
case "get_netstate":
|
||||||
LogLine("Sending netstate to: {0}", _msg.Requestor);
|
LogLine("Sending netstate to: {0}", _msg.Requestor);
|
||||||
var response = new WifiSitterIpcMessage("give_netstate", _wsIpc.MyChannelName, "", Newtonsoft.Json.JsonConvert.SerializeObject(new Model.SimpleNetworkState(netstate)));
|
response = new WifiSitterIpcMessage("give_netstate", _wsIpc.MyChannelName, "", Newtonsoft.Json.JsonConvert.SerializeObject(new Model.SimpleNetworkState(netstate)));
|
||||||
_wsIpc.MsgBroadcaster.SendToChannel(_msg.Target, response.IpcMessageJsonString());
|
_wsIpc.MsgBroadcaster.SendToChannel(_msg.Target, response.IpcMessageJsonString());
|
||||||
|
break;
|
||||||
|
case "take_five":
|
||||||
|
try {
|
||||||
|
LogLine("Taking 5 minute break and restoring interfaces.");
|
||||||
|
_pauseTimer.Stop();
|
||||||
|
_pauseTimer.Start();
|
||||||
|
_paused = true;
|
||||||
|
ResetNicState(netstate);
|
||||||
|
response = new WifiSitterIpcMessage("taking_five", _wsIpc.MyChannelName, "", "");
|
||||||
|
_wsIpc.MsgBroadcaster.SendToChannel(_msg.Target, response.IpcMessageJsonString());
|
||||||
|
}
|
||||||
|
catch { WriteLog(LogType.error, "Failed to enter paused state after 'take_five' request received."); }
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Trace.WriteLine(e.DataGram.Message);
|
Trace.WriteLine(String.Format("Message issue: {0}", e.DataGram.Message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,10 +23,12 @@
|
||||||
<tb:TaskbarIcon.ContextMenu>
|
<tb:TaskbarIcon.ContextMenu>
|
||||||
<ContextMenu>
|
<ContextMenu>
|
||||||
<MenuItem Header="Pause for 5 minutes"
|
<MenuItem Header="Pause for 5 minutes"
|
||||||
ToolTip="Restores interfaces to original status." />
|
ToolTip="Restores interfaces to original status."
|
||||||
|
Command="{Binding SendTakeFiveRequest}" />
|
||||||
<MenuItem Header="Status and Settings"
|
<MenuItem Header="Status and Settings"
|
||||||
ToolTip=""
|
ToolTip=""
|
||||||
Command="{Binding LaunchSettingsWindow}" />
|
Command="{Binding LaunchSettingsWindow}" />
|
||||||
|
<Separator />
|
||||||
<MenuItem Header="Quit"
|
<MenuItem Header="Quit"
|
||||||
Click="ContextMenu_Quit"
|
Click="ContextMenu_Quit"
|
||||||
Icon="{StaticResource CloseIcon}"
|
Icon="{StaticResource CloseIcon}"
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Input;
|
||||||
using WifiSitter;
|
using WifiSitter;
|
||||||
using WifiSitter.Model;
|
using WifiSitter.Model;
|
||||||
using WifiSitterGui.Helpers;
|
using WifiSitterGui.Helpers;
|
||||||
|
@ -18,10 +19,12 @@ namespace WifiSitterGui.ViewModel
|
||||||
|
|
||||||
private static MainWindowViewModel _windowVM;
|
private static MainWindowViewModel _windowVM;
|
||||||
private RelayCommand _launchWindowCommand;
|
private RelayCommand _launchWindowCommand;
|
||||||
|
private RelayCommand _takeFiveCommand; // Asks service to pause for 5 minutes
|
||||||
private static MainWindow _statusGui;
|
private static MainWindow _statusGui;
|
||||||
private static WifiSitterIpc _wsIpc;
|
private static WifiSitterIpc _wsIpc;
|
||||||
private Action<object, XDMessageEventArgs> _handleMsgRcv;
|
private Action<object, XDMessageEventArgs> _handleMsgRcv;
|
||||||
private static string _serviceChannel;
|
private static string _serviceChannel;
|
||||||
|
private System.Timers.Timer _netstateCheckTimer;
|
||||||
|
|
||||||
#endregion // fields
|
#endregion // fields
|
||||||
|
|
||||||
|
@ -43,16 +46,14 @@ namespace WifiSitterGui.ViewModel
|
||||||
_handleMsgRcv = new Action<object, XDMessageEventArgs>(wsIpc_MessageReceived);
|
_handleMsgRcv = new Action<object, XDMessageEventArgs>(wsIpc_MessageReceived);
|
||||||
_wsIpc = new WifiSitter.WifiSitterIpc(_handleMsgRcv);
|
_wsIpc = new WifiSitter.WifiSitterIpc(_handleMsgRcv);
|
||||||
|
|
||||||
Trace.WriteLine(String.Format("WifiSitter service msg channel: {0}", ServiceChannelName));
|
// Intermittent network state polling
|
||||||
|
_netstateCheckTimer = new System.Timers.Timer();
|
||||||
|
_netstateCheckTimer.AutoReset = true;
|
||||||
|
_netstateCheckTimer.Interval = 30 * 1000; // 30 seconds
|
||||||
|
_netstateCheckTimer.Elapsed += (o, e) => { RequestNetworkState(); };
|
||||||
|
_netstateCheckTimer.Start();
|
||||||
|
|
||||||
if (!String.IsNullOrEmpty(ServiceChannelName)) {
|
Trace.WriteLine(String.Format("WifiSitter service msg channel: {0}", ServiceChannelName));
|
||||||
try {
|
|
||||||
_wsIpc.MsgBroadcaster.SendToChannel(ServiceChannelName, new WifiSitterIpcMessage("get_netstate", _wsIpc.MyChannelName, _wsIpc.MyChannelName).IpcMessageJsonString());
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
Trace.WriteLine(e.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion // constructor
|
#endregion // constructor
|
||||||
|
@ -83,18 +84,27 @@ namespace WifiSitterGui.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetServiceChannelName() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion // properties
|
#endregion // properties
|
||||||
|
|
||||||
#region methods
|
#region methods
|
||||||
|
|
||||||
|
private void RequestNetworkState () {
|
||||||
|
if (!String.IsNullOrEmpty(ServiceChannelName)) {
|
||||||
|
try {
|
||||||
|
Trace.WriteLine("Checking for network state.");
|
||||||
|
_wsIpc.MsgBroadcaster.SendToChannel(ServiceChannelName, new WifiSitterIpcMessage("get_netstate", _wsIpc.MyChannelName, _wsIpc.MyChannelName).IpcMessageJsonString());
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Trace.WriteLine(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion // methods
|
#endregion // methods
|
||||||
|
|
||||||
#region commands
|
#region commands
|
||||||
|
|
||||||
public RelayCommand LaunchSettingsWindow {
|
public ICommand LaunchSettingsWindow {
|
||||||
get {
|
get {
|
||||||
if (_launchWindowCommand == null) {
|
if (_launchWindowCommand == null) {
|
||||||
_launchWindowCommand = new RelayCommand(() => {
|
_launchWindowCommand = new RelayCommand(() => {
|
||||||
|
@ -114,6 +124,20 @@ namespace WifiSitterGui.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ICommand SendTakeFiveRequest {
|
||||||
|
get {
|
||||||
|
if (_takeFiveCommand == null) {
|
||||||
|
_takeFiveCommand = new RelayCommand(() => {
|
||||||
|
var request = new WifiSitterIpcMessage("take_five", _wsIpc.MyChannelName, _wsIpc.MyChannelName);
|
||||||
|
_wsIpc.MsgBroadcaster.SendToChannel(_serviceChannel, request.IpcMessageJsonString());
|
||||||
|
// TODO need response validation mechanism
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return _takeFiveCommand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion // commands
|
#endregion // commands
|
||||||
|
|
||||||
#region events
|
#region events
|
||||||
|
@ -129,11 +153,20 @@ namespace WifiSitterGui.ViewModel
|
||||||
catch { Trace.WriteLine("Deserialize to ServiceRequest failed."); }
|
catch { Trace.WriteLine("Deserialize to ServiceRequest failed."); }
|
||||||
|
|
||||||
if (_sr != null) {
|
if (_sr != null) {
|
||||||
if (_sr.Request == "give_netstate") {
|
switch (_sr.Request) {
|
||||||
try {
|
case "give_netstate":
|
||||||
WindowVM.NetState = Newtonsoft.Json.JsonConvert.DeserializeObject<SimpleNetworkState>(System.Text.Encoding.UTF8.GetString(_sr.Payload));
|
try { WindowVM.NetState = Newtonsoft.Json.JsonConvert.DeserializeObject<SimpleNetworkState>(System.Text.Encoding.UTF8.GetString(_sr.Payload)); }
|
||||||
}
|
|
||||||
catch { WifiSitter.WifiSitter.LogLine("Failed to deserialize netstate, payload."); }
|
catch { WifiSitter.WifiSitter.LogLine("Failed to deserialize netstate, payload."); }
|
||||||
|
break;
|
||||||
|
case "taking_five":
|
||||||
|
Trace.WriteLine("Service paused.");
|
||||||
|
break;
|
||||||
|
case "service_status":
|
||||||
|
// TODO issue service status update
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Trace.WriteLine(String.Format("Unknown request type: {0} from {1}", _sr?.Request, _sr?.Requestor));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Reference in New Issue