aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/cmd/gnunet-service-revocation-go/main.go
blob: e21732cad82d92a6cbda02a2a2c272e737a98729 (plain) (blame)
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
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019, 2020 Bernd Fix  >Y<
//
// gnunet-go 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.
//
// gnunet-go 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/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

package main

import (
	"context"
	"flag"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"

	"gnunet/config"
	"gnunet/rpc"
	"gnunet/service"
	"gnunet/service/revocation"

	"github.com/bfix/gospel/logger"
)

func main() {
	defer func() {
		logger.Println(logger.INFO, "[revocation] Bye.")
		// flush last messages
		logger.Flush()
	}()
	logger.Println(logger.INFO, "[revocation] Starting service...")

	var (
		cfgFile  string
		socket   string
		param    string
		err      error
		logLevel int
		rpcEndp  string
	)
	// handle command line arguments
	flag.StringVar(&cfgFile, "c", "gnunet-config.json", "GNUnet configuration file")
	flag.StringVar(&socket, "s", "", "GNS service socket")
	flag.StringVar(&param, "p", "", "socket parameters (<key>=<value>,...)")
	flag.IntVar(&logLevel, "L", logger.INFO, "REVOCATION log level (default: INFO)")
	flag.StringVar(&rpcEndp, "R", "", "JSON-RPC endpoint (default: none)")
	flag.Parse()

	// read configuration file and set missing arguments.
	if err = config.ParseConfig(cfgFile); err != nil {
		logger.Printf(logger.ERROR, "[revocation] Invalid configuration file: %s\n", err.Error())
		return
	}

	// apply configuration
	logger.SetLogLevel(logLevel)
	if len(socket) == 0 {
		socket = config.Cfg.GNS.Service.Socket
	}
	params := make(map[string]string)
	if len(param) == 0 {
		for _, p := range strings.Split(param, ",") {
			kv := strings.SplitN(p, "=", 2)
			params[kv[0]] = kv[1]
		}
	} else {
		params = config.Cfg.GNS.Service.Params
	}

	// start a new REVOCATION service
	ctx, cancel := context.WithCancel(context.Background())
	rvc := revocation.NewService()
	srv := service.NewSocketHandler("revocation", rvc)
	if err = srv.Start(ctx, socket, params); err != nil {
		logger.Printf(logger.ERROR, "[revocation] Error: '%s'\n", err.Error())
		return
	}

	// start JSON-RPC server on request
	if len(rpcEndp) > 0 {
		parts := strings.Split(rpcEndp, ":")
		if parts[0] != "tcp" {
			logger.Println(logger.ERROR, "[revocation] RPC must have a TCP/IP endpoint")
			return
		}
		config.Cfg.RPC.Endpoint = parts[1]
		if err = rpc.Start(ctx); err != nil {
			logger.Printf(logger.ERROR, "[revocation] RPC failed to start: %s", err.Error())
			return
		}
		rpc.Register(rvc)
	}

	// handle OS signals
	sigCh := make(chan os.Signal, 5)
	signal.Notify(sigCh)

	// heart beat
	tick := time.NewTicker(5 * time.Minute)

loop:
	for {
		select {
		// handle OS signals
		case sig := <-sigCh:
			switch sig {
			case syscall.SIGKILL, syscall.SIGINT, syscall.SIGTERM:
				logger.Printf(logger.INFO, "[revocation] Terminating service (on signal '%s')\n", sig)
				break loop
			case syscall.SIGHUP:
				logger.Println(logger.INFO, "[revocation] SIGHUP")
			case syscall.SIGURG:
				// TODO: https://github.com/golang/go/issues/37942
			default:
				logger.Println(logger.INFO, "[revocation] Unhandled signal: "+sig.String())
			}
		// handle heart beat
		case now := <-tick.C:
			logger.Println(logger.INFO, "[revocation] Heart beat at "+now.String())
		}
	}

	// terminating service
	cancel()
	srv.Stop()
}