Skip to content

Commit eae2a4d

Browse files
committed
Connection streamlining and debugging
- Socket ConnectionStatus is derived from Socket directly - Bring Logger object into Socket - Create_Socket is turned into a subroutine; utilizing Events and Exceptions for communication - Attempted to make AuthLogin failures non-critical - Removed ReConnect subroutine from UPS_Device - Additional bits of logging sprinkled everywhere - Tried implementing Connection event for WinNUT to prevent hanging main thread, needs work - No longer attempt to connect immediatly at start up - Breakout UPS connection login into a seperate subroutine - Tweak disconnection subroutine - Reconnect menu now disconnects and connects again - Uncomment part of prefs_changed, now disconnect and connect UPS again.
1 parent 515e1c5 commit eae2a4d

File tree

4 files changed

+219
-170
lines changed

4 files changed

+219
-170
lines changed

WinNUT_V2/WinNUT-Client_Common/Common_Classes.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Public Class Nut_Parameter
5858
''' <returns></returns>
5959
Public Overrides Function ToString() As String
6060
Return String.Format("{0}@{1}:{2}, Name: {3}" & If(AutoReconnect, " [AutoReconnect]", Nothing),
61-
Login, Host, Port, AutoReconnect)
61+
Login, Host, Port, UPSName, AutoReconnect)
6262
' Return MyBase.ToString())
6363
End Function
6464
End Class

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ Imports System.IO
1515
Imports System.Windows.Forms
1616

1717
Public Class Nut_Socket
18+
#Region "Properties"
19+
Public ReadOnly Property ConnectionStatus
20+
Get
21+
If NutSocket IsNot Nothing Then
22+
Return NutSocket.Connected
23+
Else
24+
Return False
25+
End If
26+
End Get
27+
End Property
28+
#End Region
29+
30+
Private LogFile As Logger
1831
'Socket Variables
1932
Private NutSocket As Socket
2033
Private NutTCP As TcpClient
@@ -27,8 +40,6 @@ Public Class Nut_Socket
2740
Private Nut_Ver As String
2841
Private Net_Ver As String
2942

30-
Private ConnectionStatus As Boolean = False
31-
3243
Private Nut_Config As Nut_Parameter
3344

3445
Public Auth_Success As Boolean = False
@@ -44,8 +55,8 @@ Public Class Nut_Socket
4455
Public Event Socket_Broken()
4556
Public Event Socket_Deconnected()
4657

47-
Public Sub New(ByVal Nut_Config As Nut_Parameter)
48-
58+
Public Sub New(ByVal Nut_Config As Nut_Parameter, ByRef logger As Logger)
59+
LogFile = logger
4960
With Me.WatchDog
5061
.Interval = 1000
5162
.Enabled = False
@@ -88,6 +99,7 @@ Public Class Nut_Socket
8899
Public Sub Update_Config(ByVal New_Nut_Config As Nut_Parameter)
89100
Me.Nut_Config = New_Nut_Config
90101
End Sub
102+
91103
Public Function Connect() As Boolean
92104
Try
93105
'TODO: Use LIST UPS protocol command to get valid UPSs.
@@ -97,24 +109,26 @@ Public Class Nut_Socket
97109
Dim Password = Nut_Config.Password
98110

99111
If Not String.IsNullOrEmpty(Host) And Not IsNothing(Port) Then
100-
If Not Create_Socket(Host, Port) Then
101-
' Don't duplicate/override Create_Socket's error throwing functionality.
102-
' Throw New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR)
103-
Disconnect()
104-
Else
105-
If Not AuthLogin(Login, Password) Then
106-
Throw New Nut_Exception(Nut_Exception_Value.INVALID_AUTH_DATA)
107-
End If
112+
Create_Socket(Host, Port)
113+
114+
If ConnectionStatus Then
115+
AuthLogin(Login, Password)
116+
'If Not AuthLogin(Login, Password) Then
117+
' ' Throw New Nut_Exception(Nut_Exception_Value.INVALID_AUTH_DATA)
118+
' RaiseEvent OnNUTException()
119+
'End If
108120
Dim Nut_Query = Query_Data("VER")
109121

110122
If Nut_Query.Response = NUTResponse.OK Then
111-
Me.Nut_Ver = (Nut_Query.Data.Split(" "c))(4)
123+
Nut_Ver = (Nut_Query.Data.Split(" "c))(4)
112124
End If
113125
Nut_Query = Query_Data("NETVER")
114126

115127
If Nut_Query.Response = NUTResponse.OK Then
116-
Me.Net_Ver = Nut_Query.Data
128+
Net_Ver = Nut_Query.Data
117129
End If
130+
131+
LogFile.LogTracing(String.Format("NUT server reports VER: {0} NETVER: {1}", Nut_Ver, Net_Ver), LogLvl.LOG_NOTICE, Me)
118132
Return True
119133
End If
120134
End If
@@ -131,21 +145,23 @@ Public Class Nut_Socket
131145
Return False
132146
End Function
133147

134-
Private Function Create_Socket(ByVal Host As String, ByVal Port As Integer) As Boolean
148+
Private Sub Create_Socket(ByVal Host As String, ByVal Port As Integer)
135149
Try
136150
' NutSocket = New Socket(AddressFamily.InterNetwork, ProtocolType.IP)
137151
NutSocket = New Socket(SocketType.Stream, ProtocolType.IP)
152+
LogFile.LogTracing(String.Format("Attempting TCP socket connection to {0}:{1}...", Host, Port), LogLvl.LOG_NOTICE, Me)
138153
NutTCP = New TcpClient(Host, Port)
139154
NutStream = NutTCP.GetStream
140155
ReaderStream = New StreamReader(NutStream)
141156
WriterStream = New StreamWriter(NutStream)
142-
ConnectionStatus = True
157+
' ConnectionStatus = True
158+
LogFile.LogTracing(String.Format("Connection established and streams ready for {0}:{1}", Host, Port), LogLvl.LOG_NOTICE, Me)
143159
Catch Excep As Exception
160+
Disconnect(True)
144161
RaiseEvent OnNUTException(New Nut_Exception(Nut_Exception_Value.CONNECT_ERROR, Excep.Message), LogLvl.LOG_ERROR, Me)
145-
Me.ConnectionStatus = False
146162
End Try
147-
Return Me.ConnectionStatus
148-
End Function
163+
End Sub
164+
149165
Public Function Query_Data(Query_Msg As String) As (Data As String, Response As NUTResponse)
150166
Dim Response As NUTResponse = NUTResponse.NORESPONSE
151167
Dim DataResult As String = ""
@@ -338,9 +354,11 @@ Public Class Nut_Socket
338354
End Select
339355
Return Response
340356
End Function
341-
Private Function AuthLogin(ByVal Login As String, ByVal Password As String) As Boolean
357+
358+
Private Sub AuthLogin(Login As String, Password As String)
342359
Try
343-
Me.Auth_Success = False
360+
LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me)
361+
Auth_Success = False
344362
If Not String.IsNullOrEmpty(Login) AndAlso String.IsNullOrEmpty(Password) Then
345363
Dim Nut_Query = Query_Data("USERNAME " & Login)
346364

@@ -366,23 +384,27 @@ Public Class Nut_Socket
366384
End If
367385
End If
368386
End If
369-
Me.Auth_Success = True
370-
Return Me.Auth_Success
387+
388+
LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me)
389+
Auth_Success = True
390+
' Return Me.Auth_Success
391+
Catch nutEx As Nut_Exception
392+
' Auth issues aren't necessarily fatal - some interaction can still occur unauthenticated.
393+
RaiseEvent OnNUTException(nutEx, LogLvl.LOG_ERROR, Me)
394+
' Auth_Success = False
395+
' Disconnect(True)
371396
Catch Excep As Exception
372397
RaiseEvent OnError(Excep, LogLvl.LOG_ERROR, Me)
373-
Me.Disconnect()
374-
Return False
398+
Disconnect(True)
399+
' Auth_Success = False
375400
End Try
376-
End Function
401+
End Sub
377402

378403
Private Sub Event_WatchDog(sender As Object, e As EventArgs)
379404
Dim Nut_Query = Query_Data("")
380405
If Nut_Query.Response = NUTResponse.NORESPONSE Then
381-
Me.ConnectionStatus = False
382406
Disconnect(True)
383407
RaiseEvent Socket_Broken()
384-
ElseIf Nut_Query.Response = NUTResponse.UNKNOWNCOMMAND Then
385-
Me.ConnectionStatus = True
386408
End If
387409
End Sub
388410

@@ -408,10 +430,15 @@ Public Class Nut_Socket
408430
NutSocket.Close()
409431
End If
410432
Catch Excep As Exception
433+
LogFile.LogTracing("Error encountered while shutting down socket: " & vbNewLine & Excep.ToString(),
434+
LogLvl.LOG_ERROR, Me)
411435
End Try
412-
Me.ConnectionStatus = False
413436
End Sub
414437

438+
''' <summary>
439+
''' Gracefully disconnect the socket from the NUT server.
440+
''' </summary>
441+
''' <param name="ForceDisconnect">Skip raising the <see cref="Socket_Deconnected"/> event if true.</param>
415442
Public Sub Disconnect(Optional ByVal ForceDisconnect = False)
416443
Query_Data("LOGOUT")
417444
Close_Socket()

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Public Class UPS_Device
1515
Private ciClone As System.Globalization.CultureInfo
1616
Private Const CosPhi As Double = 0.6
1717

18-
Private Nut_Config As Nut_Parameter
18+
Public Nut_Config As Nut_Parameter
1919

2020
Public UPS_Datas As New UPS_Datas
2121
Public WithEvents Nut_Socket As Nut_Socket
@@ -111,7 +111,7 @@ Public Class UPS_Device
111111
Me.Nut_Config = Nut_Config
112112
Me.ciClone = CType(CultureInfo.InvariantCulture.Clone(), CultureInfo)
113113
Me.ciClone.NumberFormat.NumberDecimalSeparator = "."
114-
Me.Nut_Socket = New Nut_Socket(Me.Nut_Config)
114+
Me.Nut_Socket = New Nut_Socket(Me.Nut_Config, LogFile)
115115
With Me.Reconnect_Nut
116116
.Interval = 30000
117117
.Enabled = False
@@ -123,14 +123,17 @@ Public Class UPS_Device
123123
AddHandler .Tick, AddressOf Event_WatchDog
124124
End With
125125
AddHandler Nut_Socket.Socket_Deconnected, AddressOf Socket_Deconnected
126-
Connect_UPS()
126+
' Connect_UPS()
127127
End Sub
128+
128129
Public Sub Connect_UPS()
129-
Dim UPSName = Me.Nut_Config.UPSName
130+
' Dim UPSName = Me.Nut_Config.UPSName
131+
LogFile.LogTracing("Beginning connection: " & Nut_Config.ToString(), LogLvl.LOG_DEBUG, Me)
132+
130133
If Me.Nut_Socket.Connect() And Me.Nut_Socket.IsConnected Then
131134
LogFile.LogTracing("TCP Socket Created", LogLvl.LOG_NOTICE, Me)
132135
Me.Socket_Status = True
133-
If Nut_Socket.IsKnownUPS(UPSName) Then
136+
If Nut_Socket.IsKnownUPS(Nut_Config.UPSName) Then
134137
Me.UPS_Datas = GetUPSProductInfo()
135138
Init_Constant(Nut_Socket)
136139
RaiseEvent Connected()
@@ -146,11 +149,13 @@ Public Class UPS_Device
146149
' End If
147150
End If
148151
End Sub
149-
Public Sub ReConnect()
150-
If Not Me.IsConnected Then
151-
Nut_Socket.Connect()
152-
End If
153-
End Sub
152+
153+
'Public Sub ReConnect()
154+
' If Not Me.IsConnected Then
155+
' Nut_Socket.Connect()
156+
' End If
157+
'End Sub
158+
154159
Private Function GetUPSProductInfo() As UPS_Datas
155160
Dim UDatas As New UPS_Datas
156161
Dim UPSName = Me.Nut_Config.UPSName

0 commit comments

Comments
 (0)