Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 5.4 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      ServiceLocator.Port = this.tbPort.Text;
57
58      if (IsValid()) {
59        this.Visible = false;
60        information = new HiveServerManagementConsole();
61        information.closeFormEvent += new closeForm(EnableForm);
62        information.Show();
63      }
64    }
65
66    /// <summary>
67    /// if the input is correct and the login-method returns a
68    /// valid response
69    /// </summary>
70    /// <returns></returns>
71    private bool IsValid() {
72      Thread t = new Thread(new ThreadStart(ShowWaitDlg));
73      if (IsFormValidated()) {
74        IPAddress ipAdress = IPAddress.Parse(tbIp.Text);
75        int port = int.Parse(tbPort.Text);
76        IServerConsoleFacade scf;
77        Response resp;
78        try {
79          lblError.Text = "Trying to logon...";
80          this.Cursor = Cursors.WaitCursor;
81          t.Start();
82
83          ServiceLocator.Address = tbIp.Text.Replace(" ", "");
84          ServiceLocator.Port = tbPort.Text;
85          ServiceLocator.Username = tbUserName.Text;
86          ServiceLocator.Password = tbPwd.Text;
87          scf = ServiceLocator.GetServerConsoleFacade();
88
89          resp = scf.Login();
90          t.Abort();
91          this.Cursor = Cursors.Default;
92          if (resp.StatusMessage == ResponseStatus.Ok) return true;
93          lblError.Text = resp.StatusMessage.ToString();
94          MessageBox.Show("Wrong username or password");
95        }
96        catch (EndpointNotFoundException ene) {
97          t.Abort();
98          this.Cursor = Cursors.Default;
99          MessageBox.Show(ene.Message);
100          lblError.Text = "No Service at this address!";
101          scf = null;
102        }
103        catch (Exception ex) {
104          //login failed
105          t.Abort();
106          this.Cursor = Cursors.Default;
107          MessageBox.Show(ex.Message);
108          lblError.Text = "Logon failed! Please restart console";
109        }
110      }
111        return false;
112    }
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        lblError.Text = "";
164        if (error == true) {
165          lblError.Text = "Establishing server connection failed.";
166        }
167      }
168    }
169
170    private void HiveServerConsole_KeyPress(object sender, KeyPressEventArgs e) {
171      if (e.KeyChar == (char)Keys.Return) {
172        BtnLogin_Click(sender, e);
173      }
174    }
175
176    private void btnCancel_Click(object sender, EventArgs e) {
177      Dispose();
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.