Esempio n. 1
0
func writer(ws *websocket.Conn, me string, c chan []byte) {

	for {

		if !ws.IsClientConn() {
			break
		}

		b := <-c
		msg := base64.StdEncoding.EncodeToString(b)
		fmt.Printf("Packet received in writer - length %v\n", len(b))
		//fmt.Println(msg)

		var data *T = new(T)
		data.MsgId = time.Now().UnixNano()
		data.Reply = false
		data.To = "collectd"
		data.Msg = msg
		data.From = me
		err := websocket.JSON.Send(ws, data)
		if err != nil {
			log.Fatal(err)
		}

	}

}
Esempio n. 2
0
func transmitter(id string, ws *websocket.Conn, c chan *registrar.T, ctx context.Context, cancel context.CancelFunc) {
	var err error
	var data *registrar.T
	defer ws.Close()
	//defer close(c)
	defer cancel()
	defer registrar.RemoveConnection(id)
Loop:
	for {
		select {
		case data = <-c:
			err = websocket.JSON.Send(ws, *data)
			//websocket.Message.Send(ws, data.Msg)
			if err != nil {
				if !ws.IsClientConn() {
					log.Printf("transmitter closed\n")
				} else {
					log.Printf("transmitter error %v\n", err)
				}
				break Loop
			}
		case <-ctx.Done():
			log.Printf("transmitter closed")
			break Loop
		}
	}
}
Esempio n. 3
0
func writer(ws *websocket.Conn, me string, c chan *T) {

	conn, err := net.Dial("tcp", "173.39.210.64:2003")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	for {

		var err error
		var b []byte
		if !ws.IsClientConn() {
			break
		}

		pkt := <-c
		b, err = base64.StdEncoding.DecodeString(pkt.Msg)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Printf("Packet received in writer - let's send to Collectd (graphite currently) - %v\n", len(b))
		_, err = conn.Write(b)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

	}

}
Esempio n. 4
0
func reader(ws *websocket.Conn, id string, cancel context.CancelFunc, c chan *T) {

	//var err error
	for {

		if !ws.IsClientConn() {
			log.Fatal("No Connection")
			cancel()
		}

		var data *T = new(T)
		err := websocket.JSON.Receive(ws, data)
		if err != nil {
			log.Fatal(err)
		}
		c <- data
		//LogConditional(printLog, fmt.Printf, "\nReceived message : %s\n", data.Msg)

	}
}
Esempio n. 5
0
func reader(ws *websocket.Conn, id string) {

	//var err error
	for {

		if !ws.IsClientConn() {
			log.Fatal("No Connection")
			break
		}

		var data *T = new(T)
		err := websocket.JSON.Receive(ws, data)
		if err != nil {
			log.Fatal(err)
		}

		LogConditional(printLog, fmt.Printf, "\nReceived message : %s\n", data.Msg)
		if data.Reply {
			if data.From != id {
				LogConditional(printLog, fmt.Printf, "Need to Reply to %v\n", data.From)
				data.To = data.From
				data.From = id
				data.Reply = false
				data.Msg = fmt.Sprintf("%s%s", "Back at you ------- ", data.Msg)
				err := websocket.JSON.Send(ws, *data)
				if err != nil {
					log.Fatal(err)
				}
			}
		} else {
			if R.isWaiting(data.MsgId) {
				R.Lock()
				channel := R.waiting[data.MsgId].channel
				R.Unlock()
				channel <- data.MsgId
			}
		}

	}
}
Esempio n. 6
0
func writer(ws *websocket.Conn, me string) {
	var i int
	var to string
	var msg string

	for {

		if !ws.IsClientConn() {
			break
		}

		time.Sleep(100 * time.Millisecond)
		fmt.Print("\n\nSend To: ")
		fmt.Scanf("%s", &to)
		fmt.Print("Message: ")
		fmt.Scanf("%s", &msg)
		for i, R.messagesReceived = 0, 0; i < loopCount; i++ {
			var data *T = new(T)
			//data.MsgId = time.Now().UnixNano()
			data.Reply = true
			data.MsgId = int64(i)
			data.To = to
			data.Msg = msg
			data.From = me
			r := response{msgId: data.MsgId, channel: make(chan int64)}
			R.Lock()
			R.waiting[data.MsgId] = r
			R.Unlock()
			go replyWaiter(data.MsgId, r.channel)
			err := websocket.JSON.Send(ws, data)
			if err != nil {
				log.Fatal(err)
			}
		}

	}

}