Skip to content

Commit 977a3ef

Browse files
committed
Socket, connection handling changes
Enums: Removed noresponse enum, replaced with empty and unrecognized. Socket: - Modified Disconnect sub to have more concise parameter, remove unnecessary stream disposal - Restructuring of core Query_Data subroutine. Moved or eliminated variable initialization, early sanity check for connection status, removed redundant newline in stream write, process response using .NET functions, move Enum parsing inline to subroutine, early check for null/empty response, distinct handling of good vs. bad results - Removed unused Watchdog code (essentially became null/empty case in Query subroutine) - Removed exception from Socket_Broken parameter UPS: - Stop variable update loop from calling Socket_Broken() - In multi-variable get subroutine, only recurse on VARNOTSUPPORTED or STALE errors, everything else should be thrown.
1 parent 81b1134 commit 977a3ef

File tree

3 files changed

+52
-81
lines changed

3 files changed

+52
-81
lines changed

WinNUT_V2/WinNUT-Client_Common/Common_Enums.vb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ End Enum
6161

6262
' Define possible responses according to NUT protcol v1.2
6363
Public Enum NUTResponse
64-
NORESPONSE
64+
EMPTY
65+
UNRECOGNIZED
6566
OK
6667
VAR
6768
ACCESSDENIED

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 48 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Public Class Nut_Socket
3535
''' </summary>
3636
Private streamInUse As Boolean
3737

38-
Public Event Socket_Broken(ex As NutException)
38+
Public Event Socket_Broken()
3939

4040
Public Sub New(Nut_Config As Nut_Parameter, ByRef logger As Logger)
4141
LogFile = logger
@@ -122,9 +122,10 @@ Public Class Nut_Socket
122122
''' <summary>
123123
''' Perform various functions necessary to disconnect the socket from the NUT server.
124124
''' </summary>
125-
''' <param name="forceful">Skip sending the LOGOUT command to the NUT server. Unknown effects.</param>
126-
Public Sub Disconnect(Optional forceful = False)
127-
If IsLoggedIn AndAlso Not forceful Then
125+
''' <param name="skipLogout">Do not send the LOGOUT command to the NUT server. Unknown effects.</param>
126+
Public Sub Disconnect(Optional skipLogout = False)
127+
If IsLoggedIn AndAlso Not skipLogout Then
128+
' TODO: Move to new subroutine.
128129
Query_Data("LOGOUT")
129130
End If
130131

@@ -138,47 +139,11 @@ Public Class Nut_Socket
138139
ReaderStream.Dispose()
139140
End If
140141

141-
If NutStream IsNot Nothing Then
142-
NutStream.Dispose()
143-
End If
144-
145142
If client IsNot Nothing Then
146143
client.Close()
147144
End If
148145
End Sub
149146

150-
''' <summary>
151-
''' Parse and enumerate a NUT protocol response.
152-
''' </summary>
153-
''' <param name="Data">The raw response given from a query.</param>
154-
''' <returns></returns>
155-
Private Function EnumResponse(Data As String) As NUTResponse
156-
Dim Response As NUTResponse
157-
' Remove hyphens to prepare for parsing.
158-
Dim SanitisedString = UCase(Data.Replace("-", String.Empty))
159-
' Break the response down so we can get specifics.
160-
Dim SplitString = SanitisedString.Split(" "c)
161-
162-
Select Case SplitString(0)
163-
Case "OK", "VAR", "DESC", "UPS"
164-
Response = NUTResponse.OK
165-
Case "BEGIN"
166-
Response = NUTResponse.BEGINLIST
167-
Case "END"
168-
Response = NUTResponse.ENDLIST
169-
Case "ERR"
170-
Response = DirectCast([Enum].Parse(GetType(NUTResponse), SplitString(1)), NUTResponse)
171-
Case "NETWORK", "1.0", "1.1", "1.2", "1.3"
172-
'In case of "VER" or "NETVER" Query
173-
Response = NUTResponse.OK
174-
Case Else
175-
' We don't recognize the response, throw an error.
176-
Response = NUTResponse.NORESPONSE
177-
'Throw New Exception("Unknown response from NUT server: " & Response)
178-
End Select
179-
Return Response
180-
End Function
181-
182147
''' <summary>
183148
''' Attempt to send a query to the NUT server, and do some basic parsing.
184149
''' </summary>
@@ -188,43 +153,59 @@ Public Class Nut_Socket
188153
''' call is in progress.</exception>
189154
''' <exception cref="NutException">Thrown when the NUT server returns an error or unexpected response.</exception>
190155
Function Query_Data(Query_Msg As String) As Transaction
191-
Dim Response As NUTResponse
192-
Dim DataResult As String
193-
Dim finalTransaction As Transaction
156+
If Not ConnectionStatus Then
157+
Throw New InvalidOperationException("Attempted to send query " & Query_Msg & " while disconnected.")
158+
End If
194159

195160
If streamInUse Then
196-
Throw New InvalidOperationException("Attempted to query " & Query_Msg & " while stream is in use.")
161+
Throw New InvalidOperationException("Attempted to send query " & Query_Msg & " while stream is in use.")
197162
End If
198163

199-
If ConnectionStatus Then
164+
Try
200165
streamInUse = True
166+
WriterStream.WriteLine(Query_Msg)
167+
WriterStream.Flush()
168+
Catch
169+
Throw
170+
Finally
171+
streamInUse = False
172+
End Try
201173

202-
Try
203-
WriterStream.WriteLine(Query_Msg & vbCr)
204-
WriterStream.Flush()
205-
Catch
206-
Throw
207-
Finally
208-
streamInUse = False
209-
End Try
174+
Dim responseEnum = NUTResponse.EMPTY
175+
Dim response = ReaderStream.ReadLine()
210176

211-
DataResult = Trim(ReaderStream.ReadLine())
212-
Response = EnumResponse(DataResult)
213-
finalTransaction = New Transaction(Query_Msg, DataResult, Response)
214-
215-
' Handle error conditions
216-
If DataResult = Nothing OrElse DataResult.StartsWith("ERR") Then
217-
' TODO: Does null dataresult really mean an error condition?
218-
' https://stackoverflow.com/a/6523010/530172
219-
'Disconnect(True, True)
220-
'RaiseEvent Socket_Broken(New NutException(Query_Msg, Nothing))
221-
Throw New NutException(finalTransaction)
222-
End If
177+
If String.IsNullOrEmpty(response) Then
178+
' End of stream reached, likely server terminated connection.
179+
Disconnect(True)
180+
RaiseEvent Socket_Broken()
223181
Else
224-
Throw New InvalidOperationException("Attempted to send query while disconnected.")
182+
Dim parseResponse = response.Trim().ToUpper().Split(" "c) ' TODO: Is Trim unnecessary?
183+
184+
Select Case parseResponse(0)
185+
Case "OK", "VAR", "DESC", "UPS"
186+
responseEnum = NUTResponse.OK
187+
Case "BEGIN"
188+
responseEnum = NUTResponse.BEGINLIST
189+
Case "END"
190+
responseEnum = NUTResponse.ENDLIST
191+
Case "NETWORK", "1.0", "1.1", "1.2", "1.3"
192+
'In case of "VER" or "NETVER" Query
193+
responseEnum = NUTResponse.OK
194+
Case "ERR"
195+
responseEnum = DirectCast([Enum].Parse(GetType(NUTResponse),
196+
parseResponse(1).Replace("-", String.Empty)), NUTResponse)
197+
Case Else
198+
responseEnum = NUTResponse.UNRECOGNIZED
199+
End Select
225200
End If
226201

227-
Return finalTransaction
202+
Dim transaction = New Transaction(Query_Msg, response, responseEnum)
203+
204+
If responseEnum = NUTResponse.OK OrElse responseEnum = NUTResponse.BEGINLIST OrElse responseEnum = NUTResponse.ENDLIST Then
205+
Return transaction
206+
End If
207+
208+
Throw New NutException(transaction)
228209
End Function
229210

230211
Public Function Query_List_Datas(Query_Msg As String) As List(Of UPS_List_Datas)
@@ -351,12 +332,4 @@ Public Class Nut_Socket
351332
Throw New NutException(Nut_Query)
352333
End If
353334
End Function
354-
355-
Private Sub Event_WatchDog(sender As Object, e As EventArgs)
356-
Dim Nut_Query = Query_Data("")
357-
If Nut_Query.ResponseType = NUTResponse.NORESPONSE Then
358-
Disconnect(True)
359-
RaiseEvent Socket_Broken(New NutException(Nut_Query))
360-
End If
361-
End Sub
362335
End Class

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ Public Class UPS_Device
429429
Catch Excep As Exception
430430
LogFile.LogTracing("Something went wrong in Retrieve_UPS_Datas:", LogLvl.LOG_ERROR, Me)
431431
LogFile.LogException(Excep, Me)
432-
Socket_Broken()
433432
End Try
434433
End Sub
435434

@@ -481,12 +480,10 @@ Public Class UPS_Device
481480
Continue For
482481
End If
483482
End If
484-
485483
Case Else
486-
LogFile.LogTracing("Error with " & varName & ", trying next", LogLvl.LOG_WARNING, Me)
487-
' Continue to next variable
488-
Continue For
484+
Throw
489485
End Select
486+
490487
Catch ex As Exception
491488
LogFile.LogTracing("Exception for variable " & varName & ": " & ex.Message & ", trying next", LogLvl.LOG_WARNING, Me)
492489
' Continue to next variable

0 commit comments

Comments
 (0)