Beispiel #1
0
func main() {
	var connFile = flag.String("connection-file", "", "Path to connection file")
	flag.Parse()

	if *connFile == "" {
		fmt.Fprint(os.Stderr, "Connection file is required\n")
		flag.Usage()
		os.Exit(2)
	}

	connInfo, err := juno.NewConnectionInfo(*connFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to open connection file\n")
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	ioConnection := connInfo.IOPubConnectionString()
	iopub := zmq.NewSubChanneler(ioConnection, "")

	defer iopub.Destroy()

	broker := ssebroker.NewServer()

	go func() {
		for {
			select {
			case wireMessage := <-iopub.RecvChan:
				var message juno.Message
				err := message.ParseWireProtocol(wireMessage, connInfo)
				if err != nil {
					fmt.Fprintf(os.Stderr, "Unable to read message %v\n", err)
				}

				b, err := json.Marshal(message)

				broker.Notifier <- b
			}
		}
	}()

	log.Fatal("HTTP server error: ", http.ListenAndServe("localhost:3000", broker))

}
Beispiel #2
0
func main() {
	var connFile = flag.String("connection-file", "", "Path to connection file")
	flag.Parse()

	if *connFile == "" {
		fmt.Fprint(os.Stderr, "Connection file is required\n")
		flag.Usage()
		os.Exit(2)
	}

	connInfo, err := juno.NewConnectionInfo(*connFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to open connection file\n")
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	ioConnection := connInfo.IOPubConnectionString()
	iopub := zmq.NewSubChanneler(ioConnection, "")

	defer iopub.Destroy()

	// Listen for messages... forever!
	for {
		select {
		case wireMessage := <-iopub.RecvChan:
			var message juno.Message
			err := message.ParseWireProtocol(wireMessage, connInfo)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Unable to read message %v\n", err)
			}
			fmt.Println(message)
		}
	}

}
Beispiel #3
0
func main() {
	connFile := flag.String("existing", "", "Path to connection file")
	ioloHub := flag.String("hub", "http://iolo.fict.io:80", "IOLO Hub Base URL")
	flag.Parse()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Println()
	}

	if *connFile == "" {
		flag.Usage()
		log.Fatalln("Need a connection file.")
	}

	// Expects a runtime kernel-*.json
	connInfo, err := juno.NewConnectionInfo(*connFile)

	if err != nil {
		log.Fatalf("%v\n", err)
	}

	u, err := url.Parse(*ioloHub)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to parse URL: %v", err)
		os.Exit(3)
	}

	u.Path = "/ws"

	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to host: %v", err)
		os.Exit(4)
	}

	wsHeaders := http.Header{
		"Origin":                   {u.Scheme + "://" + u.Host},
		"Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
	}

	writeBufferSize := 1024 * 1024 * 2

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 0, writeBufferSize)
	if err != nil {
		fmt.Fprintf(os.Stderr, "websocket.NewClient Error: %s\nResp:%+v", err, resp)
		os.Exit(5)
	}
	defer wsConn.Close()

	ioConnection := connInfo.IOPubConnectionString()
	iopub := zmq.NewSubChanneler(ioConnection, "")
	defer iopub.Destroy()

	for {
		select {
		case wireMessage := <-iopub.RecvChan:
			var message juno.Message
			err := message.ParseWireProtocol(wireMessage, connInfo)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Unable to read message %v\n", err)
				continue
			}
			err = wsConn.WriteJSON(message)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Problem writing JSON %v.\nOutput too big?", err)
				break
			}
		}
	}

}
Beispiel #4
0
func main() {
	pub := os.Getenv("PUB_PORT_5000_TCP")
	if pub == "" {
		log.Fatal("Missing PUB_PORT_5000_TCP environment variable")
	}
	rep := os.Getenv("PUB_PORT_5001_TCP")
	if rep == "" {
		log.Fatal("Missing PUB_PORT_5001_TCP environment variable")
	}
	reqL.Lock()
	var err error
	req, err = goczmq.NewReq(rep)
	reqL.Unlock()
	if err != nil {
		log.Fatal("Error connecting to REP %q: %q\n", rep, err.Error())
	}

	fields[0] = cell.RandomField(128, 128)
	go func() {
		sub := goczmq.NewSubChanneler(pub, "" /* all messages, no topic filtering */)
		defer sub.Destroy()

		for msg := range sub.RecvChan {
			if len(msg) != 1 {
				log.Printf(
					"Message had unexpected number of frames: %d, want 1.\n",
					len(msg))
				continue
			}
			data := msg[0]

			pf := &cell.FieldProto{}
			if err := proto.Unmarshal(data, pf); err != nil {
				log.Printf(
					"Error unmarshalling message:\n\t%q,\n\t%#v\n",
					err.Error(),
					msg)
				continue
			}
			seq := int(pf.Seq) % cacheSize
			if seq < 0 {
				seq = seq + cacheSize
			}

			f, err := cell.FromProto(pf)
			if err != nil {
				log.Printf("Got invalid Field:\n\t%q,\n\t%#v\n", err.Error(), f)
				continue
			}

			cond.L.Lock()
			curr = seq
			fields[curr] = f
			cond.Broadcast()
			cond.L.Unlock()
		}
	}()

	http.HandleFunc("/socket", socketHandler)
	http.HandleFunc("/", viewHandler)
	http.HandleFunc("/field/", fieldHandler)
	http.ListenAndServe(":8080", nil)
}
Beispiel #5
0
func main() {
	var connFile = flag.String("connection-file", "", "Path to connection file")
	var lampostServer = flag.String("lampost-server", "https://lampost.lambdaops.com", "URL to a lampost server")
	flag.Parse()

	if *connFile == "" {
		fmt.Fprint(os.Stderr, "Connection file is required\n")
		flag.Usage()
		os.Exit(2)
	}

	connInfo, err := juno.NewConnectionInfo(*connFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to open connection file\n")
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	ioConnection := connInfo.IOPubConnectionString()
	iopub := zmq.NewSubChanneler(ioConnection, "")

	defer iopub.Destroy()

	var wg sync.WaitGroup
	wg.Add(1)

	go func() {
		defer wg.Done()
		for {
			select {
			case wireMessage := <-iopub.RecvChan:
				var message juno.Message
				err := message.ParseWireProtocol(wireMessage, connInfo)

				if err != nil {
					fmt.Fprintf(os.Stderr, "Unable to read message %v\n", err)
				}

				b, err := json.Marshal(message)
				reader := bytes.NewReader(b)

				resp, err := http.Post(*lampostServer+"/api/ioju", "application/json", reader)
				if err != nil {
					fmt.Fprintf(os.Stderr, "Unable to POST to %v with %v\n", *lampostServer, message)
					fmt.Fprintln(os.Stderr, err)
					if resp != nil {
						fmt.Fprintf(os.Stderr, "[%v] %v\n", resp.StatusCode, resp.Status)
					}
					return
				}

				if resp.StatusCode != 200 {
					fmt.Printf("[%v] %v\n", resp.StatusCode, resp.Status)
				}

			}
		}
		// This code is unreachable, but you would normally
		//     defer wg.Done()
		// once the goroutine was finished
	}()

	wg.Wait()

}