Пример #1
0
func doInterface() {
	m, _ := ma.NewMultiaddr("/ip4/127.0.0.1")
	ps := []IPeer{
		Peer{"me", [][]byte{m.Bytes()}, nil},
		Peer{"they", [][]byte{m.Bytes()}, map[string]interface{}{"age": 3}},
	}
	fmt.Printf("Encoding: %+v\n", ps)

	var buf bytes.Buffer
	mx := mux.StandardMux()
	mx.Encoder(&buf).Encode(ps)

	fmt.Printf("Encoded buffer: %s\n", buf.String())

	// Now we'll decode the []Peer structure into []IPeer slice
	// without mentioning the type Peer.
	dec := mx.Decoder(&buf)
	typ := reflect.SliceOf(reflect.TypeOf(ps[0]))
	val, err := reflectDecode(dec, typ)
	if err != nil {
		panic(err)
	}

	v := make([]IPeer, val.Len())
	for i := 0; i < val.Len(); i++ {
		v[i] = val.Index(i).Interface().(IPeer)
	}

	fmt.Printf("Decoded: %+v\n", v)
}
Пример #2
0
func (b *Broadcast) broadcaster(in <-chan msgInfo) {
	mx := mux.StandardMux()
	for {
		mi, ok := <-in
		if !ok {
			return
		}

		buf := bytes.Buffer{}
		mx.Encoder(&buf).Encode(mi.msg)
		bts := buf.Bytes()

		b.neighbsmu.RLock()
		for conn, _ := range b.neighbsPri {
			if mi.sender != conn {
				go io.Copy(conn, bytes.NewReader(bts))
			}
		}
		for conn, _ := range b.neighbsSec {
			if mi.sender != conn {
				go io.Copy(conn, bytes.NewReader(bts))
			}
		}
		b.neighbsmu.RUnlock()
	}
}
Пример #3
0
func doStdin() {
	c := mux.StandardMux()
	dec := c.Decoder(os.Stdin)

	var v interface{}
	if err := dec.Decode(&v); err != nil {
		panic(err)
	}

	fmt.Println(v)
}
Пример #4
0
func (cm CommandMap) Call(name string, args []string, stdio io.ReadWriter, apiaddr ma.Multiaddr) byte {
	cmd, ok := cm[name]
	if !ok {
		fmt.Printf("Unknown command: %s\n", name)
		return 1
	}

	switch cmd := cmd.(type) {

	case *LocalCommand:
		return cmd.Run(args, stdio)

	case *RemoteCommand:

		// Connect to daemon
		conn, err := manet.Dial(apiaddr)
		if err != nil {
			fmt.Println(err)
			return 1
		}
		defer conn.Close()

		// Send arguments
		mx := mux.StandardMux()
		err = mx.Encoder(conn).Encode(append([]string{name}, args...))
		if err != nil {
			fmt.Println(err)
			return 1
		}

		// Multiplex connection
		remoteStdio, err := WithErrorCode(conn)
		if err != nil {
			fmt.Println(err)
			return 1
		}

		// Join stdout and stdin with remote ones
		go io.Copy(remoteStdio, stdio)
		_, err = io.Copy(stdio, remoteStdio)
		if err != nil {
			fmt.Println(err)
			return 1
		}

		return <-remoteStdio.ErrorCodeCh()

	default:
		fmt.Printf("Unknown type of command: %s\n", name)
		return 1
	}
}
Пример #5
0
func (b *Broadcast) msgAccepter(rwc io.ReadWriteCloser, out chan<- msgInfo, closed chan<- io.ReadWriteCloser) {
	mx := mux.StandardMux()
	for {
		m := &Msg{}
		err := mx.Decoder(rwc).Decode(m)
		if err == io.EOF || err == io.ErrClosedPipe {
			closed <- rwc
			return
		} else if err != nil {
			panic(err)
		}
		out <- msgInfo{m, rwc}
	}
}
Пример #6
0
func doStruct() {
	s := MyStruct{"world", "earth"}
	fmt.Printf("Encoding: %+v\n", s)

	var buf bytes.Buffer
	mx := mux.StandardMux()
	mx.Encoder(&buf).Encode(s)

	fmt.Printf("Encoded buffer: %s\n", buf.String())

	var v MyStruct
	mx.Decoder(&buf).Decode(&v)

	fmt.Printf("Decoded: %+v\n", v)
}
Пример #7
0
func apiServer(port int) error {
	m := BuildAPIAddr(port)
	ln, err := manet.Listen(m)
	if err != nil {
		return err
	}

	fmt.Printf("API server listening on %v\n", m)

	// Go accept API requests.
	go func() {
		defer ln.Close()

		for {
			conn, err := ln.Accept()
			if err != nil {
				fmt.Println(err)
				return
			}

			// Go handle the new connection.
			go func(con manet.Conn) {
				defer con.Close()

				// Decode arguments.
				var args []string
				mx := mux.StandardMux()
				err = mx.Decoder(con).Decode(&args)
				if err != nil {
					fmt.Println(err)
					return
				}

				cmdName := args[0]
				args = args[1:]

				// Get the command object
				v, ok := (*commandsPtr)[cmdName]
				if !ok {
					fmt.Printf("Unknown command: %s\n", cmdName)
					return
				}
				cmd, ok := v.(*RemoteCommand)
				if !ok {
					fmt.Printf("%s is not a remote command\n", cmdName)
					return
				}

				// Multiplex connection
				remoteStdio, err := WithErrorCode(con)
				if err != nil {
					fmt.Println(err)
					return
				}

				// Run cmd. Provide remote connection as stdin/stdout.
				ec := cmd.Run(args, remoteStdio)
				remoteStdio.Close(ec)
			}(conn)
		}
	}()

	return nil
}
Пример #8
0
func codec() *mux.Multicodec {
	m := mux.StandardMux()
	m.Wrap = Flags.MCWrap
	return m
}
Пример #9
0
func codecWithPath(path string) mc.Multicodec {
	hdr := mc.Header([]byte(path))
	codec := mux.CodecWithHeader(hdr, mux.StandardMux().Codecs)
	return codec
}