Skip to content

Commit f0991d9

Browse files
authored
Merge pull request #88 from nutdotnet/80-accessdenied-crash
Improvements to Login subroutine Closing to merge changes and continue broader efforts towards #79 and #80 per PR #86 .
2 parents 616af20 + 8446ac6 commit f0991d9

File tree

2 files changed

+60
-91
lines changed

2 files changed

+60
-91
lines changed

Diff for: WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

+39-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Imports System.IO
1616
Public Class Nut_Socket
1717

1818
#Region "Properties"
19-
2019
Public ReadOnly Property ConnectionStatus As Boolean
2120
Get
2221
If NutSocket IsNot Nothing Then
@@ -33,6 +32,13 @@ Public Class Nut_Socket
3332
End Get
3433
End Property
3534

35+
Private _isLoggedIn As Boolean = False
36+
Public ReadOnly Property IsLoggedIn() As Boolean
37+
Get
38+
Return _isLoggedIn
39+
End Get
40+
End Property
41+
3642
Private Nut_Ver As String
3743
Public ReadOnly Property Nut_Version() As String
3844
Get
@@ -46,7 +52,6 @@ Public Class Nut_Socket
4652
Return Net_Ver
4753
End Get
4854
End Property
49-
5055
#End Region
5156

5257
Private LogFile As Logger
@@ -64,10 +69,6 @@ Public Class Nut_Socket
6469
''' </summary>
6570
Private streamInUse As Boolean
6671

67-
68-
Public Auth_Success As Boolean = False
69-
' Private ReadOnly WatchDog As New Timer
70-
7172
Public Event Socket_Broken(ex As NutException)
7273

7374
''' <summary>
@@ -115,7 +116,12 @@ Public Class Nut_Socket
115116
End Try
116117

117118
If ConnectionStatus Then
118-
AuthLogin(Login, Password)
119+
Try
120+
AuthLogin(Login, Password)
121+
Catch ex As NutException
122+
' TODO: Make friendly message string for user.
123+
LogFile.LogTracing("Error while attempting to log in: " & ex.Message, LogLvl.LOG_ERROR, Me)
124+
End Try
119125

120126
Dim Nut_Query = Query_Data("VER")
121127

@@ -132,6 +138,32 @@ Public Class Nut_Socket
132138
End If
133139
End Sub
134140

141+
''' <summary>
142+
''' Register with the UPSd server as being dependant on it for power.
143+
''' </summary>
144+
''' <param name="Login"></param>
145+
''' <param name="Password"></param>
146+
''' <exception cref="NutException">A protocol error was encountered while trying to authenticate.</exception>
147+
Private Sub AuthLogin(Login As String, Password As String)
148+
If _isLoggedIn Then
149+
Throw New InvalidOperationException("Attempted to login when already logged in.")
150+
End If
151+
152+
LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me)
153+
154+
If Not String.IsNullOrEmpty(Login) Then
155+
Query_Data("USERNAME " & Login)
156+
157+
If Not String.IsNullOrEmpty(Password) Then
158+
Query_Data("PASSWORD " & Password)
159+
End If
160+
End If
161+
162+
Query_Data("LOGIN")
163+
_isLoggedIn = True
164+
LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me)
165+
End Sub
166+
135167
''' <summary>
136168
''' Perform various functions necessary to disconnect the socket from the NUT server.
137169
''' </summary>
@@ -356,27 +388,6 @@ Public Class Nut_Socket
356388
End If
357389
End Function
358390

359-
Private Sub AuthLogin(Login As String, Password As String)
360-
LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me)
361-
Auth_Success = False
362-
If Not String.IsNullOrEmpty(Login) AndAlso String.IsNullOrEmpty(Password) Then
363-
Dim Nut_Query = Query_Data("USERNAME " & Login)
364-
365-
If Nut_Query.ResponseType <> NUTResponse.OK Then
366-
Throw New NutException(Nut_Query)
367-
End If
368-
369-
Nut_Query = Query_Data("PASSWORD " & Password)
370-
371-
If Nut_Query.ResponseType <> NUTResponse.OK Then
372-
Throw New NutException(Nut_Query)
373-
End If
374-
End If
375-
376-
LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me)
377-
Auth_Success = True
378-
End Sub
379-
380391
Private Sub Event_WatchDog(sender As Object, e As EventArgs)
381392
Dim Nut_Query = Query_Data("")
382393
If Nut_Query.ResponseType = NUTResponse.NORESPONSE Then

Diff for: WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

+21-63
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Public Class UPS_Device
2727

2828
Public ReadOnly Property IsAuthenticated As Boolean
2929
Get
30-
Return Nut_Socket.Auth_Success
30+
Return Nut_Socket.IsLoggedIn
3131
End Get
3232
End Property
3333

@@ -61,6 +61,26 @@ Public Class UPS_Device
6161
End Property
6262

6363
#End Region
64+
65+
#Region "Events"
66+
67+
' Public Event Unknown_UPS()
68+
Public Event DataUpdated()
69+
Public Event Connected(sender As UPS_Device)
70+
Public Event ReConnected(sender As UPS_Device)
71+
' Notify that the connection was closed gracefully.
72+
Public Event Disconnected()
73+
' Notify of an unexpectedly lost connection (??)
74+
Public Event Lost_Connect()
75+
' Error encountered when trying to connect.
76+
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
77+
Public Event EncounteredNUTException(ex As NutException, sender As Object)
78+
Public Event New_Retry()
79+
' Public Event Shutdown_Condition()
80+
' Public Event Stop_Shutdown()
81+
82+
#End Region
83+
6484
Private Const CosPhi As Double = 0.6
6585
' How many milliseconds to wait before the Reconnect routine tries again.
6686
Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000
@@ -81,73 +101,11 @@ Public Class UPS_Device
81101
Public Retry As Integer = 0
82102
Public MaxRetry As Integer = 30
83103
Private WithEvents Reconnect_Nut As New System.Windows.Forms.Timer
84-
' Private ReadOnly WatchDog As New System.Windows.Forms.Timer
85-
' Private Socket_Status As Boolean = False
86104

87105

88106

89107

90108
Private LogFile As Logger
91-
'Private ConnectionStatus As Boolean = False
92-
'Private Server As String
93-
'Private Port As Integer
94-
'Private UPSName As String
95-
'Private Delay As Integer
96-
'Private Login As String
97-
'Private Password As String
98-
'Private Mfr As String
99-
'Private Model As String
100-
'Private Serial As String
101-
'Private Firmware As String
102-
'Private BattCh As Double
103-
'Private BattV As Double
104-
'Private BattRuntime As Double
105-
'Private BattCapacity As Double
106-
'Private PowerFreq As Double
107-
'Private InputV As Double
108-
'Private OutputV As Double
109-
'Private Load As Double
110-
'Private Status As String
111-
'Private OutPower As Double
112-
'Private InputA As Double
113-
'Private Low_Batt As Integer
114-
'Private Low_Backup As Integer
115-
'Private LConnect As Boolean = False
116-
'Private AReconnect As Boolean = False
117-
'Private MaxRetry As Integer = 30
118-
'Private Retry As Integer = 0
119-
'Private ErrorStatus As Boolean = False
120-
'Private ErrorMsg As String = ""
121-
'Private Update_Nut As New System.Windows.Forms.Timer
122-
'Private Reconnect_Nut As New System.Windows.Forms.Timer
123-
'Private NutSocket As System.Net.Sockets.Socket
124-
'Private NutTCP As System.Net.Sockets.TcpClient
125-
'Private NutStream As System.Net.Sockets.NetworkStream
126-
'Private ReaderStream As System.IO.StreamReader
127-
'Private WriterStream As System.IO.StreamWriter
128-
'Private Follow_FSD As Boolean = False
129-
'Private Unknown_UPS_Name As Boolean = False
130-
'Private Invalid_Data As Boolean = False
131-
'Private Invalid_Auth_Data As Boolean = False
132-
133-
#Region "Properties"
134-
135-
#End Region
136-
137-
' Public Event Unknown_UPS()
138-
Public Event DataUpdated()
139-
Public Event Connected(sender As UPS_Device)
140-
Public Event ReConnected(sender As UPS_Device)
141-
' Notify that the connection was closed gracefully.
142-
Public Event Disconnected()
143-
' Notify of an unexpectedly lost connection (??)
144-
Public Event Lost_Connect()
145-
' Error encountered when trying to connect.
146-
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
147-
Public Event EncounteredNUTException(ex As NutException, sender As Object)
148-
Public Event New_Retry()
149-
' Public Event Shutdown_Condition()
150-
' Public Event Stop_Shutdown()
151109

152110
''' <summary>
153111
''' Raise an event when a status code is added to the UPS that wasn't there before.

0 commit comments

Comments
 (0)