Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2227 was 2118, checked in by aleitner, 15 years ago

change comments, delete manual login-data (#569)

File size: 5.5 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    }
47
48    private void tsmiExit_Click(object sender, EventArgs e) {
49      this.Close();
50    }
51
52    /// <summary>
53    /// When login button is clicked, the ManagementConsole
54    /// will be opened
55    /// </summary>
56    /// <param name="sender"></param>
57    /// <param name="e"></param>
58    private void BtnLogin_Click(object sender, EventArgs e) {
59      string newIp = tbIp.Text;
60      newIp = newIp.Replace(" ", "");
61
62      ServiceLocator.Address = newIp;
63      ServiceLocator.Port = this.tbPort.Text;
64
65      if (IsValid()) {
66        this.Visible = false;
67        information = new HiveServerManagementConsole();
68        information.closeFormEvent += new closeForm(EnableForm);
69        information.Show();
70      }
71    }
72
73    /// <summary>
74    /// if the input is correct and the login-method returns a
75    /// valid response
76    /// </summary>
77    /// <returns></returns>
78    private bool IsValid() {
79      Thread t = new Thread(new ThreadStart(ShowWaitDlg));
80      if (IsFormValidated()) {
81        IPAddress ipAdress = IPAddress.Parse(tbIp.Text);
82        int port = int.Parse(tbPort.Text);
83        ServiceLocator.Address = tbIp.Text.Replace(" ", "");
84        ServiceLocator.Port = tbPort.Text;
85        IServerConsoleFacade scf = ServiceLocator.GetServerConsoleFacade();
86        Response resp;
87        try {
88          lblError.Text = "Trying to logon...";
89          this.Cursor = Cursors.WaitCursor;
90          t.Start();
91          resp = scf.Login(tbUserName.Text, tbPwd.Text);
92          t.Abort();
93          this.Cursor = Cursors.Default;
94          if (resp.Success == true) return true;
95          lblError.Text = resp.StatusMessage;
96          MessageBox.Show("Wrong username or password");
97        }
98        catch (EndpointNotFoundException ene) {
99          t.Abort();
100          this.Cursor = Cursors.Default;
101          MessageBox.Show(ene.Message);
102          lblError.Text = "No Service at this address!";
103          scf = null;
104        }
105        catch (Exception ex) {
106          //login failed
107          t.Abort();
108          this.Cursor = Cursors.Default;
109          MessageBox.Show(ex.Message);
110          lblError.Text = "Logon failed! Please restart console";
111        }
112      }
113        return false;
114    }
115
116
117    private void ShowWaitDlg() {
118      LogonDlg dlg = new LogonDlg();
119      dlg.ShowDialog();
120    }
121
122    /// <summary>
123    /// Validates the form.
124    /// </summary>
125    /// <returns></returns>
126    private bool IsFormValidated() {
127      bool isValid = true;
128      if (String.IsNullOrEmpty(tbUserName.Text)) {
129        lblError.Text = "Please type in Username.";
130        isValid = false;
131      }
132      if (String.IsNullOrEmpty(tbPwd.Text)) {
133        lblError.Text = "Please type in Password.";
134        isValid = false;
135      }
136      if (String.IsNullOrEmpty(tbIp.Text)) {
137        lblError.Text = "Please type in Port.";
138        isValid = false;
139      }
140      if (String.IsNullOrEmpty(tbPort.Text)) {
141        lblError.Text = "Please type in IP-Address.";
142        isValid = false;
143      }
144      try {
145        int.Parse(tbPort.Text);
146      }
147      catch (Exception ex) {
148        isValid = false;
149        lblError.Text = "Please verify entered Port.";
150      }
151      try {
152        IPAddress.Parse(tbIp.Text);
153      }
154      catch (Exception ex) {
155        isValid = false;
156        lblError.Text = "Please verify entered IP address.";
157      }
158      return isValid;
159    }
160
161    private void EnableForm(bool cf, bool error) {
162      if (cf) {
163        this.Visible = true;
164        ServiceLocator.ShutDownFacade();
165        lblError.Text = "";
166        if (error == true) {
167          lblError.Text = "Establishing server connection failed.";
168        }
169      }
170    }
171
172    private void HiveServerConsole_KeyPress(object sender, KeyPressEventArgs e) {
173      if (e.KeyChar == (char)Keys.Return) {
174        BtnLogin_Click(sender, e);
175      }
176    }
177
178    private void btnCancel_Click(object sender, EventArgs e) {
179      Dispose();
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.