proxy

package
v0.0.0-...-b65274a Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UNICAST_MESSAGE       = 0 // Message to a single node
	MULTICAST_MESSAGE     = 1 // Message to every node
	JOIN_REQUEST_MESSAGE  = 2 // Message used for joining the cluster (request)
	JOIN_NOTIFY_MESSAGE   = 3 // Message used for noifiying the cluster that a new node has joined
	LEAVE_NOTIFY_MESSAGE  = 4 // Message used for notifying the cluster that a node has died
	HTTP_REQUEST_MESSAGE  = 5
	HTTP_RESPONSE_MESSAGE = 6
	ELECTION_MESSAGE      = 7 // Message used for leader election
	ANSWER_MESSAGE        = 8 // Message used for leader election
	VICTORY_MESSAGE       = 9 // Message used for leader election
)

Variables

This section is empty.

Functions

func HashBytes

func HashBytes(b []byte) uint32

func HttpRequestToBytes

func HttpRequestToBytes(r HTTPRequest) []byte

func HttpResponseToBytes

func HttpResponseToBytes(r HTTPResponse) []byte

func MessageToBytes

func MessageToBytes(message Message) []byte

Types

type CacheMetadata

type CacheMetadata struct {
	// Looks like I only need header and body.
	// Can just make two seperate items, header and body
	// in here, which should allow for easy incorporation.
	Header    http.Header
	Body      []byte
	SavedTime time.Time
}

type HTTPRequest

type HTTPRequest struct {
	Method        string
	RequestUrl    string
	Header        http.Header
	Body          []byte
	ContentLength int64
}

func BytesToHttpRequest

func BytesToHttpRequest(b []byte) HTTPRequest

type HTTPResponse

type HTTPResponse struct {
	Status        string
	RequestUrl    string
	Header        http.Header
	Body          []byte
	ContentLength int64
}

func BytesToHttpResponse

func BytesToHttpResponse(b []byte) HTTPResponse

type LocalCache

type LocalCache struct {
	// Note: Will need a mutex if LocalCache is accessed by multiple servers/threads.
	Mem map[string]CacheMetadata
}

func CreateLocalCache

func CreateLocalCache() *LocalCache

Create and initialize a new LocalCache, and return a pointer to it.

func (*LocalCache) CacheGet

func (cache *LocalCache) CacheGet(pageURL string) *CacheMetadata

Returns a []byte representing the cache'd body, if it exists. If the body has expired, or does not exist, then the entry is deleted from the cache and nil is returned.

func (*LocalCache) CacheSet

func (cache *LocalCache) CacheSet(pageURL string, Res HTTPResponse, secondsToStore int) int

If you need to convert a string into a URL struct, use: func Parse(rawurl string) (*URL, error) https://golang.org/pkg/net/url/#Parse

Attempt to cache the body of a response in a LocalCache. Takes a url, the body to store for that url, and a duration to store it for. Returns 1 on success, 0 on failure.

type Message

type Message struct {
	Timestamp   time.Time
	Data        []byte
	SenderUrl   string
	MessageType int
}

func BytesToMessage

func BytesToMessage(bytes []byte) Message

func CreateMessage

func CreateMessage(message []byte, sender_url string, message_type int) Message

type NodeInfo

type NodeInfo struct {
	Host     string
	Port     int
	Url      string
	IsLeader bool
	ID       uint32
}

func CreateNodeInfo

func CreateNodeInfo(host string, port int, leader bool) *NodeInfo

type ProxyConfig

type ProxyConfig struct {
	PublicUrl    string `json:"public_url"`
	CacheTimeout int    `json:"cache_timeout"`

	BlockedSitesList []string `json:"blocked_sites"`
	BlockedSites     map[string]string
}

func LoadProxyConfig

func LoadProxyConfig(path string) *ProxyConfig

func (*ProxyConfig) SiteIsBlocked

func (p *ProxyConfig) SiteIsBlocked(url string) bool

type ProxyNode

type ProxyNode struct {
	Config               *ProxyConfig
	Info                 *NodeInfo
	PeerInfo             []*NodeInfo
	Messenger            *TCPMessenger
	Responses            *LocalCache
	Lock                 *sync.Mutex
	CV                   *sync.Cond
	CurrentForwardingIdx int
	LeaderUrl            string
}

func CreateProxyNode

func CreateProxyNode(host string, port int, leader bool, config_path string) *ProxyNode

func (*ProxyNode) ConstructAnswerMessage

func (p *ProxyNode) ConstructAnswerMessage() Message

func (*ProxyNode) ConstructElectionMessage

func (p *ProxyNode) ConstructElectionMessage() Message

func (*ProxyNode) ConstructNodeJoinedMessage

func (p *ProxyNode) ConstructNodeJoinedMessage() Message

func (*ProxyNode) ConstructNodeLeftMessage

func (p *ProxyNode) ConstructNodeLeftMessage(url string) Message

func (*ProxyNode) ConstructVictoryMessage

func (p *ProxyNode) ConstructVictoryMessage() Message

func (*ProxyNode) ContainsResponse

func (p *ProxyNode) ContainsResponse(url string) bool

func (*ProxyNode) ContainsUrl

func (p *ProxyNode) ContainsUrl(url string) bool

func (*ProxyNode) HandleHttpRequest

func (p *ProxyNode) HandleHttpRequest(w http.ResponseWriter, r *http.Request)

func (*ProxyNode) HandleRequest

func (p *ProxyNode) HandleRequest(b []byte)

func (*ProxyNode) HandleRequests

func (p *ProxyNode) HandleRequests()

func (*ProxyNode) IndexFromString

func (p *ProxyNode) IndexFromString(url string) int

func (*ProxyNode) Multicast

func (p *ProxyNode) Multicast(message []byte)

func (*ProxyNode) RemoveNodeFromPeers

func (p *ProxyNode) RemoveNodeFromPeers(url string)

func (*ProxyNode) StartBackgroundChecker

func (p *ProxyNode) StartBackgroundChecker()

func (*ProxyNode) StartLeaderElection

func (p *ProxyNode) StartLeaderElection()

func (*ProxyNode) Unicast

func (p *ProxyNode) Unicast(message []byte, url string) bool

type TCPMessenger

type TCPMessenger struct {
	Listener            net.Listener
	RecentMessageHashes map[uint32]time.Time
}

func InitTCPMessenger

func InitTCPMessenger(url string) *TCPMessenger

func (TCPMessenger) HasMessageStored

func (m TCPMessenger) HasMessageStored(hash uint32) bool

func (TCPMessenger) PruneStoredMessages

func (m TCPMessenger) PruneStoredMessages()

Jump to

Keyboard shortcuts

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