common

package module
v0.0.0-...-e0abb51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: GPL-3.0 Imports: 14 Imported by: 2

Documentation

Overview

Package common provides shared functionality for the momo application.

Index

Constants

View Source
const (

	// TCPSocketBufferSize defines the standard buffer size for TCP sockets.
	TCPSocketBufferSize = 1024
	// TimestampLength is the expected length of a timestamp string.
	TimestampLength = 19
	// AuthTokenLength is the expected length of the authentication token.
	AuthTokenLength = 64
	// FileInfoLength is the allocated length for file information strings.
	FileInfoLength = 64
	// MaxFileSize is the maximum allowed size for a file transfer (1GB).
	MaxFileSize = 1024 * 1024 * 1024
)
View Source
const (
	// ReplicationType defines the different modes of data replication.
	ReplicationType int = iota
	// ReplicationChain indicates the use of chain replication.
	ReplicationChain
	// ReplicationSplay indicates the use of splay replication.
	ReplicationSplay
	// ReplicationPrimarySplay indicates a primary-splay replication strategy.
	ReplicationPrimarySplay
	// ReplicationNone indicates that no replication is used.
	ReplicationNone

	// DummyEpoch is a placeholder epoch value for initialization.
	DummyEpoch = 1557906926566451195
)

Variables

View Source
var GetConfigFromFile = func() (Configuration, error) {
	return GetConfig("conf/momo.conf")
}

GetConfigFromFile is a function variable that loads the configuration from the default path "conf/momo.conf". It can be overridden in tests to load a custom configuration for testing purposes.

Functions

func Connect

func Connect(wg *sync.WaitGroup, cfg Configuration, filePath string, serverId int, timestamp int64)

Connect establishes connections with daemon(s) and sends a file. It first connects to a specified daemon to determine the replication mode. If splay replication is active, it connects to all other daemons. Finally, it sends the file to all established connections concurrently.

func DialSocket

func DialSocket(servAddr string) (net.Conn, error)

DialSocket connects to the given address. It returns a net.Conn or an error.

func FindStringIndex

func FindStringIndex(slice []string, value string) int

FindStringIndex searches for a string in a slice of strings and returns its index. If the string is not found, it returns -1. ⚡ Bolt: Use `slices.Index` from the standard library for a ~7x performance improvement over manual loops.

func HashFile

func HashFile(filePath string) (string, error)

HashFile calculates the SHA-256 hash of a file. It takes the file path as input and returns the SHA-256 hash as a hex-encoded string.

func LogStdOut

func LogStdOut(logApp bool)

LogStdOut configures the logging output for the application. If logApp is true, it sets the log flags to include timestamps, file names, and line numbers. If logApp is false, it discards all log output.

func PadString

func PadString(input string, length int) string

PadString pads a string with null characters to a specified length. If the string is longer than the specified length, it is truncated.

Types

type Configuration

type Configuration struct {
	// Daemons is a list of daemons in the system.
	Daemons []*Daemon
	// Global is the global configuration.
	Global ConfigurationGlobal
	// Metrics is the metrics configuration.
	Metrics ConfigurationMetrics
}

Configuration holds the overall configuration for the application.

func GetConfig

func GetConfig(path string) (Configuration, error)

GetConfig loads and validates the configuration from the given file path.

type ConfigurationGlobal

type ConfigurationGlobal struct {
	// Debug enables or disables debug mode.
	Debug bool
	// AuthToken is the authentication token used for node-to-node and client-to-node communication.
	AuthToken string
	// ReplicationOrder is the order of replication modes to use.
	ReplicationOrder []int
	// PolymorphicSystem enables or disables the polymorphic system.
	PolymorphicSystem bool
}

ConfigurationGlobal holds the global configuration for the application.

type ConfigurationMetrics

type ConfigurationMetrics struct {
	// Interval is the interval at which to collect metrics.
	Interval int
	// MaxThreshold is the maximum threshold for metrics.
	MaxThreshold float64
	// MinThreshold is the minimum threshold for metrics.
	MinThreshold float64
	// FallbackInterval is the interval at which to fall back to a lower replication mode.
	FallbackInterval int
}

ConfigurationMetrics holds the metrics configuration for the application.

type Daemon

type Daemon struct {
	// Host is the address of the daemon.
	Host string
	// ChangeReplication is the endpoint for changing the replication mode.
	ChangeReplication string
	// Data is the endpoint for data operations.
	Data string
	// Drive is the drive used by the daemon.
	Drive string
}

Daemon represents a daemon in the system.

type FileMetadata

type FileMetadata struct {
	// Name is the name of the file.
	Name string
	// Hash is the SHA-256 hash of the file.
	Hash string
	// Size is the size of the file in bytes.
	Size int64
}

FileMetadata stores the metadata for a file.

type IdleTimeoutConn

type IdleTimeoutConn struct {
	net.Conn
	// contains filtered or unexported fields
}

IdleTimeoutConn wraps a net.Conn to provide a rolling idle timeout. Every successful Read or Write resets the deadline, preventing slowloris attacks without interrupting large, active file transfers.

func NewIdleTimeoutConn

func NewIdleTimeoutConn(conn net.Conn, timeout time.Duration) *IdleTimeoutConn

NewIdleTimeoutConn creates a new IdleTimeoutConn.

func (*IdleTimeoutConn) Read

func (c *IdleTimeoutConn) Read(b []byte) (n int, err error)

Read reads data from the connection and resets the read deadline.

func (*IdleTimeoutConn) Write

func (c *IdleTimeoutConn) Write(b []byte) (n int, err error)

Write writes data to the connection and resets the write deadline.

type ReplicationData

type ReplicationData struct {
	// Old is the old replication mode.
	Old int `json:"old"`
	// New is the new replication mode.
	New int `json:"new"`
	// TimeStamp is the timestamp of the replication mode change.
	TimeStamp int64 `json:"timestamp"`
}

ReplicationData stores the information about a replication mode change.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL