2019-06-13 16:09:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// #include "uplink_definitions.h"
|
|
|
|
import "C"
|
|
|
|
|
2019-07-30 12:40:05 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-11-12 16:45:40 +00:00
|
|
|
"storj.io/storj/internal/memory"
|
2019-07-30 12:40:05 +01:00
|
|
|
"storj.io/storj/lib/uplink"
|
2019-11-12 16:45:40 +00:00
|
|
|
"time"
|
2019-07-30 12:40:05 +01:00
|
|
|
)
|
2019-06-13 16:09:05 +01:00
|
|
|
|
2019-10-17 08:42:22 +01:00
|
|
|
var universe = newHandles()
|
|
|
|
|
|
|
|
//export internal_UniverseIsEmpty
|
|
|
|
// internal_UniverseIsEmpty returns true if nothing is stored in the global map.
|
|
|
|
func internal_UniverseIsEmpty() bool {
|
|
|
|
return universe.Empty()
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:09:05 +01:00
|
|
|
// Uplink is a scoped uplink.Uplink.
|
|
|
|
type Uplink struct {
|
|
|
|
scope
|
|
|
|
*uplink.Uplink
|
|
|
|
}
|
|
|
|
|
|
|
|
//export new_uplink
|
|
|
|
// new_uplink creates the uplink with the specified configuration and returns
|
|
|
|
// an error in cerr, when there is one.
|
|
|
|
//
|
|
|
|
// Caller must call close_uplink to close associated resources.
|
2019-10-07 13:03:58 +01:00
|
|
|
func new_uplink(cfg C.UplinkConfig, tempDir *C.char, cerr **C.char) C.UplinkRef {
|
|
|
|
scope := rootScope(C.GoString(tempDir))
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
libcfg := &uplink.Config{} // TODO: figure out a better name
|
2019-08-01 12:14:09 +01:00
|
|
|
// TODO: V3-2302, add a way to support logging
|
2019-07-30 12:40:05 +01:00
|
|
|
libcfg.Volatile.TLS.SkipPeerCAWhitelist = cfg.Volatile.tls.skip_peer_ca_whitelist == C.bool(true)
|
2019-11-12 16:45:40 +00:00
|
|
|
libcfg.Volatile.TLS.PeerCAWhitelistPath = C.GoString(cfg.Volatile.tls.peer_ca_whitelist_path)
|
|
|
|
libcfg.Volatile.PeerIDVersion = C.GoString(cfg.Volatile.peer_id_version)
|
|
|
|
libcfg.Volatile.MaxInlineSize = memory.Size(cfg.Volatile.max_inline_size)
|
|
|
|
libcfg.Volatile.MaxMemory = memory.Size(cfg.Volatile.max_memory)
|
|
|
|
libcfg.Volatile.DialTimeout = time.Duration(cfg.Volatile.dial_timeout)
|
2019-06-13 16:09:05 +01:00
|
|
|
|
|
|
|
lib, err := uplink.NewUplink(scope.ctx, libcfg)
|
|
|
|
if err != nil {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.UplinkRef{}
|
2019-06-13 16:09:05 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 12:23:12 +01:00
|
|
|
return C.UplinkRef{universe.Add(&Uplink{scope, lib})}
|
2019-06-13 16:09:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//export close_uplink
|
|
|
|
// close_uplink closes and frees the resources associated with uplink
|
2019-06-15 12:23:12 +01:00
|
|
|
func close_uplink(uplinkHandle C.UplinkRef, cerr **C.char) {
|
2019-06-13 16:09:05 +01:00
|
|
|
uplink, ok := universe.Get(uplinkHandle._handle).(*Uplink)
|
|
|
|
if !ok {
|
|
|
|
*cerr = C.CString("invalid uplink")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
universe.Del(uplinkHandle._handle)
|
|
|
|
defer uplink.cancel()
|
|
|
|
|
|
|
|
if err := uplink.Close(); err != nil {
|
2019-07-30 12:40:05 +01:00
|
|
|
*cerr = C.CString(fmt.Sprintf("%+v", err))
|
2019-06-13 16:09:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|