// Reference: zebra_hello_send function in lib/zclient.c of Quagga1.2.x (ZAPI3)
// Reference: zebra_hello_send function in lib/zclient.c of FRR3.x (ZAPI4)
// Reference: zebra_hello_send function in lib/zclient.c of FRR5.x (ZAPI5)
500368
func (b *HelloBody) Serialize(version uint8) ([]byte, error) {
	if version <= 3 {
		return []byte{uint8(b.RedistDefault)}, nil
	} else { // version >= 4
		var buf []byte
		if version == 4 {
			buf = make([]byte, 3)
		} else if version >= 5 {
			buf = make([]byte, 4)
		}
		buf[0] = uint8(b.RedistDefault)
		binary.BigEndian.PutUint16(buf[1:3], b.Instance)
		if version >= 5 {
			buf[3] = b.ReceiveNotify
		}
		return buf, nil
	}
}
727920
func UnmarshalToCallback(in io.Reader, f interface{}) error {
	valueFunc := reflect.ValueOf(f)
	t := reflect.TypeOf(f)
	if t.NumIn() != 1 {
		return fmt.Errorf("the given function must have exactly one parameter")
	}
	cerr := make(chan error)
	c := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, t.In(0)), 0)
	go func() {
		cerr <- UnmarshalToChan(in, c.Interface())
	}()
	for {
		select {
		case err := <-cerr:
			return err
		default:
		}
		v, notClosed := c.Recv()
		if !notClosed || v.Interface() == nil {
			break
		}
		valueFunc.Call([]reflect.Value{v})
	}
	return nil
}
264371
func ZmqReplySocket(ip string, reply_ports []int) *zmq.Socket {
	context, _ := zmq.NewContext()
	socket, _ := context.NewSocket(zmq.REP)
	for _, _port := range reply_ports {
		socket.Bind(fmt.Sprintf("tcp://%s:%d", ip, _port))
	}

	return socket
}
978088
func main() {
	// Imports the hello component declared above in order to make it loadable
	// in a page or usable in other components.
	//
	// Imported component can be used as URL or html tags by referencing them by
	// their lowercased names.
	// E.g:
	//  Hello   => hello
	//  foo.Bar => foo.bar
	app.Import(&Hello{})

	// Defines the component to load when an URL without path is loaded.
	app.DefaultPath = "/hello"

	// Runs the app in the browser.
	if err := app.Run(); err != nil {
		log.Print(err)
	}
}
464096
func client_task(name string, i int) {
	clientname := fmt.Sprintf("Client-%s-%d", name, i)

	client, _ := zmq.NewSocket(zmq.REQ)
	defer client.Close()
	client.SetIdentity(clientname)
	client.Connect("ipc://" + name + "-localfe.ipc")

	for {
		//  Send request, get reply
		client.Send("HELLO from "+clientname, 0)
		reply, err := client.Recv(0)
		if err != nil {
			fmt.Println("client_task interrupted", name)
			break //  Interrupted
		}
		fmt.Printf("%s: %s\n", clientname, reply)
		time.Sleep(time.Duration(500+rand.Intn(1000)) * time.Millisecond)
	}
}
570681
func (soc *Socket) SendBytes(data []byte, flags Flag) (int, error) {
	if !soc.opened {
		return 0, ErrorSocketClosed
	}
	datac := C.CString(string(data))
	var msg C.zmq_msg_t
	if i, err := C.my_msg_init_data(&msg, unsafe.Pointer(datac), C.size_t(len(data))); i != 0 {
		return -1, errget(err)
	}
	defer C.zmq_msg_close(&msg)
	n, err := C.zmq_send(soc.soc, &msg, C.int(flags))
	if n != 0 {
		return -1, errget(err)
	}
	return int(n), nil
}
491491
func (s *hwServer) SayHello(ctx context.Context, in *hwpb.HelloRequest) (*hwpb.HelloReply, error) {
	return &hwpb.HelloReply{Message: "Hello " + in.Name}, nil
}
1308211
func (p *Plugin) NewAsyncPublisherToPartition(connectionName string, topic string, partition int32, successClb func(messaging.ProtoMessage), errorClb func(messaging.ProtoMessageErr)) (messaging.ProtoPublisher, error) {
	return p.NewProtoManualConnection(connectionName).NewAsyncPublisherToPartition(topic, partition, successClb, errorClb)
}
153057
func Greeting(r *rand.Rand, name string) string {
	var n int
	if len(name) == 0 {
		n = r.Intn(3)
		switch n {
		case 0:
			return fmt.Sprintf("Hi, %s.", name)
		case 1:
			return fmt.Sprintf("Hello, %s.", name)
		case 2:
			return fmt.Sprintf("Hi there, %s.", name)
		}
	} else {
		n = r.Intn(3)
		switch n {
		case 0:
			return "Hi. How can I help you?"
		case 1:
			return "Hello. What can I do for you?"
		}
	}
	log.Debug("greeting failed to return a response")
	return ""
}
1170494
func SendXmpp(senderId, apiKey string, m XmppMessage) (string, int, error) {
	c, err := newXmppGcmClient(senderId, apiKey)
	if err != nil {
		return "", 0, fmt.Errorf("error creating xmpp client>%v", err)
	}
	return c.send(m)
}
1331551
func sendToLibhoney(ctx context.Context, toBeSent chan event.Event, toBeResent chan event.Event,
	delaySending chan int, doneSending chan bool) {
	for {
		// check and see if we need to back off the API because of rate limiting
		select {
		case delay := <-delaySending:
			time.Sleep(time.Duration(delay) * time.Millisecond)
		default:
		}
		// if we have events to retransmit, send those first
		select {
		case ev := <-toBeResent:
			// retransmitted events have already been sampled; always use
			// SendPresampled() for these
			sendEvent(ev)
			continue
		default:
		}
		// otherwise pick something up off the regular queue and send it
		select {
		case ev, ok := <-toBeSent:
			if !ok {
				// channel is closed
				// NOTE: any unrtransmitted retransmittable events will be dropped
				doneSending <- true
				return
			}
			sendEvent(ev)
			continue
		default:
		}
		// no events at all? chill for a sec until we get the next one
		time.Sleep(100 * time.Millisecond)
	}
}
429061
func main() {

	const htmlBody = `
<!DOCTYPE html>
<html>
  <head>
    <link href="/hello.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <h1>Hello, Gear!</h1>
  </body>
</html>`

	const pushBody = `
h1 {
  color: red;
}
`

	app := gear.New()

	app.UseHandler(logging.Default(true))
	app.Use(favicon.New("./testdata/favicon.ico"))

	router := gear.NewRouter()
	router.Get("/", func(ctx *gear.Context) error {
		ctx.Res.Push("/hello.css", &http.PushOptions{Method: "GET"})
		return ctx.HTML(200, htmlBody)
	})
	router.Get("/hello.css", func(ctx *gear.Context) error {
		ctx.Type("text/css")
		return ctx.End(200, []byte(pushBody))
	})
	app.UseHandler(router)
	app.Error(app.ListenTLS(":3000", "./testdata/out/test.crt", "./testdata/out/test.key"))
}
552066
func greeting(p messenger.Profile, r *messenger.Response) error {
	return r.Text(fmt.Sprintf("Hello, %v!", p.FirstName), messenger.ResponseType)
}
558076
func (*GreeterService) SayHello(ctx context.Context, request *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
	if request.Name == "" {
		return nil, errors.New("not filled name in the request")
	}

	logging.Infof("Greeting client: %v", request.Name)

	return &helloworld.HelloReply{Message: "Greetings " + request.Name}, nil
}
958080
func main() {
	snapshot, _ := zmq.NewSocket(zmq.ROUTER)
	snapshot.Bind("tcp://*:5556")
	publisher, _ := zmq.NewSocket(zmq.PUB)
	publisher.Bind("tcp://*:5557")
	collector, _ := zmq.NewSocket(zmq.PULL)
	collector.Bind("tcp://*:5558")

	//  The body of the main task collects updates from clients and
	//  publishes them back out to clients:

	sequence := int64(0)
	kvmap := make(map[string]*kvsimple.Kvmsg)

	poller := zmq.NewPoller()
	poller.Add(collector, zmq.POLLIN)
	poller.Add(snapshot, zmq.POLLIN)
LOOP:
	for {
		polled, err := poller.Poll(1000 * time.Millisecond)
		if err != nil {
			break
		}
		for _, item := range polled {
			switch socket := item.Socket; socket {
			case collector:
				//  Apply state update sent from client
				kvmsg, err := kvsimple.RecvKvmsg(collector)
				if err != nil {
					break LOOP //  Interrupted
				}
				sequence++
				kvmsg.SetSequence(sequence)
				kvmsg.Send(publisher)
				kvmsg.Store(kvmap)
				fmt.Println("I: publishing update", sequence)
			case snapshot:
				//  Execute state snapshot request
				msg, err := snapshot.RecvMessage(0)
				if err != nil {
					break LOOP
				}
				identity := msg[0]

				//  Request is in second frame of message
				request := msg[1]
				if request != "ICANHAZ?" {
					fmt.Println("E: bad request, aborting")
					break LOOP
				}
				subtree := msg[2]
				//  Send state snapshot to client

				//  For each entry in kvmap, send kvmsg to client
				for _, kvmsg := range kvmap {
					if key, _ := kvmsg.GetKey(); strings.HasPrefix(key, subtree) {
						snapshot.Send(identity, zmq.SNDMORE)
						kvmsg.Send(snapshot)
					}
				}

				//  Now send END message with sequence number
				fmt.Println("I: sending shapshot =", sequence)
				snapshot.Send(identity, zmq.SNDMORE)
				kvmsg := kvsimple.NewKvmsg(sequence)
				kvmsg.SetKey("KTHXBAI")
				kvmsg.SetBody(subtree)
				kvmsg.Send(snapshot)
			}
		}
	}
	fmt.Printf("Interrupted\n%d messages handled\n", sequence)
}
599242
func HashIndexFunction(env *Zlisp, name string, args []Sexp) (Sexp, error) {
	Q("in HashIndexFunction, with %v args = '%#v', env=%p",
		len(args), args, env)
	for i := range args {
		Q("in HashIndexFunction, args[%v] = '%v'", i, args[i].SexpString(nil))
	}
	narg := len(args)
	if narg != 2 {
		return SexpNull, WrongNargs
	}
	tmp, err := env.ResolveDotSym([]Sexp{args[0]})
	if err != nil {
		return SexpNull, err
	}
	args[0] = tmp[0]
	Q("HashIndexFunction: past dot resolve, args[0] is now type %T/val='%v'",
		args[0], args[0].SexpString(nil))

	var hash *SexpHash
	switch ar0 := args[0].(type) {
	case *SexpHash:
		hash = ar0
	case *SexpArray:
		Q("HashIndexFunction: args[0] is an array, defering to ArrayIndexFunction")
		return ArrayIndexFunction(env, name, args)
	case Selector:
		x, err := ar0.RHS(env)
		Q("ar0.RHS() returned x = %#v", x)
		if err != nil {
			Q("HashIndexFunction: Selector error: '%v'", err)
			return SexpNull, err
		}
		switch xH := x.(type) {
		case *SexpHash:
			hash = xH
		case *SexpHashSelector:
			x, err := xH.RHS(env)
			if err != nil {
				Q("HashIndexFunction: hash retreival from "+
					"SexpHashSelector gave error: '%v'", err)
				return SexpNull, err
			}
			switch xHash2 := x.(type) {
			case *SexpHash:
				hash = xHash2
			default:
				return SexpNull, fmt.Errorf("bad (hashidx h2 index) call: h2 was a hashidx itself, but it did not resolve to an hash, instead '%s'/type %T", x.SexpString(nil), x)
			}
		case *SexpArray:
			Q("HashIndexFunction sees args[0] is Selector"+
				" that resolved to an array '%v'", xH.SexpString(nil))
			return ArrayIndexFunction(env, name, []Sexp{xH, args[1]})
		default:
			return SexpNull, fmt.Errorf("bad (hashidx h index) call: h did not resolve to a hash, instead '%s'/type %T", x.SexpString(nil), x) // failing here with x a  *SexpStr
		}
	default:
		return SexpNull, fmt.Errorf("bad (hashidx h index) call: h was not a hashmap, instead '%s'/type %T",
			args[0].SexpString(nil), args[0])
	}

	sel := args[1]
	switch x := sel.(type) {
	case *SexpSymbol:
		sel = x
		/*
			if x.isDot {
				Q("hashidx sees dot symbol: '%s', removing any prefix dot", x.name)
				if len(x.name) >= 2 && x.name[0] == '.' {
					selSym := env.MakeSymbol(x.name[1:])
					//selSym.isDot = true
					sel = selSym
				}
			}
		*/
	default:
		// okay to have SexpArray/other as selector
	}

	ret := SexpHashSelector{
		Select:    sel,
		Container: hash,
	}
	Q("HashIndexFunction: returning without error, ret.Select = '%v'", args[1].SexpString(nil))
	return &ret, nil
}
1345482
func (cniApp *cniAppInfo) sendCniResp(cniResp []byte, retCode int) int {
	cniLog.Infof("sent CNI response: %s ", cniResp)
	fmt.Printf("%s\n", string(cniResp))
	return retCode
}