source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Console/3.3/HiveServerConsole.cs @ 4425

Last change on this file since 4425 was 4425, checked in by cneumuel, 9 years ago
  • removed port setting from HiveServerConsole (port is configured in config file)
  • updated Files.txt
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Net;
24using System.ServiceModel;
25using System.Threading;
26using System.Windows.Forms;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Contracts.ResponseObjects;
30
31namespace HeuristicLab.Hive.Server.ServerConsole {
32
33  public partial class HiveServerConsole : Form {
34
35    HiveServerManagementConsole information = null;
36
37    public HiveServerConsole() {
38      InitializeComponent();
39    }
40
41    private void tsmiExit_Click(object sender, EventArgs e) {
42      this.Close();
43    }
44
45    /// <summary>
46    /// When login button is clicked, the ManagementConsole
47    /// will be opened
48    /// </summary>
49    /// <param name="sender"></param>
50    /// <param name="e"></param>
51    private void BtnLogin_Click(object sender, EventArgs e) {
52      string newIp = tbIp.Text;
53      newIp = newIp.Replace(" ", "");
54
55      ServiceLocator.Address = newIp;
56
57      if (IsValid()) {
58        this.Visible = false;
59        information = new HiveServerManagementConsole();
60        information.closeFormEvent += new closeForm(EnableForm);
61        information.Show();
62      } 
63    }
64
65    /// <summary>
66    /// if the input is correct and the login-method returns a
67    /// valid response
68    /// </summary>
69    /// <returns></returns>
70    private bool IsValid() {
71      Thread t = new Thread(new ThreadStart(ShowWaitDlg));
72      if (IsFormValidated()) {
73        IPAddress ipAdress = IPAddress.Parse(tbIp.Text);
74        IServerConsoleFacade scf;
75        Response resp;
76        try {
77          lblError.Text = "Trying to logon...";
78          this.Cursor = Cursors.WaitCursor;
79          t.Start();
80
81          ServiceLocator.Address = tbIp.Text.Replace(" ", "");
82          ServiceLocator.Username = tbUserName.Text;
83          ServiceLocator.Password = tbPwd.Text;
84          scf = ServiceLocator.GetServerConsoleFacade();
85
86          resp = scf.Login();
87          t.Abort();
88          this.Cursor = Cursors.Default;
89          if (resp.StatusMessage == ResponseStatus.Ok) return true;
90          lblError.Text = resp.StatusMessage.ToString();
91          MessageBox.Show("Wrong username or password");
92        }
93        catch (EndpointNotFoundException ene) {
94          t.Abort();
95          this.Cursor = Cursors.Default;
96          MessageBox.Show(ene.Message);
97          lblError.Text = "No Service at this address!";
98          scf = null;
99        }
100        catch (Exception ex) {
101          //login failed
102          t.Abort();
103          this.Cursor = Cursors.Default;
104          MessageBox.Show(ex.Message);
105          lblError.Text = "Logon failed! Please restart console";
106        }
107      }
108        return false;
109    }
110
111
112    private void ShowWaitDlg() {
113      LogonDlg dlg = new LogonDlg();
114      dlg.ShowDialog();
115    }
116
117    /// <summary>
118    /// Validates the form.
119    /// </summary>
120    /// <returns></returns>
121    private bool IsFormValidated() {
122      bool isValid = true;
123      if (String.IsNullOrEmpty(tbUserName.Text)) {
124        lblError.Text = "Please type in Username.";
125        isValid = false;
126      }
127      if (String.IsNullOrEmpty(tbPwd.Text)) {
128        lblError.Text = "Please type in Password.";
129        isValid = false;
130      }
131      if (String.IsNullOrEmpty(tbIp.Text)) {
132        lblError.Text = "Please type in Port.";
133        isValid = false;
134      }
135      try {
136        IPAddress.Parse(tbIp.Text);
137      }
138      catch (Exception ex) {
139        isValid = false;
140        lblError.Text = "Please verify entered IP address.";
141      }
142      return isValid;
143    }
144
145    private void EnableForm(bool cf, bool error) {
146      if (cf) {
147        this.Visible = true;
148        ServiceLocator.ShutDownFacade();
149        lblError.Text = "";
150        if (error == true) {
151          lblError.Text = "Establishing server connection failed.";
152        }
153      }
154    }
155
156    private void HiveServerConsole_KeyPress(object sender, KeyPressEventArgs e) {
157      if (e.KeyChar == (char)Keys.Return) {
158        BtnLogin_Click(sender, e);
159      }
160    }
161
162    private void btnCancel_Click(object sender, EventArgs e) {
163      Dispose();
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.