Permalink
Browse files

moved info & chat messages out of Form into new class

  • Loading branch information...
1 parent e2bbce6 commit c4edfe52c051e95ad57eda049b9e8a58287844ad seuffert committed Mar 16, 2011
View
@@ -87,6 +87,9 @@ public xbs_console_app(xbs_settings settings, String[] args)
show_cloudlist();
else
{
+ node_list = new xbs_node_list();
+ natstun = new xbs_natstun();
+
}
Console.ReadLine();
View
@@ -67,9 +67,6 @@ partial class FormMain : Form
private int form1_width;
- private static Queue<String> messages = new Queue<String>();
- private static Queue<String> chat_messages = new Queue<String>();
-
private xbs_natstun natstun = null;
private const int MAX_WAIT_START_ENGINE_SECONDS = 6;
@@ -147,9 +144,9 @@ private void ShowVersionInfoMessages()
{
this.Text += " - Version " + xbs_settings.xbslink_version;
#if DEBUG
- addMessage("using PacketDotNet version " + System.Reflection.Assembly.GetAssembly(typeof(PacketDotNet.IpPacket)).GetName().Version.ToString());
- addMessage("using SharpPcap version " + SharpPcap.Version.VersionString);
- addMessage("using Mono.NAT version " + System.Reflection.Assembly.GetAssembly(typeof(Mono.Nat.NatUtility)).GetName().Version.ToString());
+ xbs_messages.addInfoMessage("using PacketDotNet version " + System.Reflection.Assembly.GetAssembly(typeof(PacketDotNet.IpPacket)).GetName().Version.ToString());
+ xbs_messages.addInfoMessage("using SharpPcap version " + SharpPcap.Version.VersionString);
+ xbs_messages.addInfoMessage("using Mono.NAT version " + System.Reflection.Assembly.GetAssembly(typeof(Mono.Nat.NatUtility)).GetName().Version.ToString());
#endif
}
@@ -370,7 +367,7 @@ private void resume_start_engine()
}
catch (Exception)
{
- addMessage("!! UPnP port mapping failed");
+ xbs_messages.addInfoMessage("!! UPnP port mapping failed");
}
try
{
@@ -385,7 +382,7 @@ private void resume_start_engine()
}
catch (Exception)
{
- addMessage("!! STUN discovery failed.");
+ xbs_messages.addInfoMessage("!! STUN discovery failed.");
}
if (external_ip==null)
external_ip = xbs_natstun.getExternalIPAddressFromWebsite();
@@ -394,7 +391,7 @@ private void resume_start_engine()
node_list.local_node = new xbs_node(local_node_ip, udp_listener.udp_socket_port);
node_list.local_node.nickname = textBox_chatNickname.Text;
- sniffer = new xbs_sniffer(pdev, checkBox_all_broadcasts.Checked, checkBox_enable_MAC_list.Checked, checkBox_mac_restriction.Checked);
+ sniffer = new xbs_sniffer(pdev, checkBox_all_broadcasts.Checked, checkBox_enable_MAC_list.Checked, checkBox_mac_restriction.Checked, node_list);
setSnifferMacList();
sniffer.start_capture();
@@ -408,7 +405,7 @@ private void resume_start_engine()
}
catch (Exception)
{
- addMessage("!! open port check failed");
+ xbs_messages.addInfoMessage("!! open port check failed");
}
if (ExceptionMessage.ABORTING)
@@ -417,7 +414,7 @@ private void resume_start_engine()
timer1.Enabled = true;
button_announce.Enabled = true;
saveRegistryValues();
- addMessage("engine ready. waiting for incoming requests.");
+ xbs_messages.addInfoMessage("engine ready. waiting for incoming requests.");
switch_tab = tabPage_info;
textBox_chatEntry.ReadOnly = false;
textBox_chatEntry.Clear();
@@ -446,7 +443,7 @@ private void engine_start()
{
// show Messages to User
tabControl1.SelectedTab = tabPage_messages;
- addMessage("starting Engine");
+ xbs_messages.addInfoMessage("starting Engine");
if (checkbox_UPnP.Checked)
natstun.upnp_startDiscovery();
if (use_STUN && textBox_stunServerHostname.Text.Length > 0 && textBox_stunServerPort.Text.Length > 0)
@@ -480,7 +477,7 @@ private void engine_stop()
if (natstun.isUPnPavailable())
natstun.upnp_deleteAllPortMappings();
engine_started = false;
- addMessage("Engine stopped.");
+ xbs_messages.addInfoMessage("Engine stopped.");
button_start_engine.Text = "Start Engine";
textBox1.Text = "Engine not started.";
textBox_chatEntry.ReadOnly = true;
@@ -846,54 +843,38 @@ private void button_clearMessages_Click(object sender, EventArgs e)
listBox_messages.Items.Clear();
}
- public static void addMessage( String msg )
- {
- DateTime dt = DateTime.Now;
- String str = String.Format("{0:00}", dt.Hour) + ":" + String.Format("{0:00}", dt.Minute) + ":" + String.Format("{0:00}", dt.Second) + " : " + msg;
- lock (messages)
- messages.Enqueue(str);
- }
-
- public static void addChatMessage(String msg)
- {
- lock (chat_messages)
- chat_messages.Enqueue(msg);
- }
-
private void timer_messages_Tick(object sender, EventArgs e)
{
bool added_messages = false;
#if !DEBUG
try
{
#endif
- lock (messages)
- while (messages.Count > 0)
- {
- added_messages = true;
- listBox_messages.Items.Add(messages.Dequeue());
- }
- if (added_messages)
- listBox_messages.SelectedIndex = listBox_messages.Items.Count - 1;
+ while (xbs_messages.getInfoMessageCount() > 0)
+ {
+ added_messages = true;
+ listBox_messages.Items.Add( xbs_messages.DequeueInfoMessageString() );
+ }
+ if (added_messages)
+ listBox_messages.SelectedIndex = listBox_messages.Items.Count - 1;
- added_messages = false;
- lock (chat_messages)
- while (chat_messages.Count > 0)
- {
- added_messages = true;
- textBox_chatMessages.Text += chat_messages.Dequeue();
- }
- if (added_messages)
- {
- if (autoswitch_on_chat_message)
- tabControl1.SelectedTab = tabPage_chat;
- textBox_chatMessages.SelectionStart = textBox_chatMessages.Text.Length;
- textBox_chatMessages.ScrollToCaret();
- }
+ added_messages = false;
+ while (xbs_messages.getChatMessageCount() > 0)
+ {
+ added_messages = true;
+ textBox_chatMessages.Text += xbs_messages.DequeueChatMessage().text;
+ }
+ if (added_messages)
+ {
+ if (autoswitch_on_chat_message)
+ tabControl1.SelectedTab = tabPage_chat;
+ textBox_chatMessages.SelectionStart = textBox_chatMessages.Text.Length;
+ textBox_chatMessages.ScrollToCaret();
+ }
- if ((DateTime.Now - app_start_time).TotalSeconds >= 5)
- if (checkBox_checkForUpdates.Checked && ((DateTime.Now - last_update_check).TotalHours >= 12) )
- checkForProgramUpdates();
+ if ((DateTime.Now - app_start_time).TotalSeconds >= 5)
+ if (checkBox_checkForUpdates.Checked && ((DateTime.Now - last_update_check).TotalHours >= 12))
+ checkForProgramUpdates();
#if !DEBUG
}
catch (Exception ex)
@@ -903,6 +884,7 @@ private void timer_messages_Tick(object sender, EventArgs e)
#endif
}
+
private void checkBox_useStunServer_CheckedChanged(object sender, EventArgs e)
{
use_STUN = checkBox_useStunServer.Checked;
@@ -1142,7 +1124,7 @@ private void button_messages_copy_Click(object sender, EventArgs e)
private void checkIncomingPortWithCloudServer()
{
- addMessage(" contacting cloud server...");
+ xbs_messages.addInfoMessage(" contacting cloud server...");
if (xbs_cloudlist.askCloudServerForHello(textBox_cloudlist.Text, node_list.local_node.ip_public, node_list.local_node.port_public))
{
lock (udp_listener._locker_HELLO)
@@ -1153,7 +1135,7 @@ private void checkIncomingPortWithCloudServer()
if (xbs_natstun.isPortReachable == false)
{
- addMessage("!! cloudlist server HELLO timeout. incoming Port is CLOSED");
+ xbs_messages.addInfoMessage("!! cloudlist server HELLO timeout. incoming Port is CLOSED");
MessageBox.Show(
"Your XBSlink is not reachable from the internet (port closed)." + Environment.NewLine +
"Please configure your router and firewall to forward a port to your computer or use UPnP where available." + Environment.NewLine + Environment.NewLine +
@@ -1164,7 +1146,7 @@ private void checkIncomingPortWithCloudServer()
);
}
else
- addMessage("incoming Port is OPEN");
+ xbs_messages.addInfoMessage("incoming Port is OPEN");
}
}
@@ -1187,7 +1169,7 @@ private void checkForProgramUpdates()
catch (WebException wex)
{
// handle error
- addMessage("!! could not get online update version information: " + wex.Message);
+ xbs_messages.addInfoMessage("!! could not get online update version information: " + wex.Message);
return;
}
@@ -1201,9 +1183,9 @@ private void checkForProgramUpdates()
System.Diagnostics.Process.Start(Resources.url_xbslink_website);
}
else if (new_version_found < 0)
- addMessage("Latest XBSlink version found: v" + result);
+ xbs_messages.addInfoMessage("Latest XBSlink version found: v" + result);
else
- addMessage("You are using the latest XBSlink version.");
+ xbs_messages.addInfoMessage("You are using the latest XBSlink version.");
}
}
View
@@ -98,6 +98,7 @@
<Compile Include="Options.cs" />
<Compile Include="xbs_cloudlist.cs" />
<Compile Include="xbs_chat.cs" />
+ <Compile Include="xbs_messages.cs" />
<Compile Include="xbs_natstun.cs" />
<Compile Include="xbs_settings.cs" />
<Compile Include="DebugWindow.cs">
View
@@ -60,12 +60,12 @@ public static void addLocalMessage(String message)
public static void addSystemMessage(String message)
{
- FormMain.addChatMessage("* " + message + System.Environment.NewLine);
+ xbs_messages.addChatMessage("* " + message + System.Environment.NewLine);
}
public static void addMessage(String message)
{
- FormMain.addChatMessage(message + System.Environment.NewLine);
+ xbs_messages.addChatMessage(message + System.Environment.NewLine);
}
}
View
@@ -122,12 +122,12 @@ private bool parse_cloudlist(String str)
String[] ret_array = str.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);
if (ret_array[0].StartsWith(xbs_cloudlist_returncode.RETURN_CODE_ERROR))
{
- FormMain.addMessage(" x cloudlist server error: " + ret_array[0]);
+ xbs_messages.addInfoMessage(" x cloudlist server error: " + ret_array[0]);
return false;
}
else if (!ret_array[0].StartsWith(xbs_cloudlist_returncode.RETURN_CODE_OK))
{
- FormMain.addMessage(" x unknown response from cloudlist server ");
+ xbs_messages.addInfoMessage(" x unknown response from cloudlist server ");
return false;
}
lock (cloudlist)
@@ -141,7 +141,7 @@ private bool parse_cloudlist(String str)
}
catch (Exception ex)
{
- FormMain.addMessage(" x error adding cloud to cloudlist: " + ex.ToString());
+ xbs_messages.addInfoMessage(" x error adding cloud to cloudlist: " + ex.ToString());
}
}
}
@@ -193,7 +193,7 @@ public bool JoinOrCreateCloud(String url, String cloudname, String max_nodes, St
get_params.Add(xbs_cloudlist_getparameters.GETALLNODES + "=1");
String full_url = url + "?" + String.Join("&", get_params.ToArray());
#if DEBUG
- FormMain.addMessage(" x joining cloud: " + full_url);
+ xbs_messages.addInfoMessage(" x joining cloud: " + full_url);
#endif
WebClient client = new WebClient();
client.Proxy = null;
@@ -213,15 +213,15 @@ public bool JoinOrCreateCloud(String url, String cloudname, String max_nodes, St
return false;
}
#if DEBUG
- FormMain.addMessage(" x cloudlist server result: " + result.Replace("\n","|"));
+ xbs_messages.addInfoMessage(" x cloudlist server result: " + result.Replace("\n","|"));
#endif
part_of_cloud = true;
current_cloudname = cloudname;
- FormMain.addMessage(" x joined cloud " + cloudname);
+ xbs_messages.addInfoMessage(" x joined cloud " + cloudname);
String[] result_rows = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
uuid = result_rows[0].Split(':')[1];
#if DEBUG
- FormMain.addMessage(" x cloud node UUID: " + uuid);
+ xbs_messages.addInfoMessage(" x cloud node UUID: " + uuid);
#endif
cloudlist_url = url;
if (result_rows.Length >= 2)
@@ -238,7 +238,7 @@ public bool JoinOrCreateCloud(String url, String cloudname, String max_nodes, St
}
catch (Exception)
{
- FormMain.addMessage("!! Error getting node contact from cloudlist server: " + result_rows[1]);
+ xbs_messages.addInfoMessage("!! Error getting node contact from cloudlist server: " + result_rows[1]);
return false;
}
xbs_node_message_announce msg = new xbs_node_message_announce(ip, port);
@@ -274,7 +274,7 @@ public bool LeaveCloud()
MessageBox.Show(result);
return false;
}
- FormMain.addMessage(" x left cloud " + current_cloudname);
+ xbs_messages.addInfoMessage(" x left cloud " + current_cloudname);
part_of_cloud = false;
uuid = null;
current_cloudname = null;
@@ -306,7 +306,7 @@ private void update_cloudlist_threadstart()
get_params.Add(xbs_cloudlist_getparameters.UUID + "=" + HttpUtility.UrlEncode(uuid));
string result = null;
String url = cloudlist_url + "?" + String.Join("&", get_params.ToArray());
- FormMain.addMessage(" x started cloudlist updater");
+ xbs_messages.addInfoMessage(" x started cloudlist updater");
#if !DEBUG
try
{
Oops, something went wrong.

0 comments on commit c4edfe5

Please sign in to comment.