<commit_before>/*
   prlpks - OpenPGP key server
   Copyright (C) 2012  <NAME>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package openpgp

import (
	"bytes"
	"encoding/hex"
	"fmt"
	. "github.com/pruthvirajsinh/symflux"
	"github.com/pruthvirajsinh/symflux/recon"
	"github.com/pruthvirajsinh/symflux/recon/leveldb"
	"io"
	"io/ioutil"
	"log"
	"net/http"

	"time"
	//	"github.com/pruthvirajsinh/PrcIdSigner"
	"github.com/pruthvirajsinh/prlpks/hkp"
)

type SksPeer struct {
	*recon.Peer
	Service        *hkp.Service
	RecoverKey     chan *RecoverKey
	KeyChanges     KeyChangeChan
	LocalDeleteKey chan *LocalDeleteKey
}

type RecoverKey struct {
	Keytext    []byte
	RecoverSet *ZSet
	Source     string
	response   hkp.ResponseChan
	//PRC Start
	verifiedDomains []string
	//PRC End
}

func NewSksPTree(reconSettings *recon.Settings) (recon.PrefixTree, error) {
	treeSettings := leveldb.NewSettings(reconSettings)
	return leveldb.New(treeSettings)
}

func NewSksPeer(s *hkp.Service) (*SksPeer, error) {
	reconSettings := recon.NewSettings(Config().Settings.TomlTree)
	ptree, err := NewSksPTree(reconSettings)
	if err != nil {
		return nil, err
	}
	//tmpAuth := recon.Authority{"<EMAIL>", "<EMAIL>", "no key"}
	peer := recon.NewPeer(reconSettings, ptree)
	sksPeer := &SksPeer{
		Peer:           peer,
		Service:        s,
		KeyChanges:     make(KeyChangeChan, reconSettings.SplitThreshold()),
		RecoverKey:     make(chan *RecoverKey),
		LocalDeleteKey: make(chan *LocalDeleteKey)}
	return sksPeer, nil
}

func (r *SksPeer) Start() {
	r.Peer.PrefixTree.Create()
	//PRC Start
	if UpdateOwnLocalState() != nil {
		fmt.Println("Error updating Own Local State")
		log.Println("Error updating Own Local State")
	}
	//PRC End
	go r.HandleRecovery()
	go r.HandleKeyUpdates()
	go r.Peer.Start()
}

func (r *SksPeer) HandleKeyUpdates() {
	for {

		select {
		case keyChange, ok := <-r.KeyChanges:<commit_msg>Added from latest hockeypuck,cleanly close the peer so that tree will be in consistent state.<commit_after>/*
   prlpks - OpenPGP key server
   Copyright (C) 2012  <NAME>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package openpgp

import (
	"bytes"
	"encoding/hex"
	"fmt"
	. "github.com/pruthvirajsinh/symflux"
	"github.com/pruthvirajsinh/symflux/recon"
	"github.com/pruthvirajsinh/symflux/recon/leveldb"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
	//	"github.com/pruthvirajsinh/PrcIdSigner"
	"github.com/pruthvirajsinh/prlpks/hkp"
)

type SksPeer struct {
	*recon.Peer
	Service        *hkp.Service
	RecoverKey     chan *RecoverKey
	KeyChanges     KeyChangeChan
	LocalDeleteKey chan *LocalDeleteKey
}

type RecoverKey struct {
	Keytext    []byte
	RecoverSet *ZSet
	Source     string
	response   hkp.ResponseChan
	//PRC Start
	verifiedDomains []string
	//PRC End
}

func NewSksPTree(reconSettings *recon.Settings) (recon.PrefixTree, error) {
	treeSettings := leveldb.NewSettings(reconSettings)
	return leveldb.New(treeSettings)
}

func NewSksPeer(s *hkp.Service) (*SksPeer, error) {
	reconSettings := recon.NewSettings(Config().Settings.TomlTree)
	ptree, err := NewSksPTree(reconSettings)
	if err != nil {
		return nil, err
	}
	//tmpAuth := recon.Authority{"dummy.<EMAIL>", "<EMAIL>", "no key"}
	peer := recon.NewPeer(reconSettings, ptree)
	sksPeer := &SksPeer{
		Peer:           peer,
		Service:        s,
		KeyChanges:     make(KeyChangeChan, reconSettings.SplitThreshold()),
		RecoverKey:     make(chan *RecoverKey),
		LocalDeleteKey: make(chan *LocalDeleteKey)}
	return sksPeer, nil
}

func (r *SksPeer) Start() {
	r.Peer.PrefixTree.Create()

	//Added from latest hockeypuck,cleanly close the peer so that tree will be in consistent state
	sigChan := make(chan os.Signal)
	signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
	go func() {
		select {
		case _ = <-sigChan:
			r.Peer.Stop()
		}
	}()

	//PRC Start
	if UpdateOwnLocalState() != nil {
		fmt.Println("Error updating Own Local State")
		log.Println("Error updating Own Local State")
	}
	//PRC End
	go r.HandleRecovery()
	go r.HandleKeyUpdates()
	go r.Peer.Start()
}

func (r *SksPeer) HandleKeyUpdates() {
	for {

		select {
		case keyChange, ok := <-r.KeyChanges:
{'fraction_non_alphanumeric': 0.10677033946520008, 'fraction_numerical': 0.0018964536317087046, 'mean_word_length': 3.838532110091743, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7173089', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1802, 'n_words': 515}