Free cookie consent management tool by TermsFeed Policy Generator

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

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

#586: Added authorization components.

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