1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 01:12:24 +08:00

Merge pull request #8 from toqueteos/master

Fix #1 build errors + gofmt
This commit is contained in:
Juan Batiz-Benet
2014-07-24 05:36:25 -07:00
3 changed files with 93 additions and 97 deletions

View File

@ -1,17 +1,18 @@
package bitswap package bitswap
import ( import (
peer "github.com/jbenet/go-ipfs/peer" "github.com/jbenet/go-ipfs/blocks"
"github.com/jbenet/go-multihash"
) )
// aliases // aliases
type Ledger struct { type Ledger struct {
// todo // todo
} }
type BitSwap struct { type BitSwap struct {
Ledgers map[peer.ID]*Ledger Ledgers map[string]*Ledger
HaveList map[multihash.Multihash]*block.Block HaveList map[string]*blocks.Block
WantList []*multihash.Multihash WantList []*multihash.Multihash
} }

View File

@ -1,7 +1,7 @@
package netmux package netmux
import ( import (
"net" "net"
) )
// An interface is the module connecting netmux // An interface is the module connecting netmux
@ -9,88 +9,88 @@ import (
// It keeps the relevant connections open. // It keeps the relevant connections open.
type Interface struct { type Interface struct {
// Interface network (e.g. udp4, tcp6) // Interface network (e.g. udp4, tcp6)
Network string Network string
// Own network address // Own network address
Address string Address string
ResolvedAddress string ResolvedAddress *net.UDPAddr
// Connection // Connection
conn *net.Conn conn net.Conn
// next packets + close control channels // next packets + close control channels
Input chan *Packet Input chan *Packet
Output chan *Packet Output chan *Packet
Closed chan bool Closed chan bool
Errors chan error Errors chan error
} }
func NewUDPInterface(net, addr string) (*Interface, error) { func NewUDPInterface(network, addr string) (*Interface, error) {
raddr, err := net.ResolveUDPAddr(net, addr) raddr, err := net.ResolveUDPAddr(network, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
conn, err := net.ListenUDP(net, addr) conn, err := net.ListenUDP(network, raddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
i := &Interface{ i := &Interface{
Network: net, Network: network,
Address: addr, Address: addr,
ResolvedAddress: raddr, ResolvedAddress: raddr,
conn: conn, conn: conn,
} }
go i.processInput() go i.processUDPInput()
go i.processOutput() go i.processOutput()
return i, nil return i, nil
} }
func (i *Interface) processOutput() { func (i *Interface) processOutput() {
for { for {
select { select {
case <-i.Closed: case <-i.Closed:
break; break
case buffer := <-i.Output: case buffer := <-i.Output:
i.conn.Write([]byte(buffer)) i.conn.Write([]byte(buffer.Data))
} }
} }
} }
func (i *Interface) processUDPInput() { func (i *Interface) processUDPInput() {
for { for {
select { select {
case <- i.Closed: case <-i.Closed:
break; break
} }
} }
} }
func (i *Interface) Read(buffer []byte) bool { func (i *Interface) Read(buffer []byte) bool {
n, err := i.Conn.Read(buffer) _, err := i.conn.Read(buffer)
if err != nil { if err != nil {
i.Errors <- err i.Errors <- err
i.Close() i.Close()
return false return false
} }
return true return true
} }
func (i *Interface) Close() { func (i *Interface) Close() {
// closing net connection // closing net connection
err := i.conn.Close() err := i.conn.Close()
if err != nil { if err != nil {
i.Errors <- err i.Errors <- err
} }
// closing channels // closing channels
close(i.Input) close(i.Input)
close(i.Output) close(i.Output)
close(i.Closed) close(i.Closed)
close(i.Errors) close(i.Errors)
} }

View File

@ -1,51 +1,46 @@
package netmux package netmux
import (
"net"
)
// The netmux module provides a "network multiplexer". // The netmux module provides a "network multiplexer".
// The core idea is to have the client be able to connect to // The core idea is to have the client be able to connect to
// many different networks (potentially over different transports) // many different networks (potentially over different transports)
// and multiplex everything over one interface. // and multiplex everything over one interface.
type Netmux struct { type Netmux struct {
// the list of NetMux interfaces // the list of NetMux interfaces
Interfaces []*Interface Interfaces []*Interface
// The channels to send/recv from // The channels to send/recv from
Incoming <-chan *Packet Incoming <-chan *Packet
Outgoing chan<- *Packet Outgoing chan<- *Packet
// internally managed other side of channels // internally managed other side of channels
incomingSrc chan<- *Packet incomingSrc chan<- *Packet
outgoingSrc <-chan *Packet outgoingSrc <-chan *Packet
} }
// Warning: will probably change to adopt multiaddr format // Warning: will probably change to adopt multiaddr format
type Packet { type Packet struct {
// the network addresses to send to // the network addresses to send to
// e.g. tcp4://127.0.0.1:12345 // e.g. tcp4://127.0.0.1:12345
NetAddrTo string NetAddrTo string
// the network addresses to recv from // the network addresses to recv from
// e.g. tcp4://127.0.0.1:12345 // e.g. tcp4://127.0.0.1:12345
// may be left blank to select one automatically. // may be left blank to select one automatically.
NetAddrFrom string NetAddrFrom string
// the data to send. // the data to send.
Data []byte Data []byte
} }
func NewNetmux() *Netmux { func NewNetmux() *Netmux {
n := &Netmux{} n := &Netmux{}
// setup channels // setup channels
och := make(chan *Packet) och := make(chan *Packet)
ich := make(chan *Packet) ich := make(chan *Packet)
n.Incoming, n.incomingSrc := ich, ich n.Incoming, n.incomingSrc = ich, ich
n.Outgoing, n.outgoingSrc := och, och n.Outgoing, n.outgoingSrc = och, och
return n return n
} }