Skip to content

Commit

Permalink
added forcereconnect for special conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovikov committed Sep 30, 2023
1 parent 802c230 commit 8ebb414
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions HFP.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/ivlovric/HFP/queue"
)

const AppVersion = "0.56.6"
const AppVersion = "0.56.7"

var localAddr *string = flag.String("l", ":9060", "Local HEP listening address")
var remoteAddr *string = flag.String("r", "192.168.2.2:9060", "Remote HEP address")
Expand All @@ -42,6 +42,7 @@ var (
MaxBufferSizeBytes int64 = 0
productsQueue *queue.Queue
hepConnect net.Conn
forceReconnect bool = true
reconnectCount uint
)

Expand All @@ -64,15 +65,11 @@ func connectToHEPBackend() error {
defer func() {
if r := recover(); r != nil {
log.Println(fmt.Printf("Panic: %v,\n%s", r, debug.Stack()))
forceReconnect = true
return
}
}()

if hepConnect != nil {
log.Println("Lets close the connection first")
hepConnect.Close()
}

dst := *remoteAddr
proto := *remoteProto

Expand All @@ -87,6 +84,7 @@ func connectToHEPBackend() error {
if err != nil {
log.Println("Unable to connect to server: ", err)
connectionStatus.Set(0)
forceReconnect = true
return fmt.Errorf("couldn't connect to server: %s", err.Error())
} else {
log.Println("Connected to server successfully")
Expand All @@ -107,6 +105,7 @@ func connectToHEPBackend() error {
SendPingHEPPacket(hepConnect)
time.Sleep(time.Second * 1)
connectionStatus.Set(1)
forceReconnect = false

if _, err := copyHEPFileOut(); err != nil {
log.Println("||-->", logsymbols.Error, "Sending HEP from file error....:", err)
Expand Down Expand Up @@ -192,9 +191,9 @@ func handleConnection(clientConn net.Conn) {

if *Debug == "on" {
if string(buf[:n]) == "HELLO HFP" {
log.Println("||--> Sending init HELLO HFP successful with filter for", string(ipf))
log.Println("||--> Added job HFP to the queue successful with filter for", string(ipf))
} else {
log.Println("||--> Sending HEP OUT successful with filter for", string(ipf))
log.Println("||--> Added job to the queue with filter for", string(ipf))
}
}
}
Expand Down Expand Up @@ -238,7 +237,7 @@ func handleConnection(clientConn net.Conn) {

productsQueue.AddJob(hepJob)
if *Debug == "on" {
log.Println("||-->", logsymbols.Success, " Sending HEP OUT successful with filter")
log.Println("||-->", logsymbols.Success, " Added HEP OUT job successful with filter")
}
}

Expand All @@ -253,9 +252,9 @@ func handleConnection(clientConn net.Conn) {

if *Debug == "on" {
if string(buf[:n]) == "HELLO HFP" {
log.Println("||-->", logsymbols.Success, " Sending init HELLO HFP successful without filters")
log.Println("||-->", logsymbols.Success, " Added HELLO HFP job successful without filters")
} else {
log.Println("||-->", logsymbols.Success, " Sending HEP OUT successful without filters")
log.Println("||-->", logsymbols.Success, " Added HEP OUT job successful without filters")
}
}

Expand All @@ -268,12 +267,13 @@ func sendHepOut(data []byte, len int) error {
defer func() {
if r := recover(); r != nil {
log.Println(fmt.Printf("sendHepOut to panic: %v,\n%s", r, debug.Stack()))
forceReconnect = true
return
}
}()

//If socket is nil - reconnect....
if hepConnect == nil {
if hepConnect == nil || forceReconnect {
if *Debug == "on" {
log.Println("||-->", logsymbols.Error, "socket is nil:")
}
Expand All @@ -294,6 +294,7 @@ func sendHepOut(data []byte, len int) error {

//Last check
if hepConnect == nil {
forceReconnect = true
return fmt.Errorf("socket is still nil")
}
}
Expand All @@ -308,6 +309,8 @@ func sendHepOut(data []byte, len int) error {
}

connectionStatus.Set(0)
forceReconnect = true

copyHEPbufftoFile(data, HEPsavefile)

//Starts reopen connection
Expand All @@ -317,6 +320,8 @@ func sendHepOut(data []byte, len int) error {
if *Debug == "on" {
log.Println("||-->", logsymbols.Error, " reconnect to HEP backend error: ", err.Error())
}
} else {
forceReconnect = false
}
reconnectCount = 0
}
Expand Down Expand Up @@ -394,7 +399,7 @@ func copyHEPFileOut() (int, error) {
fmt.Println("Read HEP file error", HEPFileDataerr)
}

if hepConnect == nil {
if hepConnect == nil || forceReconnect {
log.Println("||-->", logsymbols.Error, " connection is broken....")
return 0, fmt.Errorf("connection is broken")
}
Expand All @@ -405,6 +410,7 @@ func copyHEPFileOut() (int, error) {
log.Println("||-->X Send HEP from LOG error", err)
AppLogger.Println("||-->X Send HEP from LOG error", err)
hepFileFlushesError.Inc()
forceReconnect = true
} else {
fi, err := os.Stat(HEPsavefile)
if err != nil {
Expand Down Expand Up @@ -547,6 +553,7 @@ func SendPingHEPPacket(conn net.Conn) {
defer func() {
if r := recover(); r != nil {
log.Println(fmt.Printf("hep ping panic: %v,\n%s", r, debug.Stack()))
forceReconnect = true
return
}
}()
Expand All @@ -564,10 +571,17 @@ func SendPingHEPPacket(conn net.Conn) {

//Send Logged HEP upon reconnect out to backend
_, err = conn.Write(msg)

if err != nil {
forceReconnect = true
log.Println("||-->X Send HEP PING", err)
AppLogger.Println("||-->X Send HEP PING", err)
} else if *Debug == "on" {
log.Println("-->|| Sent HEP Ping")

} else {

forceReconnect = false
if *Debug == "on" {
log.Println("-->|| Sent HEP Ping")
}
}
}

0 comments on commit 8ebb414

Please sign in to comment.