This commit is contained in:
2025-05-23 15:28:30 +04:00
parent 92b9c356b8
commit 0e545e56de
144 changed files with 294 additions and 1907 deletions

View File

@@ -0,0 +1,15 @@
package herohandler
import (
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/handlerfactory/core"
)
// GetFactory returns the handler factory
func (h *HeroHandler) GetFactory() *core.HandlerFactory {
return h.factory
}
// RegisterHandler registers a handler with the factory
func (h *HeroHandler) RegisterHandler(handler core.Handler) error {
return h.factory.RegisterHandler(handler)
}

View File

@@ -0,0 +1,39 @@
package main
import (
"log"
"sync"
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/handlerfactory/herohandler"
)
func main() {
// Initialize the herohandler.DefaultInstance
if err := herohandler.Init(); err != nil {
log.Fatalf("Failed to initialize herohandler: %v", err)
}
// Start the telnet server on both Unix socket and TCP
socketPath := "/tmp/hero.sock"
tcpAddress := "localhost:8023"
log.Println("Starting telnet server...")
//if err := herohandler.DefaultInstance.StartTelnet(socketPath, tcpAddress, "1234");
if err := herohandler.DefaultInstance.StartTelnet(socketPath, tcpAddress); err != nil {
log.Fatalf("Failed to start telnet server: %v", err)
}
log.Println("Telnet server started successfully")
// Register a callback for when the server shuts down
herohandler.DefaultInstance.EnableSignalHandling(func() {
log.Println("Server shutdown complete")
})
log.Println("Press Ctrl+C to stop the server")
// Create a WaitGroup that never completes to keep the program running
// The signal handling in the telnet server will handle the shutdown
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}

View File

@@ -0,0 +1,94 @@
package herohandler
import (
"fmt"
"log"
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/handlerfactory/core"
// "git.ourworld.tf/herocode/heroagent/pkg/handlerfactory/heroscript/handlerfactory/fakehandler"
"git.ourworld.tf/herocode/heroagent/pkg/heroscript/handlerfactory/processmanagerhandler"
)
// HeroHandler is the main handler factory that manages all registered handlers
type HeroHandler struct {
factory *core.HandlerFactory
telnetServer *core.TelnetServer
}
var (
// DefaultInstance is the default HeroHandler instance
DefaultInstance *HeroHandler
)
// init initializes the default HeroHandler instance
func Init() error {
factory := core.NewHandlerFactory()
DefaultInstance = &HeroHandler{
factory: factory,
telnetServer: core.NewTelnetServer(factory),
}
log.Println("HeroHandler initialized")
// Register the process manager handler
handler := processmanagerhandler.NewProcessManagerHandler()
if handler == nil {
log.Fatalf("Failed to create process manager handler")
}
if err := DefaultInstance.factory.RegisterHandler(handler); err != nil {
log.Fatalf("Failed to register process manager handler: %v", err)
}
return nil
}
func StartTelnet() error {
if err := DefaultInstance.StartTelnet("/tmp/hero.sock", "localhost:8023"); err != nil {
log.Fatalf("Failed to start telnet server: %v", err)
}
return nil
}
// StartTelnet starts the telnet server on both Unix socket and TCP port
func (h *HeroHandler) StartTelnet(socketPath string, tcpAddress string, secrets ...string) error {
// Create a new telnet server with the factory and secrets
h.telnetServer = core.NewTelnetServer(h.factory, secrets...)
// Start Unix socket server
if socketPath != "" {
if err := h.telnetServer.Start(socketPath); err != nil {
return fmt.Errorf("failed to start Unix socket telnet server: %v", err)
}
log.Printf("Telnet server started on Unix socket: %s", socketPath)
}
// Start TCP server
if tcpAddress != "" {
if err := h.telnetServer.StartTCP(tcpAddress); err != nil {
return fmt.Errorf("failed to start TCP telnet server: %v", err)
}
log.Printf("Telnet server started on TCP address: %s", tcpAddress)
}
return nil
}
// StopTelnet stops the telnet server
func (h *HeroHandler) StopTelnet() error {
if h.telnetServer == nil {
return nil
}
return h.telnetServer.Stop()
}
// EnableSignalHandling sets up signal handling for graceful shutdown of the telnet server
func (h *HeroHandler) EnableSignalHandling(onShutdown func()) {
if h.telnetServer == nil {
return
}
h.telnetServer.EnableSignalHandling(onShutdown)
}