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 |
|
---|
22 | using System;
|
---|
23 | using System.Net;
|
---|
24 | using System.ServiceModel;
|
---|
25 | using System.Threading;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Hive.Contracts;
|
---|
28 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
29 | using HeuristicLab.Hive.Contracts.ResponseObjects;
|
---|
30 |
|
---|
31 | namespace 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 | if (IsValid()) {
|
---|
53 | this.Visible = false;
|
---|
54 | information = new HiveServerManagementConsole();
|
---|
55 | information.closeFormEvent += new closeForm(EnableForm);
|
---|
56 | information.Show();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// if the input is correct and the login-method returns a
|
---|
62 | /// valid response
|
---|
63 | /// </summary>
|
---|
64 | /// <returns></returns>
|
---|
65 | private bool IsValid() {
|
---|
66 | Thread t = new Thread(new ThreadStart(ShowWaitDlg));
|
---|
67 | if (IsFormValidated()) {
|
---|
68 | IServerConsoleFacade scf;
|
---|
69 | Response resp;
|
---|
70 | try {
|
---|
71 | lblError.Text = "Trying to logon...";
|
---|
72 | this.Cursor = Cursors.WaitCursor;
|
---|
73 | t.Start();
|
---|
74 |
|
---|
75 | ServiceLocator.Username = tbUserName.Text;
|
---|
76 | ServiceLocator.Password = tbPwd.Text;
|
---|
77 | scf = ServiceLocator.GetServerConsoleFacade();
|
---|
78 |
|
---|
79 | resp = scf.Login();
|
---|
80 | t.Abort();
|
---|
81 | this.Cursor = Cursors.Default;
|
---|
82 | if (resp.StatusMessage == ResponseStatus.Ok) return true;
|
---|
83 | lblError.Text = resp.StatusMessage.ToString();
|
---|
84 | MessageBox.Show("Wrong username or password");
|
---|
85 | }
|
---|
86 | catch (EndpointNotFoundException ene) {
|
---|
87 | t.Abort();
|
---|
88 | this.Cursor = Cursors.Default;
|
---|
89 | MessageBox.Show(ene.Message);
|
---|
90 | lblError.Text = "No Service at this address!";
|
---|
91 | scf = null;
|
---|
92 | }
|
---|
93 | catch (Exception ex) {
|
---|
94 | //login failed
|
---|
95 | t.Abort();
|
---|
96 | this.Cursor = Cursors.Default;
|
---|
97 | MessageBox.Show(ex.Message);
|
---|
98 | lblError.Text = "Logon failed! Please restart console";
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | private void ShowWaitDlg() {
|
---|
106 | LogonDlg dlg = new LogonDlg();
|
---|
107 | dlg.ShowDialog();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /// <summary>
|
---|
111 | /// Validates the form.
|
---|
112 | /// </summary>
|
---|
113 | /// <returns></returns>
|
---|
114 | private bool IsFormValidated() {
|
---|
115 | bool isValid = true;
|
---|
116 | if (String.IsNullOrEmpty(tbUserName.Text)) {
|
---|
117 | lblError.Text = "Please type in Username.";
|
---|
118 | isValid = false;
|
---|
119 | }
|
---|
120 | if (String.IsNullOrEmpty(tbPwd.Text)) {
|
---|
121 | lblError.Text = "Please type in Password.";
|
---|
122 | isValid = false;
|
---|
123 | }
|
---|
124 | return isValid;
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void EnableForm(bool cf, bool error) {
|
---|
128 | if (cf) {
|
---|
129 | this.Visible = true;
|
---|
130 | ServiceLocator.ShutDownFacade();
|
---|
131 | lblError.Text = "";
|
---|
132 | if (error == true) {
|
---|
133 | lblError.Text = "Establishing server connection failed.";
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | private void HiveServerConsole_KeyPress(object sender, KeyPressEventArgs e) {
|
---|
139 | if (e.KeyChar == (char)Keys.Return) {
|
---|
140 | BtnLogin_Click(sender, e);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void btnCancel_Click(object sender, EventArgs e) {
|
---|
145 | Dispose();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|