forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_client.go
97 lines (77 loc) · 2.24 KB
/
api_client.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
package weshnet
import (
"context"
"io"
"sync"
"berty.tech/weshnet/v2/pkg/errcode"
"berty.tech/weshnet/v2/pkg/protocoltypes"
"berty.tech/weshnet/v2/pkg/tyber"
)
func (s *service) ServiceExportData(_ *protocoltypes.ServiceExportData_Request, server protocoltypes.ProtocolService_ServiceExportDataServer) (err error) {
ctx, _, endSection := tyber.Section(server.Context(), s.logger, "Exporting protocol instance data")
defer func() { endSection(err, "") }()
r, w := io.Pipe()
var exportErr error
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer func() { _ = r.Close() }()
defer wg.Done()
for {
contents := make([]byte, 4096)
l, err := r.Read(contents)
if err == io.EOF {
break
} else if err != nil {
exportErr = errcode.ErrCode_ErrStreamRead.Wrap(err)
break
}
if err := server.Send(&protocoltypes.ServiceExportData_Reply{ExportedData: contents[:l]}); err != nil {
exportErr = errcode.ErrCode_ErrStreamWrite.Wrap(err)
break
}
}
}()
if err := s.export(ctx, w); err != nil {
return errcode.ErrCode_ErrInternal.Wrap(err)
}
_ = w.Close()
wg.Wait()
if exportErr != nil {
return exportErr
}
return nil
}
func (s *service) ServiceGetConfiguration(ctx context.Context, _ *protocoltypes.ServiceGetConfiguration_Request) (*protocoltypes.ServiceGetConfiguration_Reply, error) {
key, err := s.ipfsCoreAPI.Key().Self(ctx)
if err != nil {
return nil, errcode.ErrCode_TODO.Wrap(err)
}
maddrs, err := s.ipfsCoreAPI.Swarm().ListenAddrs(ctx)
if err != nil {
return nil, errcode.ErrCode_TODO.Wrap(err)
}
listeners := make([]string, len(maddrs))
for i, addr := range maddrs {
listeners[i] = addr.String()
}
accountGroup := s.getAccountGroup()
if accountGroup == nil {
return nil, errcode.ErrCode_ErrGroupMissing
}
member, err := accountGroup.MemberPubKey().Raw()
if err != nil {
return nil, errcode.ErrCode_ErrSerialization.Wrap(err)
}
device, err := accountGroup.DevicePubKey().Raw()
if err != nil {
return nil, errcode.ErrCode_ErrSerialization.Wrap(err)
}
return &protocoltypes.ServiceGetConfiguration_Reply{
AccountPk: member,
DevicePk: device,
AccountGroupPk: accountGroup.Group().PublicKey,
PeerId: key.ID().String(),
Listeners: listeners,
}, nil
}