Skip to content

Commit

Permalink
Significantly better option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
drkno committed Jan 1, 2016
1 parent a35fc23 commit 6bec8bd
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 298 deletions.
2 changes: 0 additions & 2 deletions PCR1000LibTest/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PCR1000LibTest
Expand Down
106 changes: 0 additions & 106 deletions PCRNetworkServer/Arguments.cs

This file was deleted.

168 changes: 14 additions & 154 deletions PCRNetworkServer/Cli.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO.Ports;
using System.Linq;
using PCR1000;
using PCR1000.Network;

Expand All @@ -10,168 +8,30 @@ public static class Cli
{
private static PcrNetworkServer _pcrNetworkServer;

private static void PrintHelp(Exception ex = null)
{
if (ex != null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: The program failed with an error.");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(ex.Message + "\n");
Console.WriteLine("For further help start with -h or --help.");
#if DEBUG
Console.ReadKey();
#endif
return;
}

var name = AppDomain.CurrentDomain.FriendlyName;
name = name.Substring(0, name.LastIndexOf('.'));

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("NAME");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t" + name);

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nSYNOPSIS");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t" + name + " [OPTION]...");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nDESCRIPTION");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\tStarts a new PCR1000 network server. This allows remote control of\n" +
"\tthe receiver.\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-h, --help");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tDisplays this help.\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-u, --ui");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tDetermines what ui to display (gui/cli).\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-l, --ssl");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tUse SSL for the security of the server.\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-p, --passwd");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tPassword to use to authenticate the connection.\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-n, --nport");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tNetwork port to use. Defaults to 4456.\n");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-s, --sport");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tSerial port to use. Defaults to first serial port.");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t-d, --devices");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\t\tLists all avalible devices.");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nAUTHOR");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\tWritten by Matthew Knox.");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nCOPYRIGHT");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\tCopyright (c) Matthew Knox. Released under MIT license.");

#if DEBUG
Console.ReadKey();
#endif
}

public static void Run()
public static void Run(bool security, string password, int port, string device)
{
var title = Console.Title;
Console.Title = "Network Server";
var conCol = Console.ForegroundColor;
if (Arguments.GetArgumentBool("help") || Arguments.GetArgumentBool("h"))
{
PrintHelp();
return;
}

var portNames = SerialPort.GetPortNames();
if (Arguments.GetArgumentBool("devices") || Arguments.GetArgumentBool("d"))
{
foreach (var portName in portNames)
{
Console.WriteLine(portName);
}
return;
}
_pcrNetworkServer = !string.IsNullOrWhiteSpace(password) || security ?
new PcrNetworkServer(new PcrSerialComm(device), port, security, password) :
new PcrNetworkServer(new PcrSerialComm(device), port);
#if DEBUG
_pcrNetworkServer.SetDebugLogger(true);
#endif

try
if (!_pcrNetworkServer.Start())
{
int nport;
if (!int.TryParse(Arguments.GetArgument("nport"), out nport))
{
if (!int.TryParse(Arguments.GetArgument("n"), out nport))
{
nport = 4456;
}
}

string sport;
if (string.IsNullOrWhiteSpace(sport = Arguments.GetArgument("sport")) && string.IsNullOrWhiteSpace(sport = Arguments.GetArgument("s")))
{
if (portNames.Length == 0)
{
Console.Error.WriteLine("No serial ports are avalible for use!");
return;
}
sport = portNames[0];
}
else if (!portNames.Contains(sport))
{
Console.Error.WriteLine("Serial port provided does not exist on this system.");
return;
}

var passwd = Arguments.GetArgument("passwd");
if (string.IsNullOrWhiteSpace(passwd))
{
passwd = Arguments.GetArgument("p");
}

var ssl = Arguments.GetArgumentBool("ssl") || Arguments.GetArgumentBool("l");
_pcrNetworkServer = !string.IsNullOrWhiteSpace(passwd) || ssl ?
new PcrNetworkServer(new PcrSerialComm(sport), nport, ssl, passwd) :
new PcrNetworkServer(new PcrSerialComm(sport), nport);
#if DEBUG
_pcrNetworkServer.SetDebugLogger(true);
#endif

if (!_pcrNetworkServer.Start())
{
throw new Exception("Starting the network server failed.");
}
throw new Exception("Starting the network server failed.");
}

Console.WriteLine("Server Started. Press any key to stop...");
Console.ReadKey(true);
Console.WriteLine("Server Started. Press any key to stop...");
Console.ReadKey(true);

if (!_pcrNetworkServer.Stop())
{
throw new Exception("Stopping the network server failed.");
}
}
catch (Exception ex)
if (!_pcrNetworkServer.Stop())
{
PrintHelp(ex);
throw new Exception("Stopping the network server failed.");
}
Console.ForegroundColor = conCol;
Console.Title = title;
Expand Down
31 changes: 8 additions & 23 deletions PCRNetworkServer/Gui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,20 @@ public partial class Gui : Form
{
private PcrNetworkServer _pcrNetworkServer;

public Gui()
public Gui(bool security, string password, int port, string device)
{
InitializeComponent();

var ports = SerialPort.GetPortNames();
foreach (var port in ports)
foreach (var dev in ports)
{
comboBoxSerialPort.Items.Add(port);
comboBoxSerialPort.Items.Add(dev);
}
comboBoxSerialPort.SelectedValue = Arguments.GetArgument("sport");

int n;
if (int.TryParse(Arguments.GetArgument("nport"), out n))
{
textBoxNetwork.Text = Arguments.GetArgument("nport");
}

textBoxPassword.Text = Arguments.GetArgument("passwd");
if (string.IsNullOrWhiteSpace(textBoxPassword.Text))
{
textBoxPassword.Text = Arguments.GetArgument("p");
}

checkBoxSsl.Checked = Arguments.GetArgumentBool("ssl") || Arguments.GetArgumentBool("l");
comboBoxSerialPort.SelectedIndex = comboBoxSerialPort.Items.IndexOf(device);
textBoxNetwork.Text = port.ToString();
textBoxPassword.Text = password;
checkBoxSsl.Checked = security;

if (checkBoxSsl.Checked || !string.IsNullOrWhiteSpace(textBoxPassword.Text))
{
Expand All @@ -50,12 +40,7 @@ private bool CheckSettings()
return false;
}

if (comboBoxSerialPort.SelectedIndex == -1)
{
return false;
}

return true;
return comboBoxSerialPort.SelectedIndex != -1;
}

private bool _isOn;
Expand Down
Loading

0 comments on commit 6bec8bd

Please sign in to comment.