11
11
12
12
' Class dealing only with the management of the communication socket with the Nut server
13
13
Imports System.IO
14
+ Imports System.Net
14
15
Imports System.Net.Sockets
15
16
16
17
Public Class Nut_Socket
17
18
18
19
# Region "Properties"
19
20
Public ReadOnly Property ConnectionStatus As Boolean
20
21
Get
21
- If NutSocket IsNot Nothing Then
22
- Return NutSocket.Connected
23
- Else
24
- Return False
25
- End If
22
+ Return client.Connected
26
23
End Get
27
24
End Property
28
25
@@ -58,8 +55,7 @@ Public Class Nut_Socket
58
55
Private NutConfig As Nut_Parameter
59
56
60
57
'Socket Variables
61
- Private NutSocket As Socket
62
- Private NutTCP As TcpClient
58
+ Private client As New TcpClient
63
59
Private NutStream As NetworkStream
64
60
Private ReaderStream As StreamReader
65
61
Private WriterStream As StreamWriter
@@ -71,11 +67,6 @@ Public Class Nut_Socket
71
67
72
68
Public Event Socket_Broken(ex As NutException)
73
69
74
- ''' <summary>
75
- ''' Socket was disconnected as a part of normal operations.
76
- ''' </summary>
77
- Public Event SocketDisconnected()
78
-
79
70
Public Sub New (Nut_Config As Nut_Parameter, ByRef logger As Logger)
80
71
LogFile = logger
81
72
NutConfig = Nut_Config
@@ -93,67 +84,53 @@ Public Class Nut_Socket
93
84
End If
94
85
95
86
Try
96
- ' NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
97
- NutSocket = New Socket(SocketType.Stream, ProtocolType.IP)
98
87
LogFile.LogTracing( String .Format( "Attempting TCP socket connection to {0}:{1}..." , Host, Port), LogLvl.LOG_NOTICE, Me )
99
- NutSocket.Connect(Host, Port)
100
- NutTCP = New TcpClient (Host, Port)
101
- NutStream = NutTCP .GetStream
88
+
89
+ client.Connect (Host, Port)
90
+ NutStream = client .GetStream()
102
91
ReaderStream = New StreamReader(NutStream)
103
92
WriterStream = New StreamWriter(NutStream)
104
- LogFile.LogTracing( String .Format( "Connection established and streams ready for {0}:{1}" , Host, Port), LogLvl.LOG_NOTICE, Me )
105
93
106
- ' Something went wrong - cleanup and pass along error.
94
+ LogFile.LogTracing( "Connection established and streams ready." , LogLvl.LOG_NOTICE, Me )
95
+
107
96
Catch Excep As Exception
108
97
Disconnect( True )
109
98
Throw ' Pass exception on up to UPS
110
99
End Try
111
100
112
- If ConnectionStatus Then
113
- Try
114
- AuthLogin(Login, Password)
115
- Catch ex As NutException
116
- ' TODO: Make friendly message string for user.
117
- LogFile.LogTracing( "Error while attempting to log in: " & ex.Message, LogLvl.LOG_ERROR, Me )
118
- End Try
101
+ Dim Nut_Query = Query_Data( "VER" )
119
102
120
- Dim Nut_Query = Query_Data( "VER" )
121
-
122
- If Nut_Query.ResponseType = NUTResponse.OK Then
123
- Nut_Ver = (Nut_Query.RawResponse.Split( " "c ))( 4 )
124
- End If
125
- Nut_Query = Query_Data( "NETVER" )
126
-
127
- If Nut_Query.ResponseType = NUTResponse.OK Then
128
- Net_Ver = Nut_Query.RawResponse
129
- End If
103
+ If Nut_Query.ResponseType = NUTResponse.OK Then
104
+ Nut_Ver = (Nut_Query.RawResponse.Split( " "c ))( 4 )
105
+ End If
106
+ Nut_Query = Query_Data( "NETVER" )
130
107
131
- LogFile.LogTracing( String .Format( "NUT server reports VER: {0} NETVER: {1}" , Nut_Ver, Net_Ver), LogLvl.LOG_NOTICE, Me )
108
+ If Nut_Query.ResponseType = NUTResponse.OK Then
109
+ Net_Ver = Nut_Query.RawResponse
132
110
End If
111
+
112
+ LogFile.LogTracing( String .Format( "NUT server reports VER: {0} NETVER: {1}" , Nut_Ver, Net_Ver), LogLvl.LOG_NOTICE, Me )
133
113
End Sub
134
114
135
- ''' <summary>
136
- ''' Register with the UPSd server as being dependant on it for power.
137
- ''' </summary>
138
- ''' <param name="Login"></param>
139
- ''' <param name="Password"></param>
140
- ''' <exception cref="NutException">A protocol error was encountered while trying to authenticate.</exception>
141
- Private Sub AuthLogin(Login As String , Password As String )
115
+ Public Sub Login()
142
116
If _isLoggedIn Then
143
117
Throw New InvalidOperationException( "Attempted to login when already logged in." )
144
118
End If
145
119
146
- LogFile.LogTracing( "Attempting authentication..." , LogLvl.LOG_NOTICE, Me )
120
+ LogFile.LogTracing( String .Format( "Logging in to UPS [{0}] as user [{1}] ({2})..." ,
121
+ NutConfig.UPSName, NutConfig.Login,
122
+ If ( String .IsNullOrEmpty(NutConfig.Password),
123
+ "NO Password" , "Password provided" )), LogLvl.LOG_NOTICE, Me )
147
124
148
- If Not String .IsNullOrEmpty(Login) Then
149
- Query_Data( "USERNAME " & Login)
125
+ If Not String .IsNullOrEmpty(NutConfig. Login) Then
126
+ Query_Data( "USERNAME " & NutConfig. Login)
150
127
151
- If Not String .IsNullOrEmpty(Password) Then
152
- Query_Data( "PASSWORD " & Password)
128
+ If Not String .IsNullOrEmpty(NutConfig. Password) Then
129
+ Query_Data( "PASSWORD " & NutConfig. Password)
153
130
End If
154
131
End If
155
132
156
- Query_Data( "LOGIN" )
133
+ Query_Data( "LOGIN " & NutConfig.UPSName )
157
134
_isLoggedIn = True
158
135
LogFile.LogTracing( "Authenticated successfully." , LogLvl.LOG_NOTICE, Me )
159
136
End Sub
@@ -163,12 +140,29 @@ Public Class Nut_Socket
163
140
''' </summary>
164
141
''' <param name="forceful">Skip sending the LOGOUT command to the NUT server. Unknown effects.</param>
165
142
Public Sub Disconnect( Optional forceful = False )
166
- If Not forceful AndAlso IsConnected AndAlso IsLoggedIn Then
167
- Query_Data( "LOGOUT" )
168
- End If
143
+ If IsConnected Then
144
+ If IsLoggedIn AndAlso Not forceful Then
145
+ Query_Data( "LOGOUT" )
146
+ End If
169
147
170
- Close_Socket()
171
- RaiseEvent SocketDisconnected()
148
+ If WriterStream IsNot Nothing Then
149
+ WriterStream.Close()
150
+ End If
151
+
152
+ If ReaderStream IsNot Nothing Then
153
+ ReaderStream.Close()
154
+ End If
155
+
156
+ If NutStream IsNot Nothing Then
157
+ NutStream.Close()
158
+ End If
159
+
160
+ If client IsNot Nothing Then
161
+ client.Close()
162
+ End If
163
+ Else
164
+ Throw New InvalidOperationException( "NUT Socket is already disconnected." )
165
+ End If
172
166
End Sub
173
167
174
168
''' <summary>
@@ -383,31 +377,4 @@ Public Class Nut_Socket
383
377
RaiseEvent Socket_Broken( New NutException(Nut_Query))
384
378
End If
385
379
End Sub
386
-
387
- Private Sub Close_Socket()
388
- Try
389
- If WriterStream IsNot Nothing Then
390
- WriterStream.Close()
391
- End If
392
-
393
- If ReaderStream IsNot Nothing Then
394
- ReaderStream.Close()
395
- End If
396
-
397
- If NutStream IsNot Nothing Then
398
- NutStream.Close()
399
- End If
400
-
401
- If NutTCP IsNot Nothing Then
402
- NutTCP.Close()
403
- End If
404
-
405
- If NutSocket IsNot Nothing Then
406
- NutSocket.Close()
407
- End If
408
- Catch Excep As Exception
409
- LogFile.LogTracing( "Error encountered while shutting down socket: " & vbNewLine & Excep.ToString(),
410
- LogLvl.LOG_ERROR, Me )
411
- End Try
412
- End Sub
413
380
End Class
0 commit comments