3
3
using PingCastle . PingCastleLicense ;
4
4
using PingCastle . Report ;
5
5
using PingCastle . Rules ;
6
+ using PingCastle . UserInterface ;
6
7
using System ;
7
8
using System . Collections . Generic ;
8
- using System . ComponentModel ;
9
9
using System . IO ;
10
10
using System . Text ;
11
11
using System . Xml ;
@@ -17,13 +17,15 @@ namespace PingCastle.Bot
17
17
18
18
public class Bot
19
19
{
20
+ private readonly IUserInterface _userIo = UserInterfaceFactory . GetUserInterface ( ) ;
21
+
20
22
public void Run ( string pipeName )
21
23
{
22
24
BotInputOutput input ;
23
25
bool stop = false ;
24
26
25
27
XmlSerializer xs = new XmlSerializer ( typeof ( BotInputOutput ) ) ;
26
- Console . WriteLine ( "Bot: hello" ) ;
28
+ _userIo . DisplayMessage ( "Bot: hello" ) ;
27
29
using ( var pipe = BotStream . OpenPipeStream ( pipeName ) )
28
30
{
29
31
while ( ! stop )
@@ -42,25 +44,24 @@ public void Run(string pipeName)
42
44
int r = pipe . Read ( data , read , count - read ) ;
43
45
if ( r == 0 )
44
46
{
45
- Console . WriteLine ( "Pipe shutdown" ) ;
47
+ _userIo . DisplayMessage ( "Pipe shutdown" ) ;
46
48
return ;
47
49
}
48
50
read += r ;
49
51
}
50
- Console . WriteLine ( "Bot: message received" ) ;
52
+ _userIo . DisplayMessage ( "Bot: message received" ) ;
51
53
using ( var ms = new MemoryStream ( data ) )
52
54
{
53
55
input = ( BotInputOutput ) xs . Deserialize ( ms ) ;
54
56
}
55
57
}
56
58
catch ( Exception ex )
57
59
{
58
- Console . WriteLine ( "Exception when reading the input " + ex . Message ) ;
59
- Console . WriteLine ( "StackTrace:" + ex . StackTrace ) ;
60
+ _userIo . DisplayError ( "Exception when reading the input " + ex . Message ) ;
61
+ _userIo . DisplayStackTrace ( "StackTrace:" + ex . StackTrace ) ;
60
62
return ;
61
63
}
62
64
63
-
64
65
BotInputOutput output ;
65
66
string order = GetItem ( input , "Command" ) ;
66
67
try
@@ -90,11 +91,11 @@ public void Run(string pipeName)
90
91
catch ( Exception ex )
91
92
{
92
93
output = ExceptionOutput ( "Exception during the job " + ex . Message , ex . StackTrace ) ;
93
- Console . WriteLine ( "Exception:" + ex . Message ) ;
94
- Console . WriteLine ( "StackTrace:" + ex . StackTrace ) ;
94
+ _userIo . DisplayMessage ( "Exception:" + ex . Message ) ;
95
+ _userIo . DisplayStackTrace ( "StackTrace:" + ex . StackTrace ) ;
95
96
}
96
97
97
- Console . WriteLine ( "Writing data" ) ;
98
+ _userIo . DisplayMessage ( "Writing data" ) ;
98
99
99
100
using ( var ms = new MemoryStream ( ) )
100
101
using ( XmlWriter writer = XmlWriter . Create ( ms ) )
@@ -105,11 +106,11 @@ public void Run(string pipeName)
105
106
var t = BitConverter . GetBytes ( ( int ) ms . Length ) ;
106
107
pipe . Write ( t , 0 , 4 ) ;
107
108
pipe . Write ( buffer , 0 , ( int ) ms . Length ) ;
108
- Console . WriteLine ( "Bot: message sent" ) ;
109
+ _userIo . DisplayMessage ( "Bot: message sent" ) ;
109
110
}
110
111
}
111
112
}
112
- Console . WriteLine ( "Exiting" ) ;
113
+ _userIo . DisplayMessage ( "Exiting" ) ;
113
114
}
114
115
115
116
private string GetItem ( BotInputOutput input , string key )
@@ -181,8 +182,8 @@ private BotInputOutput RunHealthCheck(BotInputOutput input)
181
182
}
182
183
catch ( Exception ex )
183
184
{
184
- Console . WriteLine ( "Exception:" + ex . Message ) ;
185
- Console . WriteLine ( "StackTrace:" + ex . StackTrace ) ;
185
+ _userIo . DisplayError ( "Exception:" + ex . Message ) ;
186
+ _userIo . DisplayStackTrace ( "StackTrace:" + ex . StackTrace ) ;
186
187
return ExceptionOutput ( "Exception during the healthcheck " + ex . Message , ex . StackTrace ) ;
187
188
}
188
189
}
@@ -195,9 +196,9 @@ private BotInputOutput ToHtml(BotInputOutput input)
195
196
using ( var ms = new MemoryStream ( UnicodeEncoding . UTF8 . GetBytes ( xml ) ) )
196
197
{
197
198
HealthcheckData healthcheckData = DataHelper < HealthcheckData > . LoadXml ( ms , "bot" , null ) ;
198
- var endUserReportGenerator = new ReportHealthCheckSingle ( ) ;
199
199
var license = LicenseCache . Instance . GetLicense ( ) ;
200
- var report = endUserReportGenerator . GenerateReportFile ( healthcheckData , license , healthcheckData . GetHumanReadableFileName ( ) ) ;
200
+ var endUserReportGenerator = new ReportHealthCheckSingle ( license ) ;
201
+ var report = endUserReportGenerator . GenerateReportFile ( healthcheckData , healthcheckData . GetHumanReadableFileName ( ) ) ;
201
202
202
203
var o = new BotInputOutput ( ) ;
203
204
o . Data = new List < BotData > ( ) ;
@@ -208,8 +209,8 @@ private BotInputOutput ToHtml(BotInputOutput input)
208
209
}
209
210
catch ( Exception ex )
210
211
{
211
- Console . WriteLine ( "Exception:" + ex . Message ) ;
212
- Console . WriteLine ( "StackTrace:" + ex . StackTrace ) ;
212
+ _userIo . DisplayError ( "Exception:" + ex . Message ) ;
213
+ _userIo . DisplayStackTrace ( "StackTrace:" + ex . StackTrace ) ;
213
214
return ExceptionOutput ( "Exception during the job " + ex . Message , ex . StackTrace ) ;
214
215
}
215
216
}
0 commit comments