Skip to content

Commit ef6ce51

Browse files
committed
More smaller networking bugfixes
Nut_Socket.vb - ConnectionStatus property checks for null client object before checking .Connected property - TcpClient object is left null at construction, and only instantiated during a Connection subroutine. - Disconnect subroutine doesn't explicitly check for connection status on the TcpClient object before running anymore. This allows it to dispose/close all objects regardless. UPS_Device.vb - Fixed polling interval not being applied to timer. Associated property is now read only, and the interval is set directly on the timer during construction. - Fixed missing call to socket's Disconnect subroutine, and added a general exception catch-all. - Removed Login code from Reconnect subroutine, leaving that to external code. - Removed ReConnected event since it seems to duplicate the Connected event's calls. WinNUT.vb - Moved Login call to connection finalization subroutine. - Added LogException call during protocol error handling.
1 parent 031bee4 commit ef6ce51

File tree

3 files changed

+33
-66
lines changed

3 files changed

+33
-66
lines changed

WinNUT_V2/WinNUT-Client/WinNUT.vb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,12 @@ Public Class WinNUT
383383
UPS_Device = New UPS_Device(Nut_Config, LogFile, My.Settings.NUT_PollIntervalMsec, My.Settings.CAL_FreqInNom)
384384
AddHandler UPS_Device.EncounteredNUTException, AddressOf HandleNUTException
385385
UPS_Device.Connect_UPS(retryOnConnFailure)
386-
387-
If Not String.IsNullOrEmpty(Nut_Config.Login) Then
388-
UPS_Device.Login()
389-
End If
390386
End Sub
391387

392388
''' <summary>
393389
''' Prepare the form to begin receiving data from a connected UPS.
394390
''' </summary>
395-
Private Sub UPSReady(nutUps As UPS_Device) Handles UPS_Device.Connected, UPS_Device.ReConnected
391+
Private Sub UPSReady(nutUps As UPS_Device) Handles UPS_Device.Connected
396392
Dim upsConf = nutUps.Nut_Config
397393
LogFile.LogTracing(upsConf.UPSName & " has indicated it's ready to start sending data.", LogLvl.LOG_DEBUG, Me)
398394

@@ -410,6 +406,10 @@ Public Class WinNUT
410406
LogFile.LogTracing("Connection to Nut Host Established", LogLvl.LOG_NOTICE, Me,
411407
String.Format(StrLog.Item(AppResxStr.STR_LOG_CONNECTED),
412408
upsConf.Host, upsConf.Port))
409+
410+
If Not String.IsNullOrEmpty(upsConf.Login) Then
411+
UPS_Device.Login()
412+
End If
413413
End Sub
414414

415415
Private Sub ConnectionError(sender As UPS_Device, ex As Exception) Handles UPS_Device.ConnectionError
@@ -596,7 +596,8 @@ Public Class WinNUT
596596
Event_Unknown_UPS()
597597
End If
598598

599-
LogFile.LogTracing("NUT protocol error encoutnered:" + vbNewLine + ex.ToString(), LogLvl.LOG_NOTICE, sender)
599+
LogFile.LogTracing("NUT protocol error encoutnered:", LogLvl.LOG_NOTICE, sender)
600+
LogFile.LogException(ex, Me)
600601
End Sub
601602

602603
Public Sub Event_Unknown_UPS() ' Handles UPS_Device.Unknown_UPS
@@ -623,7 +624,7 @@ Public Class WinNUT
623624
End Sub
624625

625626
Public Shared Sub Event_ChangeStatus() Handles Me.On_Battery, Me.On_Line,
626-
UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected, UPS_Device.New_Retry, UPS_Device.ReConnected
627+
UPS_Device.Lost_Connect, UPS_Device.Connected, UPS_Device.Disconnected, UPS_Device.New_Retry
627628
', UPS_Device.Unknown_UPS
628629
', UPS_Device.InvalidLogin
629630

WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2-
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3-
'
4-
' This program is free software: you can redistribute it and/or modify it under the terms of the
5-
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6-
' License, or any later version.
7-
'
8-
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9-
10-
11-
12-
' Class dealing only with the management of the communication socket with the Nut server
13-
Imports System.IO
14-
Imports System.Net
1+
Imports System.IO
152
Imports System.Net.Sockets
163

174
Public Class Nut_Socket
185

196
#Region "Properties"
207
Public ReadOnly Property ConnectionStatus As Boolean
218
Get
22-
Return client.Connected
9+
Return If(client IsNot Nothing, client.Connected, False)
2310
End Get
2411
End Property
2512

@@ -55,7 +42,7 @@ Public Class Nut_Socket
5542
Private NutConfig As Nut_Parameter
5643

5744
'Socket Variables
58-
Private client As New TcpClient
45+
Private client As TcpClient
5946
Private NutStream As NetworkStream
6047
Private ReaderStream As StreamReader
6148
Private WriterStream As StreamWriter
@@ -86,7 +73,7 @@ Public Class Nut_Socket
8673
Try
8774
LogFile.LogTracing(String.Format("Attempting TCP socket connection to {0}:{1}...", Host, Port), LogLvl.LOG_NOTICE, Me)
8875

89-
client.Connect(Host, Port)
76+
client = New TcpClient(Host, Port)
9077
NutStream = client.GetStream()
9178
ReaderStream = New StreamReader(NutStream)
9279
WriterStream = New StreamWriter(NutStream)
@@ -140,28 +127,24 @@ Public Class Nut_Socket
140127
''' </summary>
141128
''' <param name="forceful">Skip sending the LOGOUT command to the NUT server. Unknown effects.</param>
142129
Public Sub Disconnect(Optional forceful = False)
143-
If IsConnected Then
144-
If IsLoggedIn AndAlso Not forceful Then
145-
Query_Data("LOGOUT")
146-
End If
130+
If IsLoggedIn AndAlso Not forceful Then
131+
Query_Data("LOGOUT")
132+
End If
147133

148-
If WriterStream IsNot Nothing Then
149-
WriterStream.Close()
150-
End If
134+
If WriterStream IsNot Nothing Then
135+
WriterStream.Dispose()
136+
End If
151137

152-
If ReaderStream IsNot Nothing Then
153-
ReaderStream.Close()
154-
End If
138+
If ReaderStream IsNot Nothing Then
139+
ReaderStream.Dispose()
140+
End If
155141

156-
If NutStream IsNot Nothing Then
157-
NutStream.Close()
158-
End If
142+
If NutStream IsNot Nothing Then
143+
NutStream.Dispose()
144+
End If
159145

160-
If client IsNot Nothing Then
161-
client.Close()
162-
End If
163-
Else
164-
Throw New InvalidOperationException("NUT Socket is already disconnected.")
146+
If client IsNot Nothing Then
147+
client.Close()
165148
End If
166149
End Sub
167150

WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
' WinNUT-Client is a NUT windows client for monitoring your ups hooked up to your favorite linux server.
2-
' Copyright (C) 2019-2021 Gawindx (Decaux Nicolas)
3-
'
4-
' This program is free software: you can redistribute it and/or modify it under the terms of the
5-
' GNU General Public License as published by the Free Software Foundation, either version 3 of the
6-
' License, or any later version.
7-
'
8-
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
9-
10-
Imports System.Globalization
1+
Imports System.Globalization
112
Imports System.Windows.Forms
123

134
Public Class UPS_Device
@@ -47,13 +38,10 @@ Public Class UPS_Device
4738
''' How often UPS data is updated, in milliseconds.
4839
''' </summary>
4940
''' <returns></returns>
50-
Public Property PollingInterval As Integer
41+
Public ReadOnly Property PollingInterval As Integer
5142
Get
5243
Return Update_Data.Interval
5344
End Get
54-
Set(value As Integer)
55-
Update_Data.Interval = value
56-
End Set
5745
End Property
5846

5947
Public Property IsUpdatingData As Boolean
@@ -89,7 +77,6 @@ Public Class UPS_Device
8977
#Region "Events"
9078
Public Event DataUpdated()
9179
Public Event Connected(sender As UPS_Device)
92-
Public Event ReConnected(sender As UPS_Device)
9380
' Notify that the connection was closed gracefully.
9481
Public Event Disconnected()
9582
' Notify of an unexpectedly lost connection (??)
@@ -127,7 +114,6 @@ Public Class UPS_Device
127114
Public Sub New(ByRef Nut_Config As Nut_Parameter, ByRef LogFile As Logger, pollInterval As Integer, defaultFrequency As Integer)
128115
Me.LogFile = LogFile
129116
Me.Nut_Config = Nut_Config
130-
PollingInterval = pollInterval
131117
Freq_Fallback = defaultFrequency
132118
Nut_Socket = New Nut_Socket(Me.Nut_Config, LogFile)
133119

@@ -138,7 +124,7 @@ Public Class UPS_Device
138124
End With
139125

140126
With Update_Data
141-
.Interval = DEFAULT_UPDATE_INTERVAL_MS
127+
.Interval = pollInterval
142128
.Enabled = False
143129
AddHandler .Tick, AddressOf Retrieve_UPS_Datas
144130
End With
@@ -194,9 +180,12 @@ Public Class UPS_Device
194180

195181
Retry = 0
196182
Try
197-
183+
Nut_Socket.Disconnect(forceful)
198184
Catch nutEx As NutException
199185
RaiseEvent EncounteredNUTException(Me, nutEx)
186+
Catch ex As Exception
187+
LogFile.LogTracing("Unexpected exception while Disconnecting.", LogLvl.LOG_ERROR, Me)
188+
LogFile.LogException(ex, Me)
200189
Finally
201190
RaiseEvent Disconnected()
202191
End Try
@@ -224,12 +213,6 @@ Public Class UPS_Device
224213
LogFile.LogTracing("Nut Host Reconnected", LogLvl.LOG_DEBUG, Me)
225214
Reconnect_Nut.Stop()
226215
Retry = 0
227-
228-
If Not String.IsNullOrEmpty(Nut_Config.Login) Then
229-
Login()
230-
End If
231-
232-
RaiseEvent ReConnected(Me)
233216
End If
234217
Else
235218
LogFile.LogTracing("Max Retry reached. Stop Process Autoreconnect and wait for manual Reconnection", LogLvl.LOG_ERROR, Me, StrLog.Item(AppResxStr.STR_LOG_STOP_RETRY))

0 commit comments

Comments
 (0)