Skip to content

Commit

Permalink
start refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jun 11, 2024
1 parent fbc4b67 commit 72e0118
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 332 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
# syntax=docker/dockerfile:1
# Copyright (C) 2023 Kevin Z <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

ARG GO_VERSION=1.21
ARG REPO=github.com/LiterMC/go-openbmclapi
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 76 additions & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cluster

import (
"context"
"sync/atomic"
)

type Cluster struct {
id string
secret string
host string
port uint16

storageManager *storage.Manager
storages []int // the index of storages in the storage manager

status atomic.Int32
}

// ID returns the cluster id
func (cr *Cluster) ID() string {
return cr.id
}

// Host returns the cluster public host
func (cr *Cluster) Host() string {
return cr.host
}

// Port returns the cluster public port
func (cr *Cluster) Port() string {
return cr.port
}

// Init do setup on the cluster
// Init should only be called once during the cluster's whole life
// The context passed in only affect the logical of Init method
func (cr *Cluster) Init(ctx context.Context) error {
return
}

// Enable send enable packet to central server
// The context passed in only affect the logical of Enable method
func (cr *Cluster) Enable(ctx context.Context) error {
return
}

// Disable send disable packet to central server
// The context passed in only affect the logical of Disable method
func (cr *Cluster) Disable(ctx context.Context) error {
return
}

// setDisabled marked the cluster as disabled or kicked
func (cr *Cluster) setDisabled(kicked bool) {
return
}
20 changes: 20 additions & 0 deletions cluster/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cluster
45 changes: 45 additions & 0 deletions cluster/keepalive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cluster

import (
"context"
)

type KeepAliveRes int

// Succeed returns true when KeepAlive actions is succeed as well as the cluster is not kicked by the controller
func (r KeepAliveRes) Succeed() bool {
return r == 0
}

// Failed returns true when KeepAlive action is succeed but the cluster is forced kick by the controller
func (r KeepAliveRes) Kicked() bool {
return r == 1
}

// Failed returns true when KeepAlive is interrupted by unexpected reason
func (r KeepAliveRes) Failed() bool {
return r == 2
}

func (cr *Cluster) KeepAlive(ctx context.Context) KeepAliveRes {
//
}
61 changes: 61 additions & 0 deletions cluster/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2024 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package cluster

const (
clusterDisabled = 0
clusterEnabled = 1
clusterEnabling = 2
clusterKicked = 4
)

// Enabled returns true if the cluster is enabled or enabling
func (cr *Cluster) Enabled() bool {
s := cr.status.Load()
return s == clusterEnabled || s == clusterEnabling
}

// Running returns true if the cluster is completely enabled
func (cr *Cluster) Running() bool {
return cr.status.Load() == clusterEnabled
}

// Disabled returns true if the cluster is disabled manually
func (cr *Cluster) Disabled() bool {
return cr.status.Load() == clusterDisabled
}

// IsKicked returns true if the cluster is kicked by the central server
func (cr *Cluster) IsKicked() bool {
return cr.status.Load() == clusterKicked
}

// WaitForEnable returns a channel which receives true when cluster enabled succeed, or receives false when it failed to enable
// If the cluster is already enable, the channel always returns true
// The channel should not be used multiple times
func (cr *Cluster) WaitForEnable() <-chan bool {
ch := make(chan bool, 1)
if cr.Running() {
ch <- true
} else {
cr.enableSignals = append(cr.enableSignals, ch)
}
return ch
}
2 changes: 0 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ var emptyHashes = func() (hashes map[string]struct{}) {
return
}()

var HeaderXPoweredBy = fmt.Sprintf("go-openbmclapi/%s; url=https://github.com/LiterMC/go-openbmclapi", build.BuildVersion)

//go:embed robots.txt
var robotTxtContent string

Expand Down
Loading

0 comments on commit 72e0118

Please sign in to comment.