Skip to content

Commit 77dc156

Browse files
committed
Better connection error handling
- NutSocket should not try to gracefully disconnect if it isn't already connected - Also made NutSocket less chatty. Upper-level classes handle that. - Updated ConnectionError event in UPS to include the UPS object. - Propagated connection handling changes to WinNUT.vb - Removed some commented out code from the Globals file.
1 parent 46d6ad1 commit 77dc156

File tree

4 files changed

+19
-66
lines changed

4 files changed

+19
-66
lines changed

WinNUT_V2/WinNUT-Client/WinNUT.vb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ Public Class WinNUT
299299

300300
Private Sub UPS_Connect()
301301
Dim Nut_Config As Nut_Parameter
302-
' LogFile.LogTracing("Client UPS_Connect subroutine beginning.", LogLvl.LOG_NOTICE, Me)
302+
LogFile.LogTracing("Client UPS_Connect subroutine beginning.", LogLvl.LOG_NOTICE, Me)
303303

304304
Nut_Config = New Nut_Parameter(Arr_Reg_Key.Item("ServerAddress"),
305305
Arr_Reg_Key.Item("Port"),
@@ -326,20 +326,21 @@ Public Class WinNUT
326326
' ' .Enabled = True
327327
'End With
328328

329-
If Not (UPS_Device.IsConnected And UPS_Device.IsAuthenticated) Then
330-
LogFile.LogTracing(String.Format("Something went wrong connecting to UPS {0}. IsConnected: {1}, IsAuthenticated: {2}",
331-
upsConf.UPSName, UPS_Device.IsConnected, UPS_Device.IsAuthenticated), LogLvl.LOG_ERROR, Me,
332-
String.Format(StrLog.Item(AppResxStr.STR_LOG_CON_FAILED), upsConf.Host, upsConf.Port, "Connection Error"))
333-
UPSDisconnect()
334-
Else
335-
Menu_UPS_Var.Enabled = True
336-
UpdateIcon_NotifyIcon()
337-
LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me)
338-
RaiseEvent UpdateNotifyIconStr("Connected", Nothing)
339-
LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me,
340-
String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED),
341-
upsConf.Host, upsConf.Port))
342-
End If
329+
Menu_UPS_Var.Enabled = True
330+
UpdateIcon_NotifyIcon()
331+
LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me)
332+
RaiseEvent UpdateNotifyIconStr("Connected", Nothing)
333+
LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me,
334+
String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED),
335+
upsConf.Host, upsConf.Port))
336+
End Sub
337+
338+
Private Sub ConnectionError(sender As UPS_Device, ex As Exception) Handles UPS_Device.ConnectionError
339+
LogFile.LogTracing(String.Format("Something went wrong connecting to UPS {0}. IsConnected: {1}, IsAuthenticated: {2}",
340+
sender.Name, sender.IsConnected, sender.IsAuthenticated), LogLvl.LOG_ERROR, Me,
341+
String.Format(StrLog.Item(AppResxStr.STR_LOG_CON_FAILED), sender.Nut_Config.Host, sender.Nut_Config.Port,
342+
ex.Message))
343+
UPSDisconnect()
343344
End Sub
344345

345346
''' <summary>

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,15 @@ Public Class Nut_Socket
140140
Public Sub Disconnect(Optional silent = False, Optional forceful = False)
141141
' WatchDog.Stop()
142142

143-
If Not forceful Then
143+
If IsConnected AndAlso Not forceful Then
144144
Query_Data("LOGOUT")
145145
End If
146146

147147
Close_Socket()
148148

149149
If Not silent Then
150-
LogFile.LogTracing("NutSocket raising Disconnected event.", LogLvl.LOG_DEBUG, Me)
151150
RaiseEvent SocketDisconnected()
152151
End If
153-
154-
LogFile.LogTracing("NutSocket has been Disconnected.", LogLvl.LOG_DEBUG, Me)
155152
End Sub
156153

157154
''' <summary>

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Public Class UPS_Device
137137
' Notify of an unexpectedly lost connection (??)
138138
Public Event Lost_Connect()
139139
' Error encountered when trying to connect.
140-
Public Event ConnectionError(innerException As Exception)
140+
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
141141
Public Event EncounteredNUTException(ex As NutException, sender As Object)
142142
Public Event New_Retry()
143143
' Public Event Shutdown_Condition()
@@ -183,7 +183,7 @@ Public Class UPS_Device
183183
RaiseEvent EncounteredNUTException(ex, Me)
184184

185185
Catch ex As Exception
186-
RaiseEvent ConnectionError(ex)
186+
RaiseEvent ConnectionError(Me, ex)
187187

188188
End Try
189189
End Sub

WinNUT_V2/WinNUT-Client_Common/WinNUT_Globals.vb

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,6 @@ Public Module WinNUT_Globals
4747
GitHubURL = My.Application.Info.Trademark
4848
Copyright = My.Application.Info.Copyright
4949
IsConnected = False
50-
51-
'Add Main Gui's Strings
52-
'StrLog.Insert(AppResxStr.STR_MAIN_OLDINI_RENAMED, Resources.Frm_Main_Str_01)
53-
'StrLog.Insert(AppResxStr.STR_MAIN_OLDINI, Resources.Frm_Main_Str_02)
54-
'StrLog.Insert(AppResxStr.STR_MAIN_RECONNECT, Resources.Frm_Main_Str_03)
55-
'StrLog.Insert(AppResxStr.STR_MAIN_RETRY, Resources.Frm_Main_Str_04)
56-
'StrLog.Insert(AppResxStr.STR_MAIN_NOTCONN, Resources.Frm_Main_Str_05)
57-
'StrLog.Insert(AppResxStr.STR_MAIN_CONN, Resources.Frm_Main_Str_06)
58-
'StrLog.Insert(AppResxStr.STR_MAIN_OL, Resources.Frm_Main_Str_07)
59-
'StrLog.Insert(AppResxStr.STR_MAIN_OB, Resources.Frm_Main_Str_08)
60-
'StrLog.Insert(AppResxStr.STR_MAIN_LOWBAT, Resources.Frm_Main_Str_09)
61-
'StrLog.Insert(AppResxStr.STR_MAIN_BATOK, Resources.Frm_Main_Str_10)
62-
'StrLog.Insert(AppResxStr.STR_MAIN_UNKNOWN_UPS, Resources.Frm_Main_Str_11)
63-
'StrLog.Insert(AppResxStr.STR_MAIN_LOSTCONNECT, Resources.Frm_Main_Str_12)
64-
'StrLog.Insert(AppResxStr.STR_MAIN_INVALIDLOGIN, Resources.Frm_Main_Str_13)
65-
'StrLog.Insert(AppResxStr.STR_MAIN_EXITSLEEP, Resources.Frm_Main_Str_14)
66-
'StrLog.Insert(AppResxStr.STR_MAIN_GOTOSLEEP, Resources.Frm_Main_Str_15)
67-
68-
'Add Update Gui's Strings
69-
'StrLog.Insert(AppResxStr.STR_UP_AVAIL, Resources.Frm_Update_Str_01)
70-
'StrLog.Insert(AppResxStr.STR_UP_SHOW, Resources.Frm_Update_Str_02)
71-
'StrLog.Insert(AppResxStr.STR_UP_HIDE, Resources.Frm_Update_Str_03)
72-
'StrLog.Insert(AppResxStr.STR_UP_UPMSG, Resources.Frm_Update_Str_04)
73-
'StrLog.Insert(AppResxStr.STR_UP_DOWNFROM, Resources.Frm_Update_Str_05)
74-
75-
'Add Shutdown Gui's Strings
76-
'StrLog.Insert(AppResxStr.STR_SHUT_STAT, Resources.Frm_Shutdown_Str_01)
77-
78-
'Add App Event's Strings
79-
'StrLog.Insert(AppResxStr.STR_APP_SHUT, Resources.App_Event_Str_01)
80-
81-
'Add Log's Strings
82-
'StrLog.Insert(AppResxStr.STR_LOG_PREFS, Resources.Log_Str_01)
83-
'StrLog.Insert(AppResxStr.STR_LOG_CONNECTED, Resources.Log_Str_02)
84-
'StrLog.Insert(AppResxStr.STR_LOG_CON_FAILED, Resources.Log_Str_03)
85-
'StrLog.Insert(AppResxStr.STR_LOG_CON_RETRY, Resources.Log_Str_04)
86-
'StrLog.Insert(AppResxStr.STR_LOG_LOGOFF, Resources.Log_Str_05)
87-
'StrLog.Insert(AppResxStr.STR_LOG_NEW_RETRY, Resources.Log_Str_06)
88-
'StrLog.Insert(AppResxStr.STR_LOG_STOP_RETRY, Resources.Log_Str_07)
89-
'StrLog.Insert(AppResxStr.STR_LOG_SHUT_START, Resources.Log_Str_08)
90-
'StrLog.Insert(AppResxStr.STR_LOG_SHUT_STOP, Resources.Log_Str_09)
91-
'StrLog.Insert(AppResxStr.STR_LOG_NO_UPDATE, Resources.Log_Str_10)
92-
'StrLog.Insert(AppResxStr.STR_LOG_UPDATE, Resources.Log_Str_11)
93-
'StrLog.Insert(AppResxStr.STR_LOG_NUT_FSD, Resources.Log_Str_12)
94-
9550
LogFile = New Logger(False, LogLvl.LOG_DEBUG)
9651
End Sub
9752

0 commit comments

Comments
 (0)