Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerConsole.cs @ 2103

Last change on this file since 2103 was 2103, checked in by mbecirov, 15 years ago

#586: Handling of exceptions when user enters a wrong IP or Port at Login

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using HeuristicLab.Hive.Contracts;
33using System.Security.Cryptography;
34using System.Net;
35using System.Threading;
36using System.ServiceModel;
37
38namespace HeuristicLab.Hive.Server.ServerConsole {
39
40  public partial class HiveServerConsole : Form {
41
42    HiveServerManagementConsole information = null;
43
44    public HiveServerConsole() {
45      InitializeComponent();
46#if(DEBUG)
47      tbIp.Text = "10.20.53.1";
48      tbPort.Text = WcfSettings.GetDefaultPort().ToString();
49      tbUserName.Text = "admin";
50      tbPwd.Text = "admin";
51#endif
52    }
53
54    private void tsmiExit_Click(object sender, EventArgs e) {
55      this.Close();
56    }
57
58    /// <summary>
59    /// When login button is clicked, the ManagementConsole
60    /// will be opened
61    /// </summary>
62    /// <param name="sender"></param>
63    /// <param name="e"></param>
64    private void BtnLogin_Click(object sender, EventArgs e) {
65      string newIp = tbIp.Text;
66      newIp = newIp.Replace(" ", "");
67
68      ServiceLocator.Address = newIp;
69      ServiceLocator.Port = this.tbPort.Text;
70
71      if (IsValid()) {
72        this.Visible = false;
73        information = new HiveServerManagementConsole();
74        information.closeFormEvent += new closeForm(EnableForm);
75        information.Show();
76      }
77    }
78
79    /// <summary>
80    /// if the input is correct and the login-method returns a
81    /// valid response
82    /// </summary>
83    /// <returns></returns>
84    private bool IsValid() {
85      Thread t = new Thread(new ThreadStart(ShowWaitDlg));
86      if (IsFormValidated()) {
87        IPAddress ipAdress = IPAddress.Parse(tbIp.Text);
88        int port = int.Parse(tbPort.Text);
89        ServiceLocator.Address = tbIp.Text.Replace(" ", "");
90        ServiceLocator.Port = tbPort.Text;
91        IServerConsoleFacade scf = ServiceLocator.GetServerConsoleFacade();
92        try {
93          lblError.Text = "Trying to logon...";
94          t.Start();
95          Response resp = scf.Login(tbUserName.Text, tbPwd.Text);
96          t.Abort();
97          if (resp.StatusMessage == "Logged in") return true;
98          lblError.Text = resp.StatusMessage;
99        }
100        catch (EndpointNotFoundException ene) {
101          lblError.Text = "No Service at this address!";
102        }
103        catch (FaultException ex) {
104          //login failed
105          lblError.Text = "Logon failed!";
106          t.Abort();
107          return false;
108        }
109      }
110      //validation failed
111      t.Abort();
112      return false;
113    }
114
115    private void ShowWaitDlg() {
116      LogonDlg dlg = new LogonDlg();
117      dlg.ShowDialog();
118    }
119
120    /// <summary>
121    /// Validates the form.
122    /// </summary>
123    /// <returns></returns>
124    private bool IsFormValidated() {
125      bool isValid = true;
126      if (String.IsNullOrEmpty(tbUserName.Text)) {
127        lblError.Text = "Please type in Username.";
128        isValid = false;
129      }
130      if (String.IsNullOrEmpty(tbPwd.Text)) {
131        lblError.Text = "Please type in Password.";
132        isValid = false;
133      }
134      if (String.IsNullOrEmpty(tbIp.Text)) {
135        lblError.Text = "Please type in Port.";
136        isValid = false;
137      }
138      if (String.IsNullOrEmpty(tbPort.Text)) {
139        lblError.Text = "Please type in IP-Address.";
140        isValid = false;
141      }
142      try {
143        int.Parse(tbPort.Text);
144      }
145      catch (Exception ex) {
146        isValid = false;
147        lblError.Text = "Please verify entered Port.";
148      }
149      try {
150        IPAddress.Parse(tbIp.Text);
151      }
152      catch (Exception ex) {
153        isValid = false;
154        lblError.Text = "Please verify entered IP address.";
155      }
156      return isValid;
157    }
158
159    private void EnableForm(bool cf, bool error) {
160      if (cf) {
161        this.Visible = true;
162        ServiceLocator.ShutDownFacade();
163        if (error == true) {
164          lblError.Text = "Establishing server connection failed.";
165        }
166      }
167    }
168
169    private void HiveServerConsole_KeyPress(object sender, KeyPressEventArgs e) {
170      if (e.KeyChar == (char)Keys.Return) {
171        BtnLogin_Click(sender, e);
172      }
173    }
174
175    private void btnCancel_Click(object sender, EventArgs e) {
176      Dispose();
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.