private void button_Add_Click(object sender, EventArgs e) { bool bContinue = true; bool bShowMessage = true; if (0 != textBox_Value.Text.Length) { String sItem = textBox_Value.Text; switch (m_type) { case iControl.LocalLBClassClassType.CLASS_TYPE_ADDRESS: // Check for CIDR format; if (!isAddress(sItem)) { MessageBox.Show("'" + textBox_Value.Text + "' is not a valid network address!", "Input Error"); bShowMessage = false; textBox_Value.Focus(); textBox_Value.SelectAll(); bContinue = false; } else { // Valid address, now find out which kind. System.Net.IPAddress ipAddr = System.Net.IPAddress.Parse(sItem); if (ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { bContinue = false; // IPv6 try { if (textBox_Netmask.Text.Length == 0) { bContinue = true; } else { int v = Convert.ToInt32(textBox_Netmask.Text); if ((v >= 0) && (v <= 128)) { sItem = getAddress(sItem) + "/" + textBox_Netmask.Text.ToString(); bContinue = true; } } } catch (Exception) { // Toss away exception } } else if ((textBox_Netmask.Text.Length > 0) && !(textBox_Netmask.Text.Equals("255.255.255.255") || textBox_Netmask.Text.Equals("32"))) { bContinue = false; // IPv4 if (isAddress(textBox_Netmask.Text)) { if (CIDRHelper.isValidNetworkAddress(textBox_Value.Text, textBox_Netmask.Text)) { sItem = getAddress(sItem) + "/" + getAddress(textBox_Netmask.Text); bContinue = true; } else { MessageBox.Show("Invalid address and mask. Address must equal address & mask", "Input error"); bShowMessage = false; textBox_Value.Focus(); textBox_Value.SelectAll(); bContinue = false; } } else { // Check for CIDR foramt. try { int shorthand = Convert.ToInt32(textBox_Netmask.Text); String sMask = CIDRHelper.getMask(shorthand); if (null != sMask) { if (CIDRHelper.isValidNetworkAddress(textBox_Value.Text, sMask)) { sItem = getAddress(sItem) + "/" + shorthand.ToString(); bContinue = true; } else { MessageBox.Show("Invalid address and mask. Address must equal address & mask", "Input error"); bShowMessage = false; textBox_Value.Focus(); textBox_Value.SelectAll(); bContinue = false; } } } catch (Exception) { } } } if (!bContinue) { // Cannot specify both a CIDR and netmask if (bShowMessage) { MessageBox.Show("'" + textBox_Netmask.Text + "' is not a valid network address or CIDR format!", "Input Error"); } bShowMessage = false; textBox_Value.Focus(); textBox_Value.SelectAll(); bContinue = false; } } break; case iControl.LocalLBClassClassType.CLASS_TYPE_VALUE: if (!isInteger(sItem)) { MessageBox.Show("'" + textBox_Value.Text + "' is not a valid integer!", "Input Error"); bShowMessage = false; textBox_Value.Focus(); textBox_Value.SelectAll(); bContinue = false; } break; case iControl.LocalLBClassClassType.CLASS_TYPE_STRING: break; } if (bContinue) { // Check for duplicates bool bDuplicate = false; //for(int i=0; i<listBox_Members.Items.Count; i++) for (int i = 0; i < listView_Members.Items.Count; i++) { // Extract out the datavalue format // key := datavalue // String sEntry = listBox_Members.Items[i].ToString(); //String[] tokens = listBox_Members.Items[i].ToString().Split(new String[] { " := " }, StringSplitOptions.RemoveEmptyEntries); //String sEntry = tokens[0]; String sEntry = listView_Members.Items[i].Text.ToString(); if (sEntry.Equals(sItem)) { bDuplicate = true; break; } } if (bDuplicate) { MessageBox.Show("ERROR: Data Groups cannot contain duplicate entries"); } else { // Add support for datavalues. //if (textBox_DataValue.Text.Length > 0) //{ // sItem += " := " + textBox_DataValue.Text; //} //listBox_Members.Items.Add(sItem); //textBox_Value.Text = ""; //textBox_Netmask.Text = ""; //textBox_Value.Focus(); ListViewItem lvi = new ListViewItem(sItem); listView_Members.Items.Add(lvi); if (textBox_DataValue.Text.Length > 0) { lvi.SubItems.Add(textBox_DataValue.Text); } } } if (m_mode == DialogMode.DIALOGMODE_NEW) { button_OK.Text = "Create"; switch (m_type) { case iControl.LocalLBClassClassType.CLASS_TYPE_ADDRESS: this.Text = "New Address Data Group"; break; case iControl.LocalLBClassClassType.CLASS_TYPE_VALUE: this.Text = "New Integer Data Group"; break; case iControl.LocalLBClassClassType.CLASS_TYPE_STRING: this.Text = "New String Data Group"; break; } } else { button_OK.Text = "Update"; switch (m_type) { case iControl.LocalLBClassClassType.CLASS_TYPE_ADDRESS: this.Text = "Editing Address Data Group"; break; case iControl.LocalLBClassClassType.CLASS_TYPE_VALUE: this.Text = "Editing Integer Data Group"; break; case iControl.LocalLBClassClassType.CLASS_TYPE_STRING: this.Text = "Editing String Data Group"; break; } } } }
private bool modifyClass() { // TODO: Add data value support bool bModified = false; //int num_members = listBox_Members.Items.Count; int num_members = listView_Members.Items.Count; String sItem = ""; switch (m_type) { case iControl.LocalLBClassClassType.CLASS_TYPE_ADDRESS: { iControl.LocalLBClassAddressClass[] class_list = new iControl.LocalLBClassAddressClass[1]; class_list[0] = new iControl.LocalLBClassAddressClass(); class_list[0].name = textBox_Name.Text; class_list[0].members = new iControl.LocalLBClassAddressEntry[num_members]; String[][] data_values = new String[1][]; data_values[0] = new String[num_members]; for (int i = 0; i < num_members; i++) { //String[] tokens = listBox_Members.Items[i].ToString().Split(new String[] { " := " }, StringSplitOptions.RemoveEmptyEntries); //sItem = tokens[0]; ////sItem = listBox_Members.Items[i].ToString(); //String[] sSplit = sItem.Split(new char[] { '/' }); //class_list[0].members[i] = new iControl.LocalLBClassAddressEntry(); //class_list[0].members[i].address = sSplit[0]; //class_list[0].members[i].netmask = "255.255.255.255"; //if (sSplit.Length > 1) //{ // if (isAddress(sSplit[1])) // { // class_list[0].members[i].netmask = sSplit[1]; // } // else // { // class_list[0].members[i].netmask = CIDRHelper.getMask(Convert.ToInt32(sSplit[1])); // } //} //data_values[0][i] = (tokens.Length > 1) ? tokens[1] : ""; sItem = listView_Members.Items[i].Text.ToString(); String[] sSplit = sItem.Split(new char[] { '/' }); class_list[0].members[i] = new iControl.LocalLBClassAddressEntry(); class_list[0].members[i].address = sSplit[0]; class_list[0].members[i].netmask = "255.255.255.255"; if (sSplit.Length > 1) { if (isAddress(sSplit[1])) { class_list[0].members[i].netmask = getAddress(sSplit[1]); } else { class_list[0].members[i].netmask = CIDRHelper.getMask(Convert.ToInt32(sSplit[1])); } } if (listView_Members.Items[i].SubItems.Count > 1) { data_values[0][i] = listView_Members.Items[i].SubItems[1].Text.ToString(); } else { data_values[0][i] = ""; } } try { //try //{ // Clients.Class.delete_class(new String[] { textBox_Name.Text }); //} //catch (Exception) { } //Clients.Class.create_address_class(class_list); Clients.Class.modify_address_class(class_list); if (m_bLTMSupportsValues) { Clients.Class.set_address_class_member_data_value(class_list, data_values); } bModified = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } break; case iControl.LocalLBClassClassType.CLASS_TYPE_VALUE: { iControl.LocalLBClassValueClass[] class_list = new iControl.LocalLBClassValueClass[1]; class_list[0] = new iControl.LocalLBClassValueClass(); class_list[0].name = textBox_Name.Text; class_list[0].members = new long[num_members]; String[][] data_values = new String[1][]; data_values[0] = new String[num_members]; for (int i = 0; i < num_members; i++) { //String[] tokens = listBox_Members.Items[i].ToString().Split(new String[] { " := " }, StringSplitOptions.RemoveEmptyEntries); //sItem = tokens[0]; //class_list[0].members[i] = Convert.ToInt32(sItem); //data_values[0][i] = (tokens.Length > 1) ? tokens[1] : ""; class_list[0].members[i] = Convert.ToInt32(listView_Members.Items[i].Text.ToString()); if (listView_Members.Items[i].SubItems.Count > 1) { data_values[0][i] = listView_Members.Items[i].SubItems[1].Text.ToString(); } else { data_values[0][i] = ""; } } try { //Clients.Class.delete_class(new String[] { textBox_Name.Text }); //Clients.Class.create_value_class(class_list); Clients.Class.modify_value_class(class_list); if (m_bLTMSupportsValues) { Clients.Class.set_value_class_member_data_value(class_list, data_values); } bModified = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } break; case iControl.LocalLBClassClassType.CLASS_TYPE_STRING: { iControl.LocalLBClassStringClass[] class_list = new iControl.LocalLBClassStringClass[1]; class_list[0] = new iControl.LocalLBClassStringClass(); class_list[0].name = textBox_Name.Text; class_list[0].members = new String[num_members]; String[][] data_values = new String[1][]; data_values[0] = new String[num_members]; for (int i = 0; i < num_members; i++) { //String[] tokens = listBox_Members.Items[i].ToString().Split(new String[] { " := " }, StringSplitOptions.RemoveEmptyEntries); //sItem = tokens[0]; //class_list[0].members[i] = sItem; //data_values[0][i] = (tokens.Length > 1) ? tokens[1] : ""; class_list[0].members[i] = listView_Members.Items[i].Text.ToString(); if (listView_Members.Items[i].SubItems.Count > 1) { data_values[0][i] = listView_Members.Items[i].SubItems[1].Text.ToString(); } else { data_values[0][i] = ""; } } try { //Clients.Class.delete_class(new String[] { textBox_Name.Text }); //Clients.Class.create_string_class(class_list); Clients.Class.modify_string_class(class_list); if (m_bLTMSupportsValues) { Clients.Class.set_string_class_member_data_value(class_list, data_values); } bModified = true; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Error"); } } break; } if (bModified) { try { Clients.ConfigSync.save_configuration("", iControl.SystemConfigSyncSaveMode.SAVE_HIGH_LEVEL_CONFIG); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error Saving Configuration"); } } return(bModified); }