Stable
This commit is contained in:
parent
3ee652111a
commit
7110f02154
6
go.mod
6
go.mod
@ -1,3 +1,9 @@
|
|||||||
module cimeyclust.com/steel
|
module cimeyclust.com/steel
|
||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gizak/termui v3.1.0+incompatible // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||||
|
github.com/nsf/termbox-go v1.1.1 // indirect
|
||||||
|
)
|
||||||
|
|||||||
6
go.sum
Normal file
6
go.sum
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
github.com/gizak/termui v3.1.0+incompatible h1:N3CFm+j087lanTxPpHOmQs0uS3s5I9TxoAFy6DqPqv8=
|
||||||
|
github.com/gizak/termui v3.1.0+incompatible/go.mod h1:PkJoWUt/zacQKysNfQtcw1RW+eK2SxkieVBtl+4ovLA=
|
||||||
|
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||||
|
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||||
|
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
|
||||||
|
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
|
||||||
@ -6,32 +6,81 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
// Level enum of "info", "warning", "error"
|
||||||
|
Level string
|
||||||
|
|
||||||
|
// Message to be logged
|
||||||
|
Message string
|
||||||
|
|
||||||
|
// Time of the log
|
||||||
|
Time string
|
||||||
|
|
||||||
|
// Source of the log
|
||||||
|
Source string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoggerQueue struct {
|
||||||
|
queue chan Log
|
||||||
|
}
|
||||||
|
|
||||||
|
var Logger = &LoggerQueue{
|
||||||
|
queue: make(chan Log, 100),
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LoggerQueue) log(level string, message string, source string) {
|
||||||
|
l.queue <- Log{
|
||||||
|
Level: level,
|
||||||
|
Message: message,
|
||||||
|
Time: time.Now().Format("2006/01/02 15:04:05"),
|
||||||
|
Source: source,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LoggerQueue) Info(message string) {
|
||||||
|
l.log("INFO", message, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LoggerQueue) Warning(message string) {
|
||||||
|
l.log("WARNING", message, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LoggerQueue) Error(message string) {
|
||||||
|
l.log("ERROR", message, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LoggerQueue) processLogs() {
|
||||||
|
for logEntry := range l.queue {
|
||||||
|
// Move cursor up and clear line
|
||||||
|
fmt.Printf("%s [%s]: %s\n", logEntry.Time, logEntry.Level, logEntry.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Start(wg *sync.WaitGroup, stopChan chan struct{}) {
|
func Start(wg *sync.WaitGroup, stopChan chan struct{}) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
go Logger.processLogs()
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
for {
|
for {
|
||||||
fmt.Print("> ")
|
fmt.Print("> ")
|
||||||
input, err := reader.ReadString('\n')
|
input, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := fmt.Fprintf(os.Stderr, "Error while reading input: %v", err)
|
Logger.Error(fmt.Sprintf("Error while reading input: %v", err))
|
||||||
if err != nil {
|
continue
|
||||||
return
|
|
||||||
}
|
|
||||||
close(stopChan)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
input = strings.TrimSpace(input)
|
input = strings.TrimSpace(input)
|
||||||
switch input {
|
switch input {
|
||||||
case "stop":
|
case "stop":
|
||||||
fmt.Println("Received stop command.")
|
Logger.Info("Received stop command.")
|
||||||
close(stopChan)
|
close(stopChan)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unknown command:", input)
|
Logger.Info(fmt.Sprintf("Unknown command: %s", input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cimeyclust.com/steel/pkg/cmd"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Start Starts the TCP server on the specified address.
|
// Start Starts the TCP server on the specified address.
|
||||||
@ -14,11 +15,7 @@ func Start(addr string, stopChan <-chan struct{}, wg *sync.WaitGroup) {
|
|||||||
// Start listening on the specified address
|
// Start listening on the specified address
|
||||||
listener, err := net.Listen("tcp", addr)
|
listener, err := net.Listen("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := fmt.Fprintf(os.Stderr, "Error starting TCP server: %v", err)
|
cmd.Logger.Error("Error starting TCP server: %v")
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the listener when the application closes.
|
// Close the listener when the application closes.
|
||||||
@ -29,7 +26,21 @@ func Start(addr string, stopChan <-chan struct{}, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
}(listener)
|
}(listener)
|
||||||
|
|
||||||
fmt.Printf("TCP server listening on %s\n", addr)
|
// slog.Info(fmt.Sprintf("Listening on %s", addr))
|
||||||
|
cmd.Logger.Info(fmt.Sprintf("Listening on %s", addr))
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
// Print test every second
|
||||||
|
select {
|
||||||
|
case <-stopChan:
|
||||||
|
return // Safe exit if stop has been signalled
|
||||||
|
default:
|
||||||
|
cmd.Logger.Info("Test")
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Listen for an incoming connection in a goroutine.
|
// Listen for an incoming connection in a goroutine.
|
||||||
go func() {
|
go func() {
|
||||||
@ -40,7 +51,7 @@ func Start(addr string, stopChan <-chan struct{}, wg *sync.WaitGroup) {
|
|||||||
case <-stopChan:
|
case <-stopChan:
|
||||||
return // Safe exit if stop has been signalled
|
return // Safe exit if stop has been signalled
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(os.Stderr, "Error accepting connection: %v", err)
|
cmd.Logger.Error(fmt.Sprintf("Error while accepting conn: %v", err))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -65,7 +76,7 @@ func handleRequest(conn net.Conn) {
|
|||||||
// Read the incoming connection into the buffer.
|
// Read the incoming connection into the buffer.
|
||||||
_, err := conn.Read(buf)
|
_, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error reading: %v", err)
|
cmd.Logger.Error(fmt.Sprintf("Error reading: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
pkg/utils/queue.go
Normal file
53
pkg/utils/queue.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
// Queue implementation
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
Value interface{}
|
||||||
|
Next *Node
|
||||||
|
}
|
||||||
|
|
||||||
|
type Queue struct {
|
||||||
|
Head *Node
|
||||||
|
Tail *Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) Enqueue(value interface{}) {
|
||||||
|
node := &Node{Value: value}
|
||||||
|
|
||||||
|
if q.Head == nil {
|
||||||
|
q.Head = node
|
||||||
|
q.Tail = node
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
q.Tail.Next = node
|
||||||
|
q.Tail = node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) Dequeue() interface{} {
|
||||||
|
if q.Head == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
node := q.Head
|
||||||
|
q.Head = node.Next
|
||||||
|
|
||||||
|
if q.Head == nil {
|
||||||
|
q.Tail = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return node.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) Peek() interface{} {
|
||||||
|
if q.Head == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return q.Head.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queue) IsEmpty() bool {
|
||||||
|
return q.Head == nil
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user