Skip to content
Snippets Groups Projects
Commit 0a3d16c5 authored by Hans Moog's avatar Hans Moog
Browse files

Refactor: refactored some code

parent 3ee42649
No related branches found
No related tags found
No related merge requests found
Showing
with 183 additions and 42 deletions
......@@ -13,8 +13,8 @@ func main() {
node.Run(
cli.PLUGIN,
autopeering.PLUGIN,
analysis.PLUGIN,
statusscreen.PLUGIN,
gracefulshutdown.PLUGIN,
analysis.PLUGIN,
)
}
......@@ -63,6 +63,7 @@ func HandleConnection(conn *network.ManagedConnection) {
}
onDisconnect = func() {
Events.NodeOffline.Trigger(connectedNodeId)
Events.RemoveNode.Trigger(connectedNodeId)
conn.Events.ReceiveData.Detach(onReceiveData)
conn.Events.Close.Detach(onDisconnect)
......
......@@ -8,10 +8,25 @@ import (
"golang.org/x/net/websocket"
)
var nodes = make(map[string]bool)
var links = make(map[string]map[string]bool)
func dataStream(ws *websocket.Conn) {
eventHandlers := &types.EventHandlers{
AddNode: func(nodeId string) { fmt.Fprint(ws, "A"+nodeId) },
RemoveNode: func(nodeId string) { fmt.Fprint(ws, "a"+nodeId) },
AddNode: func(nodeId string) {
if _, exists := nodes[nodeId]; !exists {
nodes[nodeId] = true
fmt.Fprint(ws, "A"+nodeId)
}
},
RemoveNode: func(nodeId string) {
if _, exists := nodes[nodeId]; exists {
delete(nodes, nodeId)
fmt.Fprint(ws, "a"+nodeId)
}
},
ConnectNodes: func(sourceId string, targetId string) { fmt.Fprint(ws, "C"+sourceId+targetId) },
DisconnectNodes: func(sourceId string, targetId string) { fmt.Fprint(ws, "c"+sourceId+targetId) },
NodeOnline: func(nodeId string) { fmt.Fprint(ws, "O"+nodeId) },
......
......@@ -34,7 +34,7 @@ func index(w http.ResponseWriter, r *http.Request) {
break;
case "A":
addNode(e.data.substr(1));
//addNode(e.data.substr(1));
break;
case "a":
......@@ -42,6 +42,8 @@ func index(w http.ResponseWriter, r *http.Request) {
break;
case "C":
addNode(e.data.substr(1, 40));
addNode(e.data.substr(41, 40));
connectNodes(e.data.substr(1, 40), e.data.substr(41, 40));
break;
......@@ -72,13 +74,27 @@ func index(w http.ResponseWriter, r *http.Request) {
.enableNodeDrag(false)
.onNodeHover(node => elem.style.cursor = node ? 'pointer' : null)
.onNodeClick(removeNodeX)
.linkDirectionalParticles(3)
.linkDirectionalParticleWidth(0.8)
.linkDirectionalParticleSpeed(0.01)
//.linkDirectionalParticles(3)
//.linkDirectionalParticleWidth(0.8)
//.linkDirectionalParticleSpeed(0.01)
.nodeColor(node => node.online ? 'rgba(0,255,0,1)' : 'rgba(255,255,255,1)')
.graphData(data);
function addNode(nodeId) {
var updateRequired = true;
setInterval(function() {
if (updateRequired) {
Graph.graphData(data);
updateRequired = false;
}
}, 500)
updateGraph = function() {
updateRequired = true;
};
function addNode(nodeId, displayImmediately) {
node = {id : nodeId, online: false};
if (!(node.id in nodesById)) {
......@@ -86,7 +102,7 @@ func index(w http.ResponseWriter, r *http.Request) {
nodesById[node.id] = node;
Graph.graphData(data);
updateGraph();
}
}
......@@ -96,31 +112,35 @@ func index(w http.ResponseWriter, r *http.Request) {
delete nodesById[nodeId];
Graph.graphData(data);
updateGraph();
}
function setNodeOnline(nodeId) {
if (nodeId in nodesById) {
nodesById[nodeId].online = true;
}
Graph.graphData(data);
updateGraph();
}
function setNodeOffline(nodeId) {
if (nodeId in nodesById) {
nodesById[nodeId].online = false;
Graph.graphData(data);
updateGraph();
}
}
function connectNodes(sourceNodeId, targetNodeId) {
data.links = [...data.links, { source: sourceNodeId, target: targetNodeId }];
Graph.graphData(data);
updateGraph();
}
function disconnectNodes(sourceNodeId, targetNodeId) {
data.links = data.links.filter(l => !(l.source.id == sourceNodeId && l.target.id == targetNodeId) && !(l.source.id == targetNodeId && l.target.id == sourceNodeId));
Graph.graphData(data);
updateGraph();
}
function removeNodeX(node) {
......
......@@ -9,33 +9,90 @@ import (
var recordedEvents = make([]types.EventHandlersConsumer, 0)
var nodes = make(map[string]bool)
var links = make(map[string]map[string]bool)
func Configure(plugin *node.Plugin) {
server.Events.AddNode.Attach(func(nodeId string) {
nodes[nodeId] = false
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.AddNode(nodeId)
})
*/
})
server.Events.RemoveNode.Attach(func(nodeId string) {
delete(nodes, nodeId)
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.AddNode(nodeId)
})
*/
})
server.Events.NodeOnline.Attach(func(nodeId string) {
nodes[nodeId] = true
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.NodeOnline(nodeId)
})
*/
})
server.Events.NodeOffline.Attach(func(nodeId string) {
nodes[nodeId] = false
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.NodeOffline(nodeId)
})
*/
})
server.Events.ConnectNodes.Attach(func(sourceId string, targetId string) {
connectionMap, connectionMapExists := links[sourceId]
if !connectionMapExists {
connectionMap = make(map[string]bool)
links[sourceId] = connectionMap
}
connectionMap[targetId] = true
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.ConnectNodes(sourceId, targetId)
})
*/
})
server.Events.DisconnectNodes.Attach(func(sourceId string, targetId string) {
connectionMap, connectionMapExists := links[sourceId]
if connectionMapExists {
delete(connectionMap, targetId)
}
/*
recordedEvents = append(recordedEvents, func(handlers *types.EventHandlers) {
handlers.ConnectNodes(sourceId, targetId)
})
*/
})
}
func Replay(handlers *types.EventHandlers, delay time.Duration) {
for nodeId, online := range nodes {
handlers.AddNode(nodeId)
if online {
handlers.NodeOnline(nodeId)
} else {
handlers.NodeOffline(nodeId)
}
}
for sourceId, targetMap := range links {
for targetId, _ := range targetMap {
handlers.ConnectNodes(sourceId, targetId)
}
}
/*
for _, recordedEvent := range recordedEvents {
recordedEvent(handlers)
......@@ -43,4 +100,5 @@ func Replay(handlers *types.EventHandlers, delay time.Duration) {
time.Sleep(delay)
}
}
*/
}
package chosenneighborcandidates
import (
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
......@@ -16,7 +17,7 @@ var DISTANCE = func(anchor *peer.Peer) func(p *peer.Peer) uint64 {
}
}
func init() {
func Configure(plugin *node.Plugin) {
updateNeighborCandidates()
neighborhood.Events.Update.Attach(updateNeighborCandidates)
......
......@@ -3,6 +3,7 @@ package entrynodes
import (
"encoding/hex"
"github.com/iotaledger/goshimmer/packages/identity"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerlist"
......@@ -11,7 +12,11 @@ import (
"strings"
)
var INSTANCE = parseEntryNodes()
var INSTANCE peerlist.PeerList
func Configure(node *node.Plugin) {
INSTANCE = parseEntryNodes()
}
func parseEntryNodes() peerlist.PeerList {
result := make(peerlist.PeerList, 0)
......
package knownpeers
import (
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/entrynodes"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peerregister"
)
var INSTANCE = initKnownPeers()
func Configure(plugin *node.Plugin) {
INSTANCE = initKnownPeers()
}
func initKnownPeers() peerregister.PeerRegister {
knownPeers := make(peerregister.PeerRegister)
for _, entryNode := range entrynodes.INSTANCE {
......
package neighborhood
import (
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/packages/timeutil"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
......@@ -27,7 +28,7 @@ var NEIGHBORHOOD_SELECTOR = func(this peerregister.PeerRegister, req *request.Re
var lastUpdate = time.Now()
func init() {
func Configure(plugin *node.Plugin) {
updateNeighborHood()
go timeutil.Ticker(updateNeighborHood, 1 * time.Second)
......
package outgoingrequest
import (
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/request"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/salt"
......@@ -9,7 +10,7 @@ import (
var INSTANCE *request.Request
func init() {
func Configure(plugin *node.Plugin) {
INSTANCE = &request.Request{
Issuer: ownpeer.INSTANCE,
}
......
......@@ -2,16 +2,21 @@ package ownpeer
import (
"github.com/iotaledger/goshimmer/packages/accountability"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
"net"
)
var INSTANCE = &peer.Peer{
var INSTANCE *peer.Peer
func Configure(plugin *node.Plugin) {
INSTANCE = &peer.Peer{
Identity: accountability.OWN_ID,
PeeringPort: uint16(*parameters.PORT.Value),
GossipPort: uint16(*parameters.PORT.Value),
Address: net.IPv4(0, 0, 0, 0),
Salt: saltmanager.PUBLIC_SALT,
}
}
package instances
import (
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/chosenneighborcandidates"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/entrynodes"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/outgoingrequest"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer"
)
func Configure(plugin *node.Plugin) {
ownpeer.Configure(plugin)
entrynodes.Configure(plugin)
knownpeers.Configure(plugin)
neighborhood.Configure(plugin)
outgoingrequest.Configure(plugin)
chosenneighborcandidates.Configure(plugin)
}
......@@ -3,6 +3,7 @@ package autopeering
import (
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/knownpeers"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
......@@ -13,6 +14,7 @@ import (
)
func configure(plugin *node.Plugin) {
instances.Configure(plugin)
server.Configure(plugin)
protocol.Configure(plugin)
......
......@@ -3,9 +3,9 @@ package constants
import "time"
const (
NEIGHBOR_COUNT = 8
NEIGHBOR_COUNT = 4
FIND_NEIGHBOR_INTERVAL = 5 * time.Second
FIND_NEIGHBOR_INTERVAL = 60 * time.Second
// How often does the outgoing ping processor check if new pings should be sent.
PING_PROCESS_INTERVAL = 1 * time.Second
......
......@@ -5,7 +5,7 @@ import (
"github.com/iotaledger/goshimmer/packages/daemon"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/neighborhood"
"github.com/iotaledger/goshimmer/plugins/autopeering/parameters"
"github.com/iotaledger/goshimmer/plugins/autopeering/instances/ownpeer"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/types"
"github.com/iotaledger/goshimmer/plugins/autopeering/saltmanager"
......@@ -13,7 +13,6 @@ import (
"github.com/iotaledger/goshimmer/plugins/autopeering/types/ping"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/salt"
"math/rand"
"net"
"time"
)
......@@ -27,13 +26,7 @@ func createOutgoingPingProcessor(plugin *node.Plugin) func() {
lastPing = time.Now().Add(-constants.PING_CYCLE_LENGTH)
outgoingPing := &ping.Ping{
Issuer: &peer.Peer{
Identity: accountability.OWN_ID,
Address: net.IPv4(0, 0, 0, 0),
PeeringPort: uint16(*parameters.PORT.Value),
GossipPort: uint16(*parameters.PORT.Value),
Salt: saltmanager.PUBLIC_SALT,
},
Issuer: ownpeer.INSTANCE,
}
outgoingPing.Sign()
......
......@@ -65,6 +65,17 @@ func (this *Request) Accept(peers []*peer.Peer) error {
}
func (this *Request) Reject(peers []*peer.Peer) error {
peeringResponse := &response.Response{
Type: response.TYPE_REJECT,
Issuer: ownpeer.INSTANCE,
Peers: peers,
}
peeringResponse.Sign()
if _, err := this.Issuer.Send(peeringResponse.Marshal(), types.PROTOCOL_TYPE_TCP, false); err != nil {
return err
}
return nil
}
......
......@@ -3,6 +3,7 @@ package response
import (
"bytes"
"github.com/iotaledger/goshimmer/packages/identity"
"github.com/iotaledger/goshimmer/plugins/autopeering/protocol/constants"
"github.com/iotaledger/goshimmer/plugins/autopeering/types/peer"
"github.com/pkg/errors"
)
......@@ -76,12 +77,14 @@ func (this *Response) Marshal() []byte {
copy(result[MARSHALLED_ISSUER_START:MARSHALLED_ISSUER_END], this.Issuer.Marshal())
for i, peer := range this.Peers {
if i < constants.NEIGHBOR_COUNT {
PEERING_RESPONSE_MARSHALLED_PEER_START := MARSHALLED_PEERS_START + (i * MARSHALLED_PEER_SIZE)
PEERING_RESPONSE_MARSHALLED_PEER_END := PEERING_RESPONSE_MARSHALLED_PEER_START + MARSHALLED_PEER_SIZE
result[PEERING_RESPONSE_MARSHALLED_PEER_START] = 1
copy(result[PEERING_RESPONSE_MARSHALLED_PEER_START+1:PEERING_RESPONSE_MARSHALLED_PEER_END], peer.Marshal()[:MARSHALLED_PEER_SIZE-1])
}
}
copy(result[MARSHALLED_SIGNATURE_START:MARSHALLED_SIGNATURE_END], this.Signature[:MARSHALLED_SIGNATURE_SIZE])
......
......@@ -2,8 +2,8 @@ package cli
import (
"flag"
"github.com/iotaledger/goshimmer/packages/parameter"
"github.com/iotaledger/goshimmer/packages/node"
"github.com/iotaledger/goshimmer/packages/parameter"
"strings"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment