Exemple #1
0
func main() {
	log.Println("testing...")
	usage := `relay

Usage:
  relay [--external=<interface> --internal=<destination>]
  relay -h | --help
  relay --version

Options:
  -h --help                 Show this screen.
  --version                 Show version.
  --external=<interface>    Interface to listen on [default: localhost:9999].
  --internal=<destination>  Destination to forward to [default: localhost:9998].`

	arguments, _ := docopt.Parse(usage, nil, true, "Relay v1.0.0", false)
	log.Println(arguments)

	external := common.ArgOrDefault("--external", "localhost:9998", arguments)
	internal := common.ArgOrDefault("--internal", "localhost:9999", arguments)

	log.Println("Listening to external", external)
	externalListener := listenOrExit(external)
	defer externalListener.Close()

	log.Println("Listening to internal", internal)
	internalListener := listenOrExit(internal)
	defer internalListener.Close()

	externalConnectionsCh := make(chan net.Conn, 100)
	internalConnectionsCh := make(chan net.Conn, 100)

	externalConnectionsQueue := &ConnectionQueue{nodes: make([]net.Conn, 3)}
	internalConnectionsQueue := &ConnectionQueue{nodes: make([]net.Conn, 3)}

	go getConnections(externalListener, externalConnectionsCh)
	go getConnections(internalListener, internalConnectionsCh)

	log.Println("Starting main run loop.")

	for {
		select {
		case externalConnection := <-externalConnectionsCh:
			{
				externalConnectionsQueue.Push(externalConnection)
			}
		case internalConnection := <-internalConnectionsCh:
			{
				internalConnectionsQueue.Push(internalConnection)
			}
		case <-time.After(10 * time.Second):
			log.Println("No connections, tick")
		}
		// NKG: It is important to note that there isn't any code that checks
		// to see if the connections are still open/active once they've been
		// put into the queue.
		if externalConnectionsQueue.Size() > 0 && internalConnectionsQueue.Size() > 0 {
			externalConnection := externalConnectionsQueue.Pop()
			internalConnection := internalConnectionsQueue.Pop()
			go relayPair(externalConnection, internalConnection)
		}
	}
}
Exemple #2
0
func main() {
	usage := `echoserver

Usage:
  echoserver [--listen=<interface>]
  echoserver -h | --help
  echoserver --version

Options:
  -h --help                Show this screen.
  --version                Show version.
  --listen=<interface>     Interface to listen on [default: localhost:9998].`

	arguments, _ := docopt.Parse(usage, nil, true, "echoserver v1.0.0", false)

	listen := common.ArgOrDefault("--listen", "localhost:9998", arguments)

	// id to bool (true if has received activity)
	connections := make(map[int]bool)
	connectionPairs := make(chan ConnectionPair, 100)
	connectionActivity := make(chan int, 100)
	closedConnections := make(chan int, 100)

	lastId := 0

	for {
		select {
		case connection, ok := <-connectionPairs:
			{
				if !ok {
					return
				}
				connections[connection.id] = false
				go echoFunc(connection.conn)
			}
		case connection, ok := <-connectionActivity:
			{
				if !ok {
					return
				}
				connections[connection] = true
			}
		case connection, ok := <-closedConnections:
			{
				if !ok {
					return
				}
				delete(connections, connection)
			}
		case <-time.After(10 * time.Second):
			{
				count := 0
				for _, hasReceivedData := range connections {
					if hasReceivedData {
						count++
					}
				}
				if count == len(connections) {
					for i := 0; i < 3; i++ {
						lastId++
						createConn(listen, lastId, connectionPairs)
					}
				}
			}
		}
	}
}