<commit_before>// Copyright (c) 2015 Monetas.
// Copyright 2016 <NAME>.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package rpc

import (
	"errors"
	"fmt"
	"sync"
	"time"

	pb "github.com/DanielKrawisz/bmd/rpcproto"
	"github.com/DanielKrawisz/bmutil"
	"github.com/DanielKrawisz/bmutil/identity"
	"github.com/DanielKrawisz/bmutil/pow"
	"github.com/btcsuite/btcd/btcec"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/credentials"
)

var (
	// ErrIdentityNotFound is returned by GetIdentity.
	ErrIdentityNotFound = errors.New("identity not found")
)

// ClientConfig are configuration options for the RPC client to bmd.
type ClientConfig struct {
	// DisableTLS specifies whether TLS should be disabled for a connection to
	// bmd.
	DisableTLS bool

	// CAFile is  the file containing root certificates to authenticate a TLS
	// connection with bmd.
	CAFile string

	// ConnectTo is the hostname/IP and port of bmd RPC server to connect to.
	ConnectTo string

	// Username is the username to use for authentication with bmd.
	Username string

	// Password is the password to use for authentication with bmd.
	Password string

	// The timeout duration.
	Timeout time.Duration
}

// Client encapsulates a connection to bmd and provides helper methods for
// retrieving relevant data.
type Client struct {
	bmd           pb.BmdClient
	conn          *grpc.ClientConn
	msgFunc       func(counter uint64, msg []byte)
	broadcastFunc func(counter uint64, msg []byte)
	getpubkeyFunc func(counter uint64, msg []byte)
	quit          chan struct{}
	wg            sync.WaitGroup
	started       bool
	shutdown      bool
	quitMtx       sync.Mutex
}

// NewClient creates a new RPC connection to bmd.
func NewClient(cfg *ClientConfig, msg, broadcast, getpubkey func(counter uint64, msg []byte)) (*Client, error) {

	opts := []grpc.DialOption{
		grpc.WithPerRPCCredentials(
			pb.NewBasicAuthCredentials(cfg.Username, cfg.Password)),
		grpc.WithTimeout(cfg.Timeout)}

	if !cfg.DisableTLS {
		creds, err := credentials.NewClientTLSFromFile(cfg.CAFile, "")
		if err != nil {
			return nil, fmt.Errorf("Failed to create TLS credentials %v", err)
		}
		opts = append(opts, grpc.WithTransportCredentials(creds))<commit_msg>Fix grpc client initialization so that the insecure option is used explicitly when --disableclienttls is used.<commit_after>// Copyright (c) 2015 Monetas.
// Copyright 2016 <NAME>.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package rpc

import (
	"errors"
	"fmt"
	"sync"
	"time"

	pb "github.com/DanielKrawisz/bmd/rpcproto"
	"github.com/DanielKrawisz/bmutil"
	"github.com/DanielKrawisz/bmutil/identity"
	"github.com/DanielKrawisz/bmutil/pow"
	"github.com/btcsuite/btcd/btcec"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/credentials"
)

var (
	// ErrIdentityNotFound is returned by GetIdentity.
	ErrIdentityNotFound = errors.New("identity not found")
)

// ClientConfig are configuration options for the RPC client to bmd.
type ClientConfig struct {
	// DisableTLS specifies whether TLS should be disabled for a connection to
	// bmd.
	DisableTLS bool

	// CAFile is  the file containing root certificates to authenticate a TLS
	// connection with bmd.
	CAFile string

	// ConnectTo is the hostname/IP and port of bmd RPC server to connect to.
	ConnectTo string

	// Username is the username to use for authentication with bmd.
	Username string

	// Password is the password to use for authentication with bmd.
	Password string

	// The timeout duration.
	Timeout time.Duration
}

// Client encapsulates a connection to bmd and provides helper methods for
// retrieving relevant data.
type Client struct {
	bmd           pb.BmdClient
	conn          *grpc.ClientConn
	msgFunc       func(counter uint64, msg []byte)
	broadcastFunc func(counter uint64, msg []byte)
	getpubkeyFunc func(counter uint64, msg []byte)
	quit          chan struct{}
	wg            sync.WaitGroup
	started       bool
	shutdown      bool
	quitMtx       sync.Mutex
}

// NewClient creates a new RPC connection to bmd.
func NewClient(cfg *ClientConfig, msg, broadcast, getpubkey func(counter uint64, msg []byte)) (*Client, error) {

	opts := []grpc.DialOption{
		grpc.WithPerRPCCredentials(
			pb.NewBasicAuthCredentials(cfg.Username, cfg.Password)),
		grpc.WithTimeout(cfg.Timeout)}

	if cfg.DisableTLS {
		opts = append(opts, grpc.WithInsecure())
	} else {
		creds, err := credentials.NewClientTLSFromFile(cfg.CAFile, "")
		if err != nil {
			return nil, fmt.Errorf("Failed to create TLS credentials %v", err)
		}
		opts = append(opts, grpc.WithTransportCredentials(creds))
{'fraction_non_alphanumeric': 0.09467084639498433, 'fraction_numerical': 0.006687565308254964, 'mean_word_length': 3.6196911196911197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1012427', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1498, 'n_words': 486}