Skip to content

Commit 8548764

Browse files
committedApr 10, 2019
Change the parameters of the file function to array
1 parent e3bad77 commit 8548764

File tree

8 files changed

+54
-70
lines changed

8 files changed

+54
-70
lines changed
 

‎channel/channel.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ type Channel interface {
4848
// RemoveMember removes member contained in the channel
4949
RemoveMember(common.PKIidType) (*protos.ChainState, error)
5050

51-
// AddFile adds file to the channel
52-
AddFile(common.FileSyncInfo) (*protos.ChainState, error)
51+
// AddFile adds files to the channel
52+
AddFile([]*common.FileSyncInfo) (*protos.ChainState, error)
5353

5454
// RemoveFile removes file contained in the channel
55-
RemoveFile(string) (*protos.ChainState, error)
55+
RemoveFile([]string) (*protos.ChainState, error)
5656

5757
// Stop the channel's activity
5858
Stop()

‎channel/service.go

+29-33
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,7 @@ func (gc *gossipChannel) RemoveMember(member common.PKIidType) (*protos.ChainSta
252252
return gc.chainStateMsg, nil
253253
}
254254

255-
func (gc *gossipChannel) AddFile(file common.FileSyncInfo) (*protos.ChainState, error) {
256-
if gc.fileState.lookupFSyncProviderByFilename(file.Path) != nil {
257-
return nil, errors.Errorf("File %s has already exists ", file.Path)
258-
}
259-
255+
func (gc *gossipChannel) AddFile(files []*common.FileSyncInfo) (*protos.ChainState, error) {
260256
gc.Lock()
261257
defer gc.Unlock()
262258

@@ -265,19 +261,25 @@ func (gc *gossipChannel) AddFile(file common.FileSyncInfo) (*protos.ChainState,
265261
return nil, err
266262
}
267263

268-
mode, exists := protos.File_Mode_value[file.Mode]
269-
if !exists {
270-
return nil, errors.Errorf("Unknow file mode: %s", file.Mode)
271-
}
264+
for _, file := range files {
265+
if gc.fileState.lookupFSyncProviderByFilename(file.Path) != nil {
266+
logging.Warningf("File %s has already exists", file.Path)
267+
continue
268+
}
272269

273-
for _, f := range stateInfo.Properties.Files {
274-
if f.Path == file.Path {
275-
return gc.chainStateMsg, nil
270+
mode, exists := protos.File_Mode_value[file.Mode]
271+
if !exists {
272+
return nil, errors.Errorf("Unknow file mode: %s", file.Mode)
276273
}
277-
}
278274

279-
f := &protos.File{Path: file.Path, Mode: protos.File_Mode(mode)}
280-
stateInfo.Properties.Files = append(stateInfo.Properties.Files, f)
275+
f := &protos.File{Path: file.Path, Mode: protos.File_Mode(mode)}
276+
stateInfo.Properties.Files = append(stateInfo.Properties.Files, f)
277+
278+
err = gc.fileState.createProvider(file.Path, protos.File_Mode(mode), file.Metadata, true)
279+
if err != nil {
280+
return nil, errors.Wrap(err, "Failed creating file sync provider")
281+
}
282+
}
281283

282284
envp, err := msg.Sign(func(msg []byte) ([]byte, error) {
283285
return gc.idMapper.Sign(msg)
@@ -287,12 +289,10 @@ func (gc *gossipChannel) AddFile(file common.FileSyncInfo) (*protos.ChainState,
287289
}
288290
gc.chainStateMsg.Envelope = envp
289291
gc.chainStateMsg.SeqNum = uint64(time.Now().UnixNano())
290-
291-
gc.fileState.createProvider(file.Path, protos.File_Mode(mode), file.Metadata, true)
292292
return gc.chainStateMsg, nil
293293
}
294294

295-
func (gc *gossipChannel) RemoveFile(filename string) (*protos.ChainState, error) {
295+
func (gc *gossipChannel) RemoveFile(filenames []string) (*protos.ChainState, error) {
296296
gc.Lock()
297297
defer gc.Unlock()
298298

@@ -301,19 +301,18 @@ func (gc *gossipChannel) RemoveFile(filename string) (*protos.ChainState, error)
301301
return nil, err
302302
}
303303

304-
var found bool
305-
n := len(stateInfo.Properties.Files)
306-
for i := 0; i < n; i++ {
307-
f := stateInfo.Properties.Files[i]
308-
if f.Path == filename {
309-
stateInfo.Properties.Files = append(stateInfo.Properties.Files[:i], stateInfo.Properties.Files[i+1:]...)
310-
found = true
311-
break
312-
}
313-
}
304+
for _, filename := range filenames {
305+
gc.fileState.closeFSyncProvider(filename)
306+
gc.Unregister(fsync.GenerateMAC(gc.chainMac, filename))
314307

315-
if !found {
316-
return gc.chainStateMsg, nil
308+
n := len(stateInfo.Properties.Files)
309+
for i := 0; i < n; i++ {
310+
f := stateInfo.Properties.Files[i]
311+
if f.Path == filename {
312+
stateInfo.Properties.Files = append(stateInfo.Properties.Files[:i], stateInfo.Properties.Files[i+1:]...)
313+
break
314+
}
315+
}
317316
}
318317

319318
envp, err := msg.Sign(func(msg []byte) ([]byte, error) {
@@ -326,9 +325,6 @@ func (gc *gossipChannel) RemoveFile(filename string) (*protos.ChainState, error)
326325
gc.chainStateMsg.Envelope = envp
327326
gc.chainStateMsg.SeqNum = uint64(time.Now().UnixNano())
328327

329-
gc.fileState.closeFSyncProvider(filename)
330-
gc.Unregister(fsync.GenerateMAC(gc.chainMac, filename))
331-
332328
return gc.chainStateMsg, nil
333329
}
334330

‎gossip/batcher.go

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type batchedMessage struct {
7070
}
7171

7272
func (p *batchingEmitterImpl) Add(message interface{}) {
73+
if p.toDie() {
74+
return
75+
}
7376
if p.iterations == 0 {
7477
return
7578
}

‎gossip/file_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func TestFileSync(t *testing.T) {
3737
assert.NoError(t, err)
3838
_, err = gossipSvc1.AddMemberToChain(mac, gossipSvc2.SelfPKIid())
3939
assert.NoError(t, err)
40-
_, err = gossipSvc1.AddFileToChain(mac, common.FileSyncInfo{Path: "https-cert.pem", Mode: "Append"})
40+
_, err = gossipSvc1.AddFileToChain(mac, []*common.FileSyncInfo{&common.FileSyncInfo{Path: "https-cert.pem", Mode: "Append"}})
4141
assert.NoError(t, err)
42-
_, err = gossipSvc1.AddFileToChain(mac, common.FileSyncInfo{Path: "https-key.pem", Mode: "Append"})
42+
_, err = gossipSvc1.AddFileToChain(mac, []*common.FileSyncInfo{&common.FileSyncInfo{Path: "https-key.pem", Mode: "Append"}})
4343
assert.NoError(t, err)
4444

4545
time.Sleep(5 * time.Second)
@@ -95,7 +95,7 @@ func TestChainStateDynamicUpdate(t *testing.T) {
9595
assert.Len(t, state.Properties.Members, 2)
9696
assert.Len(t, state.Properties.Files, 3)
9797

98-
_, err = gossipSvc1.AddFileToChain(mac, common.FileSyncInfo{Path: "https-cert.pem", Mode: "Append"})
98+
_, err = gossipSvc1.AddFileToChain(mac, []*common.FileSyncInfo{&common.FileSyncInfo{Path: "https-cert.pem", Mode: "Append"}})
9999
assert.NoError(t, err)
100100

101101
time.Sleep(5 * time.Second)

‎gossip/gossip.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ type Gossip interface {
3939
RemoveMemberWithChain(chainMac common.ChainMac, member common.PKIidType) (*protos.ChainState, error)
4040

4141
// AddFileToChain adds file to channel
42-
AddFileToChain(chainMac common.ChainMac, file common.FileSyncInfo) (*protos.ChainState, error)
42+
AddFileToChain(chainMac common.ChainMac, files []*common.FileSyncInfo) (*protos.ChainState, error)
4343

4444
// RemoveFileWithChain removes file contained in the channel
45-
RemoveFileWithChain(chainMac common.ChainMac, filename string) (*protos.ChainState, error)
45+
RemoveFileWithChain(chainMac common.ChainMac, filenames []string) (*protos.ChainState, error)
4646

4747
// GetPKIidOfCert returns the PKI-ID of a certificate
4848
GetPKIidOfCert(nodeID string, cert *x509.Certificate) (common.PKIidType, error)

‎gossip/service.go

+5-17
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,22 @@ func (g *gossipService) RemoveMemberWithChain(chainMac common.ChainMac, member c
204204
return gc.RemoveMember(member)
205205
}
206206

207-
func (g *gossipService) AddFileToChain(chainMac common.ChainMac, file common.FileSyncInfo) (*protos.ChainState, error) {
207+
func (g *gossipService) AddFileToChain(chainMac common.ChainMac, files []*common.FileSyncInfo) (*protos.ChainState, error) {
208208
gc := g.chanState.getChannelByMAC(chainMac)
209209
if gc == nil {
210210
return nil, errors.Errorf("Channel %s not yet created", chainMac)
211211
}
212212

213-
return gc.AddFile(file)
213+
return gc.AddFile(files)
214214
}
215215

216-
func (g *gossipService) RemoveFileWithChain(chainMac common.ChainMac, filename string) (*protos.ChainState, error) {
216+
func (g *gossipService) RemoveFileWithChain(chainMac common.ChainMac, filenames []string) (*protos.ChainState, error) {
217217
gc := g.chanState.getChannelByMAC(chainMac)
218218
if gc == nil {
219219
return nil, errors.Errorf("Channel %s not yet created", chainMac)
220220
}
221221

222-
return gc.RemoveFile(filename)
222+
return gc.RemoveFile(filenames)
223223
}
224224

225225
func (g *gossipService) GetPKIidOfCert(nodeID string, cert *x509.Certificate) (common.PKIidType, error) {
@@ -327,8 +327,8 @@ func (g *gossipService) Stop() {
327327
logging.Info("Stopping gossip")
328328
defer logging.Info("Stopped gossip")
329329
g.chanState.stop()
330-
g.discAdapter.close()
331330
g.disc.Stop()
331+
g.discAdapter.close()
332332
g.toDieChan <- struct{}{}
333333
g.emitter.Stop()
334334
g.ChannelDeMultiplexer.Close()
@@ -579,8 +579,6 @@ func (g *gossipService) isInChannel(m protos.ReceivedMessage) bool {
579579
}
580580

581581
func (g *gossipService) forwardDiscoveryMsg(msg protos.ReceivedMessage) {
582-
g.discAdapter.RLock()
583-
defer g.discAdapter.RUnlock()
584582
if g.discAdapter.toDie() {
585583
return
586584
}
@@ -717,14 +715,10 @@ type discoveryAdapter struct {
717715
gossipFunc func(message *protos.SignedRKSyncMessage)
718716
forwardFunc func(message protos.ReceivedMessage)
719717
disclosurePolicy discovery.DisclosurePolicy
720-
sync.RWMutex
721718
}
722719

723720
func (da *discoveryAdapter) close() {
724721
atomic.StoreInt32(&da.stopping, int32(1))
725-
726-
da.Lock()
727-
defer da.Unlock()
728722
close(da.incChan)
729723
}
730724

@@ -733,8 +727,6 @@ func (da *discoveryAdapter) toDie() bool {
733727
}
734728

735729
func (da *discoveryAdapter) Gossip(msg *protos.SignedRKSyncMessage) {
736-
da.RLock()
737-
defer da.RUnlock()
738730
if da.toDie() {
739731
return
740732
}
@@ -743,8 +735,6 @@ func (da *discoveryAdapter) Gossip(msg *protos.SignedRKSyncMessage) {
743735
}
744736

745737
func (da *discoveryAdapter) Forward(msg protos.ReceivedMessage) {
746-
da.RLock()
747-
defer da.RUnlock()
748738
if da.toDie() {
749739
return
750740
}
@@ -753,8 +743,6 @@ func (da *discoveryAdapter) Forward(msg protos.ReceivedMessage) {
753743
}
754744

755745
func (da *discoveryAdapter) SendToPeer(peer *common.NetworkMember, msg *protos.SignedRKSyncMessage) {
756-
da.RLock()
757-
defer da.RUnlock()
758746
if da.toDie() {
759747
return
760748
}

‎server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestRKSyncServiceServe(t *testing.T) {
190190
chainInfo := srv2.gossip.SelfChainInfo("testchannel")
191191
assert.NotNil(t, chainInfo)
192192

193-
err = srv1.AddFileToChan("testchannel", "rfc2616.txt", "Append", []byte{})
193+
err = srv1.AddFileToChan("testchannel", []*common.FileSyncInfo{&common.FileSyncInfo{Path: "rfc2616.txt", Mode: "Append", Metadata: []byte{}}})
194194
assert.NoError(t, err)
195195

196196
time.Sleep(5 * time.Second)

‎service.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,16 @@ func (srv *Server) RemoveMemberWithChan(chainID string, nodeID string, cert *x50
218218
}
219219

220220
// AddFileToChan adds a file to the channel
221-
func (srv *Server) AddFileToChan(chainID string, filepath string, filemode string, metadata []byte) error {
221+
func (srv *Server) AddFileToChan(chainID string, files []*common.FileSyncInfo) error {
222222
if chainID == "" {
223223
return errors.New("Channel ID must be provided")
224224
}
225-
if filepath == "" {
226-
return errors.New("File path must be provided")
227-
}
228-
if filemode == "" {
229-
return errors.New("File mode must be provided")
225+
if len(files) == 0 {
226+
return errors.New("files can't be nil or empty")
230227
}
231228

232229
mac := channel.GenerateMAC(srv.gossip.SelfPKIid(), chainID)
233-
chainState, err := srv.gossip.AddFileToChain(mac, common.FileSyncInfo{Path: filepath, Mode: filemode, Metadata: metadata})
230+
chainState, err := srv.gossip.AddFileToChain(mac, files)
234231
if err != nil {
235232
return err
236233
}
@@ -239,16 +236,16 @@ func (srv *Server) AddFileToChan(chainID string, filepath string, filemode strin
239236
}
240237

241238
// RemoveFileWithChan removes file contained in the channel
242-
func (srv *Server) RemoveFileWithChan(chainID string, filename string) error {
239+
func (srv *Server) RemoveFileWithChan(chainID string, filenames []string) error {
243240
if chainID == "" {
244241
return errors.New("Channel ID must be provided")
245242
}
246-
if filename == "" {
247-
return errors.New("File name must be provided")
243+
if len(filenames) == 0 {
244+
return errors.New("files can't be nil or empty")
248245
}
249246

250247
mac := channel.GenerateMAC(srv.gossip.SelfPKIid(), chainID)
251-
chainState, err := srv.gossip.RemoveFileWithChain(mac, filename)
248+
chainState, err := srv.gossip.RemoveFileWithChain(mac, filenames)
252249
if err != nil {
253250
return err
254251
}

0 commit comments

Comments
 (0)