Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,10 @@ jobs:
- name: Copy README.md
run: cp README.md common-files/

- name: Copy VERSION.md
run: cp backend/cmd/VERSION.md common-files/
- name: Create VERSION.txt
run: |
VERSION="${{ github.event.inputs.version }}"
echo "$VERSION" > common-files/VERSION.txt

- name: Upload common files artifact
uses: actions/upload-artifact@v4
Expand All @@ -292,7 +294,7 @@ jobs:

- name: Create release directories for each platform
run: |
VERSION="${{ github.event.inputs.version || github.event.release.tag_name || 'latest' }}"
VERSION="${{ github.event.inputs.version }}"

# Create directory structure for each platform
mkdir -p release-linux
Expand All @@ -302,7 +304,7 @@ jobs:

- name: Organize Linux release files
run: |
VERSION="${{ github.event.inputs.version || github.event.release.tag_name || 'latest' }}"
VERSION="${{ github.event.inputs.version }}"

# Copy Linux backend
cp artifacts/backend-linux/backend-linux-amd64 release-linux/backend
Expand All @@ -325,7 +327,7 @@ jobs:

- name: Organize Windows release files
run: |
VERSION="${{ github.event.inputs.version || github.event.release.tag_name || 'latest' }}"
VERSION="${{ github.event.inputs.version }}"

# Copy Windows backend
cp artifacts/backend-windows/backend-windows-amd64.exe release-windows/backend.exe
Expand All @@ -348,7 +350,7 @@ jobs:

- name: Organize macOS Intel release files
run: |
VERSION="${{ github.event.inputs.version || github.event.release.tag_name || 'latest' }}"
VERSION="${{ github.event.inputs.version }}"

# Copy macOS Intel backend
cp artifacts/backend-macos/backend-macos-amd64 release-macos/backend
Expand All @@ -371,7 +373,7 @@ jobs:

- name: Organize macOS ARM64 release files
run: |
VERSION="${{ github.event.inputs.version || github.event.release.tag_name || 'latest' }}"
VERSION="${{ github.event.inputs.version }}"

# Copy macOS ARM64 backend
cp artifacts/backend-macos/backend-macos-arm64 release-macos-arm64/backend
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ backend/cmd/cmd
.idea/
.vscode/
.prettierrc

# Claude
CLAUDE.md
.claude
1 change: 0 additions & 1 deletion backend/cmd/VERSION.md

This file was deleted.

13 changes: 9 additions & 4 deletions backend/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ type Network struct {
Manual bool
}

type Transport struct {
PropagateFault bool
}

type Config struct {
Vehicle vehicle.Config
Server server.Config
Adj Adj
Network Network
Vehicle vehicle.Config
Server server.Config
Adj Adj
Network Network
Transport Transport
}
2 changes: 2 additions & 0 deletions backend/cmd/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ branch = "main" # Leave blank when using ADJ as a submodule (like this: "")
test = true
[network]
manual = false
[transport]
propagate_fault = true
58 changes: 24 additions & 34 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
vehicle_models "github.com/HyperloopUPV-H8/h9-backend/internal/vehicle/models"
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
"github.com/HyperloopUPV-H8/h9-backend/pkg/broker"
connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection"
blcu_topics "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/blcu"
connection_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/connection"
data_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/data"
logger_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/logger"
message_topic "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/message"
Expand Down Expand Up @@ -87,7 +87,7 @@ var currentVersion string

func main() {

versionFile := "VERSION.md"
versionFile := "VERSION.txt"
versionData, err := os.ReadFile(versionFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading version file (%s): %v\n", versionFile, err)
Expand Down Expand Up @@ -185,41 +185,35 @@ func main() {
} else {

fmt.Println("Backend folder not detected. Launching existing updater...")
osType := detectOS()

execPath, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting executable path: %v\n", err)
os.Exit(1)
}
updatersDir := filepath.Join(filepath.Dir(execPath), "updaters")

var updaterExe string
switch osType {
case "updaters/updater-windows-64.exe":
updaterExe = filepath.Join(updatersDir, "updater-windows-64")
case "updaters/updater-linux":
updaterExe = filepath.Join(updatersDir, "updater-linux")
case "updaters/updater-macos-m1":
updaterExe = filepath.Join(updatersDir, "updater-macos-m1")
case "updaters/updater-macos-64":
updaterExe = filepath.Join(updatersDir, "updater-macos-64")
default:
fmt.Fprintf(os.Stderr, "Unsupported updater: %s\n", osType)
os.Exit(1)
execDir := filepath.Dir(execPath)

updaterExe := filepath.Join(execDir, "updater")
// En Windows el ejecutable lleva extensión .exe
if runtime.GOOS == "windows" {
updaterExe += ".exe"
}

cmd := exec.Command(updaterExe)
cmd.Dir = updatersDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err)
os.Exit(1)
if _, err := os.Stat(updaterExe); err == nil {
cmd := exec.Command(updaterExe)
cmd.Dir = execDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error launching updater: %v\n", err)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "Updater not found: %s\n", updaterExe)
fmt.Println("Skipping update. Proceeding with the current version.")
}
}

os.Exit(0)
} else {
fmt.Println("Skipping update. Proceeding with the current version.")
}
Expand Down Expand Up @@ -322,6 +316,7 @@ func main() {

// <--- transport --->
transp := transport.NewTransport(trace.Logger)
transp.SetpropagateFault(config.Transport.PropagateFault)

// <--- vehicle --->
ipToBoardId := make(map[string]abstraction.BoardId)
Expand Down Expand Up @@ -742,14 +737,9 @@ func getLatestVersionFromGitHub() (string, error) {
func detectOS() string {
switch runtime.GOOS {
case "windows":
return "updaters/updater-windows-64.exe"
case "darwin":
if strings.Contains(runtime.GOARCH, "arm") {
return "updaters/updater-macos-m1"
}
return "updaters/updater-macos-64"
case "linux":
return "updaters/updater-linux"
return "updater.exe"
case "darwin", "linux":
return "updater"
default:
fmt.Fprintf(os.Stderr, "Unsupported operating system: %s\n", runtime.GOOS)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions backend/internal/adj/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adj

import (
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -35,6 +36,7 @@ func updateRepo(AdjBranch string) error {
_, err = git.PlainClone(tempPath, false, cloneOptions)
if err != nil {
// If the clone fails, work with the local ADJ
log.Printf("Warning: Could not clone ADJ branch '%s' from remote. Working with local ADJ. Error: %v", AdjBranch, err)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions backend/pkg/adj/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package adj

import (
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -35,6 +36,7 @@ func updateRepo(AdjBranch string) error {
_, err = git.PlainClone(tempPath, false, cloneOptions)
if err != nil {
// If the clone fails, work with the local ADJ
log.Printf("Warning: Could not clone ADJ branch '%s' from remote. Working with local ADJ. Error: %v", AdjBranch, err)
return nil
}

Expand Down
54 changes: 54 additions & 0 deletions backend/pkg/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Transport struct {

tftp *tftp.Client

propagateFault bool

api abstraction.TransportAPI

logger zerolog.Logger
Expand Down Expand Up @@ -170,6 +172,14 @@ func (transport *Transport) handleTCPConn(conn net.Conn) error {
return
}

if transport.propagateFault && packet.Id() == 0 {
connectionLogger.Info().Msg("replicating packet with id 0 to all boards")
err := transport.handlePacketEvent(NewPacketMessage(packet))
if err != nil {
connectionLogger.Error().Err(err).Msg("failed to replicate packet")
}
}

from := conn.RemoteAddr().String()
to := conn.LocalAddr().String()

Expand Down Expand Up @@ -208,6 +218,37 @@ func (transport *Transport) SendMessage(message abstraction.TransportMessage) er
// handlePacketEvent is used to send an order to one of the connected boards
func (transport *Transport) handlePacketEvent(message PacketMessage) error {
eventLogger := transport.logger.With().Str("type", fmt.Sprintf("%T", message.Packet)).Uint16("id", uint16(message.Id())).Logger()

if message.Id() == 0 {
eventLogger.Info().Msg("broadcasting packet id 0")
data, err := transport.encoder.Encode(message.Packet)
if err != nil {
eventLogger.Error().Stack().Err(err).Msg("encode")
transport.errChan <- err
return err
}

transport.connectionsMx.Lock()
defer transport.connectionsMx.Unlock()
for target, conn := range transport.connections {
eventLogger := eventLogger.With().Str("target", string(target)).Logger()

totalWritten := 0
for totalWritten < len(data) {
n, err := conn.Write(data[totalWritten:])
eventLogger.Trace().Int("amount", n).Msg("written chunk")
totalWritten += n
if err != nil {
eventLogger.Error().Stack().Err(err).Msg("write")
transport.errChan <- err
return err
}
}
eventLogger.Info().Msg("sent")
}
return nil
}

target, ok := transport.idToTarget[message.Id()]
if !ok {
eventLogger.Debug().Msg("target not found")
Expand Down Expand Up @@ -299,6 +340,15 @@ func (transport *Transport) handleConversation(socket network.Socket, reader io.
return
}

// Intercept packets with id == 0 and replicate
if transport.propagateFault && packet.Id() == 0 {
conversationLogger.Info().Msg("replicating packet with id 0 to all boards")
err := transport.handlePacketEvent(NewPacketMessage(packet))
if err != nil {
conversationLogger.Error().Err(err).Msg("failed to replicate packet")
}
}

transport.api.Notification(NewPacketNotification(packet, srcAddr, dstAddr, time.Now()))
}
}()
Expand All @@ -322,3 +372,7 @@ func (transport *Transport) SendFault() {
// transport.errChan <- err
// }
}

func (transport *Transport) SetpropagateFault(enabled bool) {
transport.propagateFault = enabled
}
Loading
Loading