summaryrefslogtreecommitdiff
path: root/test/commandandcontrol/cc_agent/component/pingPongComponent.go
blob: 70e3980b6110fe2a66e5cf4e287c7b5cbeccd8f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand 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, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand 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 go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package component

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"time"

	"github.com/algorand/go-algorand/libgoal"
	"github.com/algorand/go-algorand/shared/pingpong"
)

// PingPongComponentInstance extends component instance
// supports management of ping pong instances
type PingPongComponentInstance struct {
	ci         Instance
	ctx        context.Context
	cancelFunc context.CancelFunc
}

func getPingPongConfig(command Command) (config pingpong.PpConfig, err error) {
	err = json.Unmarshal([]byte(command.options), &config)
	return
}

// Init the PingPong instance
func (componentInstance *PingPongComponentInstance) Init() (err error) {
	return
}

// Process the ping pong instance
func (componentInstance *PingPongComponentInstance) Process(command Command) (err error) {

	switch command.command {
	case "start":
		// terminate previous instance
		err = componentInstance.Terminate()
		if err != nil {
			log.Warnf("terminating component instance resulted in error: %v", err)
		}

		//  unmarshal config sent by client
		var pingPongConfig pingpong.PpConfig
		pingPongConfig, err = getPingPongConfig(command)
		if err != nil {
			err = fmt.Errorf("error demarshalling ping pong options %s, err: %v", command.options, err)
			log.Errorf("%v", err)
		} else {
			log.Infof("starting Ping Pong with configuration %+v", pingPongConfig)
			err = componentInstance.startPingPong(&pingPongConfig)
			if err != nil {
				err = fmt.Errorf("starting ping pong instance resulted in error: %v", err)
				log.Errorf("%v", err)
			} else {
				log.Infof("ping pong process started")
			}
		}
		break
	case "stop":
		log.Infof("terminating Ping Pong")
		err = componentInstance.Terminate()
		if err != nil {
			err = fmt.Errorf("terminating ping pong instance resulted in error: %v", err)
			log.Errorf("%v", err)
		} else {
			log.Infof("ping pong process terminated")
		}
		break
	default:
		log.Warnf("unsupported pingpong action '%s'", command.command)
	}
	return
}

// Terminate the ping pong instance
func (componentInstance *PingPongComponentInstance) Terminate() (err error) {
	if componentInstance.cancelFunc != nil {
		componentInstance.cancelFunc()
		componentInstance.cancelFunc = nil
		componentInstance.ctx = nil
	}
	return
}

func (componentInstance *PingPongComponentInstance) startPingPong(cfg *pingpong.PpConfig) (err error) {
	// Make a cache dir for wallet handle tokens
	cacheDir, err := os.MkdirTemp(GetHostAgent().TempDir, PINGPONG)
	if err != nil {
		log.Errorf("Cannot make temp dir: %v\n", err)
		return
	}

	ac, err := libgoal.MakeClientWithBinDir(GetHostAgent().BinDir, componentInstance.ci.dataDir, cacheDir, libgoal.FullClient)
	if err != nil {
		log.Errorf("failed to create lib goal client %v", err)
		return
	}

	// Prepare configuration
	if cfg == nil {
		cfg = &pingpong.DefaultConfig
	}

	log.Infof("Preparing to initialize PingPong with config: %+v\n", cfg)

	pps := pingpong.NewPingpong(*cfg)

	// Initialize accounts if necessary, this may take several attempts while previous transactions to settle
	for i := 0; i < 10; i++ {
		err = pps.PrepareAccounts(&ac)
		if err == nil {
			break
		} else {
			log.Warnf("problem[%d] preparing accounts for transfers: %v\n, retrying", i, err)
			time.Sleep(time.Second * 2)
		}
	}
	if err != nil {
		log.Errorf("Error preparing accounts for transfers: %v\n", err)
		return
	}

	log.Infof("Preparing to run PingPong with config: %+v\n", cfg)

	// prepare cancelable context
	componentInstance.ctx, componentInstance.cancelFunc = context.WithCancel(context.Background())

	// Kick off the real processing
	go pps.RunPingPong(componentInstance.ctx, &ac)

	return
}