Esempio n. 1
0
//implement part2, after all improvements
func part2_bars(width int64, barHeight int64) {
	x := d3.ScaleLinear().Range([]int64{0, width})

	chart := d3.Select(pickChart2).Attr(propWidth, width)

	//read sample data from the server
	d3.TSV("sample.tsv", filterIntData, func(err js.Object, data js.Object) {
		if !err.IsNull() {
			console.Error(err)
			return
		}
		x.Domain([]int64{0, d3.Max(data, extractValue)})
		chart.Attr(propHeight, barHeight*int64(data.Length()))

		bar := chart.SelectAll(pickG).Data(data).Enter().Append(gTag)
		bar.AttrFunc2S(propXform, func(d js.Object, i int64) string {
			return fmt.Sprintf("translate(0,%d)", i*barHeight)
		})
		rect := bar.Append(rectTag)
		rect.AttrFunc(propWidth, x.Func(extractValue)).Attr(propHeight, barHeight-1)

		text := bar.Append(textTag)
		text.AttrFunc(propX, func(d js.Object) int64 {
			return x.Linear(d, extractValue) - int64(3)
		})

		text.Attr(propY, barHeight/2).AttrS(propDy, ".35em")
		text.Text(func(d js.Object) string {
			return fmt.Sprintf("%s:%d", d.Get("name"), extractValue(d))
		})

	})
}
Esempio n. 2
0
func (g *gateway) start() {
	for {
		buf := make([]byte, 1024)
		n, err := g.conn.Read(buf) // Blocks until a WebSocket frame is received
		if err == nil {
			g.handleMessage(buf[:n])
		} else {
			console.Error(err.Error())
		}
	}
}
Esempio n. 3
0
//convert object with string fields into one with parsed fields
func filterIntData(obj js.Object) js.Object {
	result := js.Global.Get("Object").New()
	result.Set("name", obj.Get("name").Str())
	s := obj.Get("value").Str()
	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		console.Error("unable to parse ", s, " in the dataset: IGNORED")
		return nil
	}
	result.Set("value", i)
	return result
}
Esempio n. 4
0
func filterFloatData(obj js.Object) js.Object {
	result := js.Global.Get("Object").New()
	result.Set("letter", obj.Get("letter").Str())
	s := obj.Get("frequency").Str()
	f, err := strconv.ParseFloat(s, 64)
	if err != nil {
		console.Error("unable to parse ", s, " in the dataset: IGNORED")
		return nil
	}
	result.Set("frequency", f)
	return result

}
Esempio n. 5
0
func printLog(prefix string, printTrace bool, message string, v ...interface{}) {

	mess := fmt.Sprintf("%s %s %s", time.Now().Format(timeFormat), prefix, fmt.Sprintf(message, v...))

	switch prefix {
	case WarnPrefix:
		console.Warn(mess)
	case ErrPrefix:
		console.Error(mess)
	default:
		console.Log(mess)
	}
}
Esempio n. 6
0
func newGateway() *gateway {
	win := dom.GetWindow()
	doc := win.Document()

	wsEndpoint := doc.GetElementByID("ws-endpoint").(dom.HTMLElement).TextContent()
	statusEl := doc.GetElementByID("status").(dom.HTMLElement)

	canvas := newCanvas(doc.GetElementByID("board").(*dom.HTMLCanvasElement))

	statusEl.SetTextContent("Connecting")
	conn := connect(wsEndpoint)
	statusEl.SetTextContent("Waiting To Play")

	wsSend := make(chan string)

	gw := &gateway{conn: conn, send: wsSend, statusEl: statusEl, canvas: canvas}

	// start send loop (send over websocket to server)
	go func(s chan string) {
		for {
			msg := <-gw.send

			_, err := conn.Write([]byte(msg))
			if err != nil {
				console.Error(err.Error())
			}
		}
	}(wsSend)

	// listen for canvas events
	go func() {
		for {
			e := <-canvas.event

			parts := strings.Split(e, ",")
			if parts[0] == "L" {
				gw.processLostEvent()
			} else if parts[0] == "N" {
				gw.processNetExchangeEvent(e)
			} else {
				console.Log(fmt.Sprintf("unsupported event: %s\n", e))
			}
		}
	}()

	return gw
}
Esempio n. 7
0
func part3_bars(overall_width, overall_height, top, right, bottom, left int64) {

	width := overall_width - left - right
	height := overall_height - top - bottom

	x := d3.ScaleOrdinal().RangeBands3([]int64{0, width}, 0.1)
	y := d3.ScaleLinear().Range([]int64{height, 0})

	xAxis := d3.NewAxis().ScaleO(x).Orient(d3.BOTTOM)
	yAxis := d3.NewAxis().Scale(y).Orient(d3.LEFT).Ticks(10, "%")

	chart := d3.Select(pickChart3).Attr(propWidth, width+left+right).
		Attr(propHeight, height+top+bottom).Append(gTag).
		AttrS(propXform, fmt.Sprintf("translate(%d,%d)", left, top))

	d3.TSV("letter_freq.tsv", filterFloatData, func(err js.Object, data js.Object) {
		if !err.IsNull() {
			console.Error(err)
			return
		}
		x.Domain(extractAllLetters(data))
		y.DomainF([]float64{0.0, d3.MaxF(data, extractFreq)})

		//AXES
		chart.Append(gTag).AttrS(propClass, "x axis").AttrS("transform",
			fmt.Sprintf("translate(0,%d)", height)).Call(xAxis)

		yText := chart.Append(gTag).AttrS(propClass, "y axis").Call(yAxis).Append(textTag)
		yText.AttrS(propXform, "rotate(-90)").Attr(propY, 6).
			AttrS(propDy, "0.71em").StyleS(propTextAnchor, "end").TextS("Frequency")

		//BAR
		rect := chart.SelectAll(pickBar).Data(data).Enter().Append(rectTag)
		rect.AttrS(propClass, "bar")
		rect.AttrFunc(propX, func(d js.Object) int64 {
			return x.Ordinal(d, extractLetter)
		})
		rect.AttrFuncF(propY, func(d js.Object) float64 {
			return y.LinearF(d, extractFreq)
		})
		rect.AttrFuncF(propHeight, func(obj js.Object) float64 {
			return float64(height) - y.LinearF(obj, extractFreq)
		})
		rect.AttrF(propWidth, x.RangeBandF())
	})
}
Esempio n. 8
0
func connect(wsEndpoint string) *websocket.Conn {
	count := 0

	ticker := time.NewTicker(time.Duration(1) * time.Second)
	defer ticker.Stop()

	// connect
	for {
		count++

		console.Log(fmt.Printf("trying to connect to server at %s, attempt %d", wsEndpoint, count))

		conn, err := websocket.Dial(wsEndpoint) // Blocks until connection is established
		if err == nil {
			return conn
		}
		console.Error(err.Error())

		<-ticker.C
	}
}
Esempio n. 9
0
func GenerateCode(target, options string) (string, string) {
	var globalOptions GlobalOptions
	var curlOptions common.CurlOptions
	curlOptions.Init()

	parser := flags.NewParser(&globalOptions, flags.Default)
	curlCommand, err := parser.AddCommand("curl",
		"Generate code from curl options",
		"This command has almost same options of curl and generate code",
		&curlOptions)

	if !strings.HasPrefix(options, "curl ") {
		options = "curl " + options
	}
	args := shell.Parse(options)

	urls, err := parser.ParseArgs(args)
	if err != nil {
		console.Log(err)
		return "", err.Error()
	}
	if len(urls) > 1 {
		return "", "It accept only one url. Remained urls are ignored:" + strings.Join(urls, ", ")
	}
	if parser.Active == curlCommand {
		// --url option has higher priority than params.
		if curlOptions.Url == "" {
			if len(urls) > 0 {
				curlOptions.Url = urls[0]
			} else {
				console.Error("Both --url option and url parameters are missing")
				return "", "Both --url option and url parameters are missing"
			}
		}
		sourceCode, _, _, _ := generator.GenerateCode(target, &curlOptions)
		return html.EscapeString(sourceCode), ""
	}
	return "", ""
}