<commit_before>package main

import (
	"bytes"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"reflect"
	"strings"
	"time"
	"strconv"

	log "github.com/sirupsen/logrus"
	"github.com/ugorji/go/codec"
	"github.com/nttdots/go-dots/dots_common"
	"github.com/nttdots/go-dots/dots_common/messages"
	"github.com/nttdots/go-dots/dots_client/task"
	"github.com/nttdots/go-dots/libcoap"
	"github.com/shopspring/decimal"
	client_message "github.com/nttdots/go-dots/dots_client/messages"
)

type RequestInterface interface {
	LoadJson([]byte) error
	CreateRequest()
	Send()
}

const (
	Register     uint16 = 0
	Deregister   uint16 = 1
)

/*
 * Dots requests
 */
type Request struct {
	Message     interface{}
	RequestCode messages.Code
	pdu         *libcoap.Pdu
	coapType    libcoap.Type
	method      string
	requestName string
	queryParams []string

	env         *task.Env
	options     map[messages.Option]string
}

/*
 * Request constructor.
 */
func NewRequest(code messages.Code, coapType libcoap.Type, method string, requestName string, queryParams []string, env *task.Env, options map[messages.Option]string) *Request {
	return &Request{
		nil,
		code,
		nil,
		coapType,
		method,
		requestName,
		queryParams,
		env,
		options,
	}
}

/*
 * Load a Message to this Request
 */
func (r *Request) LoadMessage(message interface{}) {
	r.Message = message
}

/*
 * convert this Request into the Cbor format.
 */
func (r *Request) dumpCbor() []byte {
	var buf []byte
	e := codec.NewEncoderBytes(&buf, dots_common.NewCborHandle())

	err := e.Encode(r.Message)
	if err != nil {
		log.Errorf("Error decoding %s", err)
	}
	return buf
}

/*
 * convert this Requests into the JSON format.
 */
func (r *Request) dumpJson() []byte {
	payload, _ := json.Marshal(r.Message)
	return payload
}

/*
 * Load Message from JSON data.
 */
func (r *Request) LoadJson(jsonData []byte) error {
	m := reflect.New(r.RequestCode.Type()).Interface()

	err := json.Unmarshal(jsonData, &m)
	if err != nil {
		return fmt.Errorf("Can't Convert Json to Message Object: %v\n", err)

	}
	r.Message = m
	return nil
}

/*
 * return the Request paths.
 */
func (r *Request) pathString() {
	r.RequestCode.PathString()
}

/*
 * Create CoAP requests.
 */
func (r *Request) CreateRequest() {
	var code libcoap.Code
	var observe uint16

	switch strings.ToUpper(r.method) {
	case "GET":
		code = libcoap.RequestGet
	case "PUT":
		code = libcoap.RequestPut
	case "POST":
		code = libcoap.RequestPost
	case "DELETE":
		code = libcoap.RequestDelete
	default:
		log.WithField("method", r.method).Error("invalid request method.")
	}

	r.pdu = &libcoap.Pdu{}
	r.pdu.Type = r.coapType
	r.pdu.Code = code
	r.pdu.MessageID = r.env.CoapSession().NewMessageID()
	r.pdu.Token = dots_common.RandStringBytes(8)
	r.pdu.Options = make([]libcoap.Option, 0)
	observeStr := r.options[messages.OBSERVE]
	if observeStr != "" {
		observeValue, error := strconv.ParseUint(observeStr, 10, 16)
		if error != nil {
			log.Errorf("Observe is not uint type.")
			return
		}
		observe = uint16(observeValue)

		if observe == Register || observe == Deregister {
			r.pdu.Options = append(r.pdu.Options, libcoap.OptionObserve.Uint16(observe))
			queryString := task.QueryParamsToString(r.queryParams)
			if observe == Register {
				r.env.AddToken(r.pdu.Token, queryString)
			} else {
				r.pdu.Token = r.env.GetToken(queryString)
				r.env.RemoveToken(queryString)
			}
		}
	}
	if val, ok := r.options[messages.IFMATCH]; ok {
		r.pdu.Options = append(r.pdu.Options, libcoap.OptionIfMatch.String(val))
	}

	if r.Message != nil {
		r.pdu.Data = r.dumpCbor()
		r.pdu.Options = append(r.pdu.Options, libcoap.OptionContentFormat.Uint16(60))
		log.Debugf("hex dump cbor request:\n%s", hex.Dump(r.pdu.Data))
	}
	tmpPathWithQuery := r.RequestCode.PathString() + "/" + strings.Join(r.queryParams, "/")
	r.pdu.SetPathString(tmpPathWithQuery)
	log.Debugf("SetPathString=%+v", tmpPathWithQuery)
	log.Debugf("r.pdu=%+v", r.pdu)
}

func handleTimeout(task *task.MessageTask) {
	log.Info("<<< handleTimeout >>>")
}


/*
 * Send the request to the server.
*/<commit_msg>Handle the same observe requests from a dots client #33<commit_after>package main

import (
	"bytes"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"reflect"
	"strings"
	"time"
	"strconv"

	log "github.com/sirupsen/logrus"
	"github.com/ugorji/go/codec"
	"github.com/nttdots/go-dots/dots_common"
	"github.com/nttdots/go-dots/dots_common/messages"
	"github.com/nttdots/go-dots/dots_client/task"
	"github.com/nttdots/go-dots/libcoap"
	"github.com/shopspring/decimal"
	client_message "github.com/nttdots/go-dots/dots_client/messages"
)

type RequestInterface interface {
	LoadJson([]byte) error
	CreateRequest()
	Send()
}

const (
	Register     uint16 = 0
	Deregister   uint16 = 1
)

/*
 * Dots requests
 */
type Request struct {
	Message     interface{}
	RequestCode messages.Code
	pdu         *libcoap.Pdu
	coapType    libcoap.Type
	method      string
	requestName string
	queryParams []string

	env         *task.Env
	options     map[messages.Option]string
}

/*
 * Request constructor.
 */
func NewRequest(code messages.Code, coapType libcoap.Type, method string, requestName string, queryParams []string, env *task.Env, options map[messages.Option]string) *Request {
	return &Request{
		nil,
		code,
		nil,
		coapType,
		method,
		requestName,
		queryParams,
		env,
		options,
	}
}

/*
 * Load a Message to this Request
 */
func (r *Request) LoadMessage(message interface{}) {
	r.Message = message
}

/*
 * convert this Request into the Cbor format.
 */
func (r *Request) dumpCbor() []byte {
	var buf []byte
	e := codec.NewEncoderBytes(&buf, dots_common.NewCborHandle())

	err := e.Encode(r.Message)
	if err != nil {
		log.Errorf("Error decoding %s", err)
	}
	return buf
}

/*
 * convert this Requests into the JSON format.
 */
func (r *Request) dumpJson() []byte {
	payload, _ := json.Marshal(r.Message)
	return payload
}

/*
 * Load Message from JSON data.
 */
func (r *Request) LoadJson(jsonData []byte) error {
	m := reflect.New(r.RequestCode.Type()).Interface()

	err := json.Unmarshal(jsonData, &m)
	if err != nil {
		return fmt.Errorf("Can't Convert Json to Message Object: %v\n", err)

	}
	r.Message = m
	return nil
}

/*
 * return the Request paths.
 */
func (r *Request) pathString() {
	r.RequestCode.PathString()
}

/*
 * Create CoAP requests.
 */
func (r *Request) CreateRequest() {
	var code libcoap.Code
	var observe uint16

	switch strings.ToUpper(r.method) {
	case "GET":
		code = libcoap.RequestGet
	case "PUT":
		code = libcoap.RequestPut
	case "POST":
		code = libcoap.RequestPost
	case "DELETE":
		code = libcoap.RequestDelete
	default:
		log.WithField("method", r.method).Error("invalid request method.")
	}

	r.pdu = &libcoap.Pdu{}
	r.pdu.Type = r.coapType
	r.pdu.Code = code
	r.pdu.MessageID = r.env.CoapSession().NewMessageID()
	r.pdu.Token = dots_common.RandStringBytes(8)
	r.pdu.Options = make([]libcoap.Option, 0)
	observeStr := r.options[messages.OBSERVE]
	if observeStr != "" {
		observeValue, error := strconv.ParseUint(observeStr, 10, 16)
		if error != nil {
			log.Errorf("Observe is not uint type.")
			return
		}
		observe = uint16(observeValue)

		if observe == Register || observe == Deregister {
			r.pdu.Options = append(r.pdu.Options, libcoap.OptionObserve.Uint16(observe))
			queryString := task.QueryParamsToString(r.queryParams)
			token := r.env.GetToken(queryString)
			if observe == Register {
				if token != nil {
					r.pdu.Token = token
				} else {
					r.env.AddToken(r.pdu.Token, queryString)
				}
			} else {
				if token != nil {
					r.pdu.Token = token
				}
				r.env.RemoveToken(queryString)
			}
		}
	}
	if val, ok := r.options[messages.IFMATCH]; ok {
		r.pdu.Options = append(r.pdu.Options, libcoap.OptionIfMatch.String(val))
	}

	if r.Message != nil {
		r.pdu.Data = r.dumpCbor()
		r.pdu.Options = append(r.pdu.Options, libcoap.OptionContentFormat.Uint16(60))
		log.Debugf("hex dump cbor request:\n%s", hex.Dump(r.pdu.Data))
	}
	tmpPathWithQuery := r.RequestCode.PathString() + "/" + strings.Join(r.queryParams, "/")
	r.pdu.SetPathString(tmpPathWithQuery)
	log.Debugf("SetPathString=%+v", tmpPathWithQuery)
	log.Debugf("r.pdu=%+v", r.pdu)
}

func handleTimeout(task *task.MessageTask) {
	log.Info("<<< handleTimeout >>>")
}


/*
 * Send the request to the server.
*/
{'fraction_non_alphanumeric': 0.14620024125452352, 'fraction_numerical': 0.005548854041013269, 'mean_word_length': 3.702779353374929, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '4402556', 'n_tokens_mistral': 3246, 'n_tokens_neox': 3056, 'n_words': 648}