hardlink/lockserver/lockserver.go
2025-05-22 12:08:55 +01:00

99 lines
3.0 KiB
Go

package lockserver
import (
"bufio"
"fmt"
"net"
"net/url"
// "os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var (
LockserverUrl string
)
func InitializeServerConnection() (net.Conn, error) {
const funcName = "InitializeServerConnection"
// Parse the URL to extract host and port
parsedUrl, err := url.Parse(LockserverUrl)
if err != nil {
return nil, fmt.Errorf("[%s] failed to parse LockserverUrl: %v", funcName, err)
}
// Remove any leading/trailing slashes just in case
address := strings.Trim(parsedUrl.Host, "/")
// Establish a TCP connection to the Visionline server
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, fmt.Errorf("failed to connect to Visionline server: %v", err)
}
return conn, nil
}
func SendHeartbeatToServer(conn net.Conn) (string, error) {
const funcName = "SendHeartbeatToServer"
// Write the check command to the server
heartbeat := "CCC;EAHEARTBEAT;AM1;\r\n"
log.Printf("Sending headrbeat: %q", heartbeat)
_, err := conn.Write([]byte(heartbeat))
if err != nil {
return "", fmt.Errorf("failed to send Heartbeat command: %v", err)
}
// set a read timeout to avoid indefinite blocking
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// Read the response from the server. Visionline returns the response as ASCII text terminated by CRLF.
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
substr := "RC"
response = strings.ReplaceAll(response, "\r\n", "") // Remove CRLF from the response
num := strings.Index(response, substr) // Find the index of the response code
responseCode := response[num+2 : len(response)-1] // Extract the result code from the response
if responseCode != "0" {
return "", fmt.Errorf("negative Heartbeat response code: %s", responseCode)
}
return "Success: " + response, nil
}
func RequestEncoding(conn net.Conn, command string) (string, error) {
const funcName = "RequestEncoding"
// Write the command to the connection
log.Printf("Sending Encoding request: %q", command)
_, err := conn.Write([]byte(command))
if err != nil {
return "", fmt.Errorf("failed to send Encoding request: %v", err)
}
// Optional: set a read timeout to avoid indefinite blocking
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// Read the response from the server. Visionline returns the response as ASCII text terminated by CRLF.
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
substr := "RC"
response = strings.ReplaceAll(response, "\r\n", "") // Remove CRLF from the response
num := strings.Index(response, substr) // Find the index of the response code
responseCode := response[num+2 : len(response)-1] // Extract the result code from the response
if responseCode != "0" {
return "", fmt.Errorf("negative lock server response code: %s", responseCode)
}
return "Success: " + response, nil
}