Example #1
0
File: main.go Project: resc/jsonbus
func runserver() {
	bus := NewBus(BusConfig{Address: *addr, Port: *port})
	err := bus.Serve()
	if err != nil {
		fmt.Panic("Error serving: %v", err)
	}
}
Example #2
0
File: config.go Project: resc/core
func (cfg *Config) Section(name string) *Section {
	if s, ok := cfg.sections[name]; ok {
		return s
	}
	fmt.Panic("Unknown config section: %v", name)
	return nil
}
Example #3
0
File: main.go Project: resc/jsonbus
func receive(conn *ws.Conn) *Msg {
	msg := new(Msg)
	err := ws.JSON.Receive(conn, msg)
	if err != nil {
		if err == io.EOF {
			fmt.Printline("Connection closed")
			return nil
		}

		fmt.Panic("Error receiving message: " + err.Error())
	} else {
		if !*quiet {
			text, err := json.MarshalIndent(msg, "", "  ")
			if err != nil {
				fmt.Panic("Error formatting message for display: " + err.Error())
			}
			fmt.Printline("Received:\n%v", string(text))
		}
	}

	return msg
}
Example #4
0
File: tree.go Project: resc/jsonbus
func (n *Node) Compact() {
	if n.Handle != InvalidHandle || n.isLeaf() {
		return
	}

	// get the common prefix length of the children
	prefix := n.Nodes[0].Prefix
	l := len(prefix)
	for i := 1; i < len(n.Nodes); i++ {
		l = min(l, CommonPrefixLength(prefix, n.Nodes[i].Prefix))
		if l == 0 {
			// no common prefix, bail out
			return
		}
	}

	// hoist the common child prefix
	n.Prefix = n.Prefix + prefix[:l]

	// update child prefix and hoist empty prefix child, if any
	lnodes := len(n.Nodes)
	for i := 0; i < lnodes; i++ {
		child := &n.Nodes[i]
		child.Prefix = child.Prefix[l:]
		if len(child.Prefix) == 0 {
			if n.Handle != InvalidHandle {
				// this should never happen.
				fmt.Panic("More than 1 empty prefix child found during compaction")
			}
			// move child data
			n.Handle = child.Handle
			n.Nodes = append(n.Nodes, child.Nodes...)

			// and remove the empty node
			last := len(n.Nodes) - 1
			n.Nodes[i], n.Nodes[last] = n.Nodes[last], Node{}
			n.Nodes = n.Nodes[:last]
		}
	}
}
Example #5
0
File: main.go Project: resc/jsonbus
func runclient() {
	origin := fmt.String("http://%v/", *addr)
	url := fmt.String("ws://%v:%v/jsonbus", *addr, *port)
	conn, err := ws.Dial(url, "", origin)
	if err != nil {
		fmt.Panic("Error connecting to server: %v", err)
	}

	defer conn.Close()
	err = ws.JSON.Send(conn, NewMessage("", "", &Hello{}))
	if err != nil {
		fmt.Panic("Error sending hello: " + err.Error())
	}

	msg := receive(conn)
	if msg == nil {
		return
	}

	if msg.Type != "HelloAck" {
		fmt.Panic("Protocol error, expected HelloAck")

	}

	ack := new(HelloAck)
	err = json.Unmarshal(msg.Body, ack)
	if err != nil {
		fmt.Panic(err.Error())
	}

	msgIds := GenerateMessageIds(ack.ConnId)
	shutdown := make(chan struct{})
	go func() {
		defer close(shutdown)
		for {
			if receive(conn) == nil {
				return
			}
		}
	}()

	if sub != nil && *sub {
		err := ws.JSON.Send(conn, NewMessage(<-msgIds, "", &Subscribe{Topic: *topic}))
		if err != nil {
			fmt.Panic("Error sending subscription: " + err.Error())
		}
	}

	if pub != nil && *pub {
		count := 1
		start := time.Now()
		spamcount := 100000
		if *spam {
			count = spamcount
		}

		for ; count > 0; count-- {
			err := ws.JSON.Send(conn, NewMessage(<-msgIds, "", &Publish{Topic: *topic, Message: *message}))
			if err != nil {
				fmt.Panic("Error sending subscription: " + err.Error())
			}
		}

		if *spam {
			d := time.Since(start)
			fmt.Printline("Spamming done %v in %v: %v/second.", spamcount, d, float64(spamcount)/d.Seconds())
		}

	}
	<-shutdown
}