Example #1
0
func NewNode(config NodeConfig) (*Node, error) {
	node := &Node{
		Config:                     config,
		outputMessagesReceived:     nil,                                                     // received
		transportsMessagesReceived: make(chan []byte, config.TransportMessageChannelLength), // received
		serializer:                 serialize.NewSerializer(),
		lock:                       &sync.Mutex{}, // Lock
		closeGroup:                 &sync.WaitGroup{},
		closing:                    make(chan bool, 10),
		transports:                 make(map[transport.ITransport]bool),
		routes:                     make(map[domain.RouteID]domain.Route),
		localRoutesByTerminatingPeer:   make(map[cipher.PubKey]domain.RouteID),
		localRoutes:                    make(map[domain.RouteID]domain.LocalRoute),
		routeExtensionsAwaitingConfirm: make(map[domain.RouteID]chan bool),
		//myCrypto:                   &ChaChaCrypto{config.ChaCha20Key},
		controlChannels: make(map[uuid.UUID]*ControlChannel),
	}
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{1}, domain.UserMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{2}, domain.SetRouteMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{3}, domain.RefreshRouteMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{4}, domain.DeleteRouteMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{5}, domain.SetRouteReply{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{6}, domain.SetControlChannelMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{7}, domain.SetControlChannelResponseMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{8}, domain.SetRouteControlMessage{})
	node.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{9}, domain.ResponseMessage{})

	go node.processIncomingMessagesLoop()
	go node.expireOldRoutesLoop()
	go node.refreshRoutesLoop()

	return node, nil
}
Example #2
0
func NewConnection(node *mesh.Node, peerID cipher.PubKey) IConnection {
	connection := &Connection{
		Node:                   node,
		ConnectedPeerID:        peerID,
		Serializer:             serialize.NewSerializer(),
		MessagesBeingAssembled: make(map[domain.MessageID]*domain.MessageUnderAssembly),
		TimeToAssembleMessage:  2 * time.Second,
		closing:                make(chan bool, 10),
	}

	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{1}, domain.UserMessage{})
	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{2}, domain.SetRouteMessage{})
	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{3}, domain.RefreshRouteMessage{})
	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{4}, domain.DeleteRouteMessage{})
	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{5}, domain.SetRouteReply{})
	connection.Serializer.RegisterMessageForSerialization(serialize.MessagePrefix{6}, domain.AddNodeMessage{})

	go connection.expireOldMessagesLoop()

	return connection
}
Example #3
0
func NewTransport(physicalTransport ITransport, config TransportConfig) *Transport {
	transport := &Transport{
		currentID,
		config,
		physicalTransport,
		nil,
		serialize.NewSerializer(),
		[]byte{},
		DISCONNECTED,
		&sync.Mutex{},
		make(map[domain.MessageID]messageSentState),
		make(map[domain.MessageID]time.Time),
		1000,
		time.Time{},
		0,
		0,
		0,
		0,
		0,
		make(chan []byte, config.PhysicalReceivedChannelLength),
		make(chan bool, 10),
		&sync.WaitGroup{},
	}

	currentID++

	transport.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{1}, SendMessage{})
	transport.serializer.RegisterMessageForSerialization(serialize.MessagePrefix{2}, ReplyMessage{})

	go transport.processReceivedLoop()
	go transport.expireMessagesLoop()
	go transport.retransmitLoop()

	transport.physicalTransport.SetReceiveChannel(transport.physicalReceived)
	return transport
}