Browse files

moved GUI code from cloudlist to Form

added xbs_cloud class
cloudlist uses xbs_cloud List
implemented console cloud list
  • Loading branch information...
1 parent cdf12f5 commit e2bbce6b2a635ee6306c3dc0c1bd9247c1e517d0 @seuffert committed Mar 16, 2011
Showing with 102 additions and 32 deletions.
  1. +23 −2 XBSlink/CommandLine.cs
  2. +26 −4 XBSlink/Form1.cs
  3. +53 −26 XBSlink/xbs_cloudlist.cs
View
25 XBSlink/CommandLine.cs
@@ -21,6 +21,7 @@ class xbs_console_app
public static xbs_sniffer sniffer = null;
public static xbs_node_list node_list = null;
private xbs_natstun natstun = null;
+ private xbs_cloudlist cloudlist = null;
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();
@@ -31,9 +32,11 @@ public xbs_console_app(xbs_settings settings, String[] args)
AllocConsole();
xbs_settings = settings;
+ cloudlist = new xbs_cloudlist();
bool cmd_help = false;
bool cmd_list_devices = false;
+ bool cmd_list_clouds = false;
String option_nickname = null;
String option_cloudserver = null;
String option_cloudname = null;
@@ -43,10 +46,11 @@ public xbs_console_app(xbs_settings settings, String[] args)
IPAddress option_local_ip = null;
String option_capture_device = null;
OptionSet command_line_option_set = new OptionSet() {
- { "h|help", "show this help message", v => cmd_help = v!=null },
+ { "?|h|help", "show this help message", v => cmd_help = v!=null },
{ "l|list-devices", "list all available network packet capture devices", v => cmd_list_devices = v != null },
{ "n|nickname=", "set the nickname", v => option_nickname=v },
{ "s|cloudserver=", "set cloudserver URL", v => option_cloudserver=v },
+ { "j|list-clouds", "list available clouds on cloudserver", v => cmd_list_clouds = v!=null },
{ "c|cloudname=", "connect to this cloud", v => option_cloudname=v },
{ "u|upnp", "use UPnP to forward incoming port", v => option_upnp = v!=null },
{ "a|advanced-broadcast", "enable advanced forwarding of broadcasts", v => option_advanced_broadcast = v!=null },
@@ -68,7 +72,7 @@ public xbs_console_app(xbs_settings settings, String[] args)
command_line_parser_error(command_line_option_set, e);
return;
}
- if (!cmd_help && !cmd_list_devices && option_capture_device == null)
+ if (!cmd_help && !cmd_list_devices && !cmd_list_clouds && option_capture_device == null)
{
command_line_parser_error(command_line_option_set, new OptionException("you need to specify a capture device", "capture-device"));
return;
@@ -79,6 +83,11 @@ public xbs_console_app(xbs_settings settings, String[] args)
ShowHelp(command_line_option_set);
else if (cmd_list_devices)
list_Devices(args);
+ else if (cmd_list_clouds)
+ show_cloudlist();
+ else
+ {
+ }
Console.ReadLine();
}
@@ -117,5 +126,17 @@ private void ShowHelp(OptionSet command_line_option_set)
command_line_option_set.WriteOptionDescriptions(Console.Out);
}
+ private void show_cloudlist()
+ {
+ bool ret = cloudlist.loadCloudlistFromURL( xbs_cloudlist.DEFAULT_CLOUDLIST_SERVER );
+ xbs_cloud[] clouds = cloudlist.getCloudlistArray();
+ int count=0;
+ Console.WriteLine("Available clouds on cloudlist server:");
+ foreach (xbs_cloud cloud in clouds)
+ {
+ count++;
+ Console.WriteLine(" " + count + ") " + cloud.name + " (" + cloud.node_count + "/" + cloud.max_nodes + ") " + (cloud.isPrivate ? " (password)" : ""));
+ }
+ }
}
}
View
30 XBSlink/Form1.cs
@@ -113,7 +113,6 @@ private void initializeCloudListView()
for (int i = 1; i < listView_clouds.Columns.Count; i++)
width -= listView_clouds.Columns[i].Width;
listView_clouds.Columns[0].Width = width-listView_clouds.Columns.Count-1;
- cloudlist = new xbs_cloudlist(this.listView_clouds);
}
private void Form1_Load(object sender, EventArgs e)
@@ -122,6 +121,7 @@ private void Form1_Load(object sender, EventArgs e)
WebRequest.DefaultWebProxy = null;
node_list = new xbs_node_list();
+ cloudlist = new xbs_cloudlist();
updatecheck_webclient = new WebClient();
natstun = new xbs_natstun();
@@ -1030,14 +1030,36 @@ private void buttonLoadCloudlist_Click(object sender, EventArgs e)
bool ret = cloudlist.loadCloudlistFromURL( textBox_cloudlist.Text );
if (ret)
{
- xbs_settings.setRegistryValue(xbs_settings.REG_CLOUDLIST_SERVER, textBox_cloudlist.Text);
- if (listView_clouds.Items.Count>0)
- toolTip2.Show(listView_clouds.Items.Count + " clouds loaded.", buttonLoadCloudlist, 0, -20, 2000);
+ xbs_cloud[] clouds = cloudlist.getCloudlistArray();
+ if (clouds.Length > 0)
+ {
+ initCloudListView();
+ foreach (xbs_cloud cloud in clouds)
+ {
+ ListViewItem lv_item = new ListViewItem(cloud.name);
+ lv_item.SubItems.Add(cloud.node_count.ToString());
+ lv_item.SubItems.Add(cloud.max_nodes.ToString());
+ if (cloud.isPrivate)
+ lv_item.ImageIndex = 0;
+ listView_clouds.Items.Add(lv_item);
+ }
+ toolTip2.Show(clouds.Length + " clouds loaded.", buttonLoadCloudlist, 0, -20, 2000);
+ }
else
toolTip2.Show("no clouds available on server.", buttonLoadCloudlist, 0, -20, 2000);
+
+ xbs_settings.setRegistryValue(xbs_settings.REG_CLOUDLIST_SERVER, textBox_cloudlist.Text);
}
}
+ private void initCloudListView()
+ {
+ listView_clouds.Items.Clear();
+ ImageList il = new ImageList();
+ il.Images.Add(Properties.Resources.icon_key);
+ listView_clouds.SmallImageList = il;
+ }
+
private void button_CloudJoin_Click(object sender, EventArgs e)
{
if (textBox_CloudName.Text.Length>=xbs_cloudlist.MIN_CLOUDNAME_LENGTH)
View
79 XBSlink/xbs_cloudlist.cs
@@ -63,6 +63,22 @@ class xbs_cloudlist_getparameters
public const String GETALLNODES = "getallnodes";
}
+ class xbs_cloud
+ {
+ public String name;
+ public int node_count;
+ public int max_nodes;
+ public bool isPrivate = false;
+
+ public xbs_cloud(String name, int node_count, int max_nodes, bool isPrivate)
+ {
+ this.name = name;
+ this.node_count = node_count;
+ this.max_nodes = max_nodes;
+ this.isPrivate = isPrivate;
+ }
+ }
+
class xbs_cloudlist
{
public const String DEFAULT_CLOUDLIST_SERVER = "http://www.secudb.de/~seuffert/xbslink/cloudlist";
@@ -74,13 +90,12 @@ class xbs_cloudlist
public String uuid = null;
public String cloudlist_url = null;
- private ListView cloudlist_view = null;
-
private Thread update_thread = null;
- public xbs_cloudlist(ListView clv)
+ private List<xbs_cloud> cloudlist = new List<xbs_cloud>();
+
+ public xbs_cloudlist()
{
- cloudlist_view = clv;
}
public bool loadCloudlistFromURL(String url)
@@ -102,14 +117,6 @@ public bool loadCloudlistFromURL(String url)
return parse_cloudlist(result);
}
- private void initCloudListView()
- {
- cloudlist_view.Items.Clear();
- ImageList il = new ImageList();
- il.Images.Add(Properties.Resources.icon_key);
- cloudlist_view.SmallImageList = il;
- }
-
private bool parse_cloudlist(String str)
{
String[] ret_array = str.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);
@@ -123,16 +130,19 @@ private bool parse_cloudlist(String str)
FormMain.addMessage(" x unknown response from cloudlist server ");
return false;
}
- initCloudListView();
- for (int i = 1; i < ret_array.Length; i++)
+ lock (cloudlist)
{
- try
- {
- parseAndAddCloudFromURLString(ret_array[i]);
- }
- catch (Exception ex)
+ cloudlist.Clear();
+ for (int i = 1; i < ret_array.Length; i++)
{
- FormMain.addMessage(" x error adding cloud to cloudlist: "+ex.ToString());
+ try
+ {
+ parseAndAddCloudFromURLString(ret_array[i]);
+ }
+ catch (Exception ex)
+ {
+ FormMain.addMessage(" x error adding cloud to cloudlist: " + ex.ToString());
+ }
}
}
return true;
@@ -144,12 +154,13 @@ private void parseAndAddCloudFromURLString( String s )
List<String> keys = new List<String>(query.AllKeys);
if (keys.Contains(xbs_cloudlist_getparameters.CLOUDNAME) && keys.Contains(xbs_cloudlist_getparameters.MAXNODES) & keys.Contains(xbs_cloudlist_getparameters.PASSWORD) & keys.Contains(xbs_cloudlist_getparameters.COUNTNODES))
{
- ListViewItem lv_item = new ListViewItem(query[xbs_cloudlist_getparameters.CLOUDNAME]);
- lv_item.SubItems.Add(query[xbs_cloudlist_getparameters.COUNTNODES]);
- lv_item.SubItems.Add(query[xbs_cloudlist_getparameters.MAXNODES]);
- if (query[xbs_cloudlist_getparameters.PASSWORD].ToUpper() == "TRUE")
- lv_item.ImageIndex = 0;
- cloudlist_view.Items.Add(lv_item);
+ String cloudname = query[xbs_cloudlist_getparameters.CLOUDNAME];
+ int node_count;
+ int.TryParse(query[xbs_cloudlist_getparameters.COUNTNODES], out node_count);
+ int max_nodes;
+ int.TryParse(query[xbs_cloudlist_getparameters.MAXNODES], out max_nodes);
+ bool isPrivate = query[xbs_cloudlist_getparameters.PASSWORD].ToUpper() == "TRUE";
+ cloudlist.Add( new xbs_cloud(cloudname, node_count, max_nodes, isPrivate) );
}
}
@@ -351,5 +362,21 @@ public static bool askCloudServerForHello( String server, IPAddress node_ip, int
}
return (result.StartsWith(xbs_cloudlist_returncode.RETURN_CODE_OK));
}
+
+ public xbs_cloud[] getCloudlistArray()
+ {
+ xbs_cloud[] clouds;
+ lock (cloudlist)
+ clouds = cloudlist.ToArray();
+ return clouds;
+ }
+
+ public int cloud_count()
+ {
+ int count;
+ lock (cloudlist)
+ count = cloudlist.Count;
+ return count;
+ }
}
}

0 comments on commit e2bbce6

Please sign in to comment.