This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
/
service.go
129 lines (98 loc) · 4.22 KB
/
service.go
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
// Copyright 2019 The go-pttai Authors
// This file is part of the go-pttai library.
//
// The go-pttai library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-pttai library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-pttai library. If not, see <http://www.gnu.org/licenses/>.
package service
import (
"github.com/ailabstw/go-pttai/common/types"
"github.com/ailabstw/go-pttai/pttdb"
"github.com/ailabstw/go-pttai/rpc"
)
type Backend interface {
// master-oplog
GetMasterOplogList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MasterOplog, error)
GetPendingMasterOplogMasterList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MasterOplog, error)
GetPendingMasterOplogInternalList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MasterOplog, error)
GetMasterOplogMerkleNodeList(idBytes []byte, level MerkleTreeLevel, startKey []byte, limit int, listOrder pttdb.ListOrder) ([]*BackendMerkleNode, error)
// master
GetMasterListFromCache(idBytes []byte) ([]*Master, error)
GetMasterList(idBytes []byte, startIDBytes []byte, limit int, listOrder pttdb.ListOrder) ([]*Master, error)
// member-oplog
GetMemberOplogList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MemberOplog, error)
GetPendingMemberOplogMasterList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MemberOplog, error)
GetPendingMemberOplogInternalList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*MemberOplog, error)
GetMemberOplogMerkleNodeList(idBytes []byte, level MerkleTreeLevel, startKey []byte, limit int, listOrder pttdb.ListOrder) ([]*BackendMerkleNode, error)
// member
GetMemberList(idBytes []byte, startIDBytes []byte, limit int, listOrder pttdb.ListOrder) ([]*Master, error)
// op-key-oplog
GetOpKeyOplogList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*OpKeyOplog, error)
GetPendingOpKeyOplogMasterList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*OpKeyOplog, error)
GetPendingOpKeyOplogInternalList(idBytes []byte, logID []byte, limit int, listOrder pttdb.ListOrder) ([]*OpKeyOplog, error)
// op-key
ShowValidateKey() (*types.PttID, error)
ValidateValidateKey(keyBytes []byte) (bool, error)
GetOpKeyInfos([]*KeyInfo, error)
RevokeOpKey(keyIDBytes []byte, myKeyBytes []byte) (bool, error)
GetOpKeyInfosFromDB() ([]*KeyInfo, error)
// peers
CountPeers() (int, error)
GetPeers() ([]*BackendPeer, error)
}
type Service interface {
// APIs retrieves the list of RPC descriptors the service provides
APIs() []rpc.API
// Start is called after all services have been constructed and the networking
// layer was not initialized yet.
Prestart() error
// Start is called after all services have been constructed and the networking
// layer was also initialized to spawn any goroutines required by the service.
Start() error
// Stop terminates all goroutines belonging to the service, blocking until they
// are all terminated.
Stop() error
SPM() ServiceProtocolManager
Name() string
Ptt() Ptt
}
type MyService interface {
Service
}
/*
BaseService implements the base-type of Service
*/
type BaseService struct {
spm ServiceProtocolManager
ptt Ptt
}
func NewBaseService(ptt Ptt, spm ServiceProtocolManager) (*BaseService, error) {
return &BaseService{ptt: ptt, spm: spm}, nil
}
func (svc *BaseService) APIs() []rpc.API {
return nil
}
func (svc *BaseService) Prestart() error {
return svc.SPM().Prestart()
}
func (svc *BaseService) Start() error {
return svc.SPM().Start()
}
func (svc *BaseService) Stop() error {
return svc.SPM().Stop()
}
func (svc *BaseService) SPM() ServiceProtocolManager {
return svc.spm
}
func (svc *BaseService) Ptt() Ptt {
return svc.ptt
}