Skip to content

Commit 668f13b

Browse files
authored
Merge pull request #64 from nutdotnet/63-conn_handling
Improve connection loss handling
2 parents e2e72c0 + 6d419a0 commit 668f13b

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

WinNUT_V2/WinNUT-Client/WinNUT.vb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ Public Class WinNUT
346346
End If
347347
End Sub
348348

349+
Private Sub UPS_Lostconnect() Handles UPS_Device.Lost_Connect
350+
LogFile.LogTracing("Notify user of lost connection", LogLvl.LOG_ERROR, Me,
351+
String.Format(StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port))
352+
' UPSDisconnect()
353+
354+
ReInitDisplayValues()
355+
If UPS_Device.Nut_Config.AutoReconnect And UPS_Retry <= UPS_MaxRetry Then
356+
ActualAppIconIdx = AppIconIdx.IDX_ICO_RETRY
357+
Else
358+
ActualAppIconIdx = AppIconIdx.IDX_ICO_OFFLINE
359+
End If
360+
361+
UpdateIcon_NotifyIcon()
362+
RaiseEvent UpdateNotifyIconStr("Lost Connect", Nothing)
363+
RaiseEvent UpdateBatteryState("Lost Connect")
364+
LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me)
365+
End Sub
366+
349367
''' <summary>
350368
''' Perform final actions to wrap up a disconnected UPS.
351369
''' </summary>
@@ -587,24 +605,6 @@ Public Class WinNUT
587605
HasFocus = False
588606
End Sub
589607

590-
Private Sub UPS_Lostconnect() Handles UPS_Device.Lost_Connect
591-
LogFile.LogTracing("Notify user of lost connection", LogLvl.LOG_ERROR, Me,
592-
String.Format(StrLog.Item(AppResxStr.STR_MAIN_LOSTCONNECT), UPS_Device.Nut_Config.Host, UPS_Device.Nut_Config.Port))
593-
' UPSDisconnect()
594-
595-
'ReInitDisplayValues()
596-
If UPS_Device.Nut_Config.AutoReconnect And UPS_Retry <= UPS_MaxRetry Then
597-
ActualAppIconIdx = AppIconIdx.IDX_ICO_RETRY
598-
Else
599-
ActualAppIconIdx = AppIconIdx.IDX_ICO_OFFLINE
600-
End If
601-
602-
UpdateIcon_NotifyIcon()
603-
RaiseEvent UpdateNotifyIconStr("Lost Connect", Nothing)
604-
RaiseEvent UpdateBatteryState("Lost Connect")
605-
LogFile.LogTracing("Update Icon", LogLvl.LOG_DEBUG, Me)
606-
End Sub
607-
608608
Public Shared Sub Event_ChangeStatus() Handles Me.On_Battery, Me.On_Line,
609609
UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected, UPS_Device.New_Retry, UPS_Device.ReConnected
610610
', UPS_Device.Unknown_UPS

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,30 +188,31 @@ Public Class Nut_Socket
188188
''' </summary>
189189
''' <param name="Query_Msg">The query to be sent to the server, within specifications of the NUT protocol.</param>
190190
''' <returns>The full <see cref="Transaction"/> of this function call.</returns>
191-
''' <exception cref="InvalidOperationException">Thrown when calling this function while disconnected.</exception>"
191+
''' <exception cref="InvalidOperationException">Thrown when calling this function while disconnected, or another
192+
''' call is in progress.</exception>
192193
''' <exception cref="NutException">Thrown when the NUT server returns an error or unexpected response.</exception>
193194
Function Query_Data(Query_Msg As String) As Transaction
194195
Dim Response As NUTResponse
195196
Dim DataResult As String
196197
Dim finalTransaction As Transaction
197198

198199
If streamInUse Then
199-
LogFile.LogTracing("Attempted to query " & Query_Msg & " while using the stream.", LogLvl.LOG_ERROR, Me)
200-
Return Nothing
200+
Throw New InvalidOperationException("Attempted to query " & Query_Msg & " while stream is in use.")
201201
End If
202202

203-
streamInUse = True
204-
205203
If ConnectionStatus Then
206-
' LogFile.LogTracing("Query: " & Query_Msg, LogLvl.LOG_DEBUG, Me)
207-
WriterStream.WriteLine(Query_Msg & vbCr)
208-
WriterStream.Flush()
204+
streamInUse = True
209205

210-
DataResult = Trim(ReaderStream.ReadLine())
211-
' LogFile.LogTracing(vbTab & "Response: " & DataResult, LogLvl.LOG_DEBUG, Me)
212-
streamInUse = False
213-
' LogFile.LogTracing("Done processing response for query " & Query_Msg, LogLvl.LOG_DEBUG, Me)
206+
Try
207+
WriterStream.WriteLine(Query_Msg & vbCr)
208+
WriterStream.Flush()
209+
Catch
210+
Throw
211+
Finally
212+
streamInUse = False
213+
End Try
214214

215+
DataResult = Trim(ReaderStream.ReadLine())
215216
Response = EnumResponse(DataResult)
216217
finalTransaction = New Transaction(Query_Msg, DataResult, Response)
217218

0 commit comments

Comments
 (0)