|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + ab "github.com/hyperledger/fabric/orderer/atomicbroadcast" |
| 23 | + "golang.org/x/net/context" |
| 24 | + "google.golang.org/grpc" |
| 25 | +) |
| 26 | + |
| 27 | +type deliverClient struct { |
| 28 | + client ab.AtomicBroadcast_DeliverClient |
| 29 | + windowSize uint64 |
| 30 | + unAcknowledged uint64 |
| 31 | +} |
| 32 | + |
| 33 | +func newDeliverClient(client ab.AtomicBroadcast_DeliverClient, windowSize uint64) *deliverClient { |
| 34 | + return &deliverClient{client: client, windowSize: windowSize} |
| 35 | +} |
| 36 | + |
| 37 | +func (r *deliverClient) seekOldest() error { |
| 38 | + return r.client.Send(&ab.DeliverUpdate{ |
| 39 | + Type: &ab.DeliverUpdate_Seek{ |
| 40 | + Seek: &ab.SeekInfo{ |
| 41 | + Start: ab.SeekInfo_OLDEST, |
| 42 | + WindowSize: r.windowSize, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func (r *deliverClient) seekNewest() error { |
| 49 | + return r.client.Send(&ab.DeliverUpdate{ |
| 50 | + Type: &ab.DeliverUpdate_Seek{ |
| 51 | + Seek: &ab.SeekInfo{ |
| 52 | + Start: ab.SeekInfo_NEWEST, |
| 53 | + WindowSize: r.windowSize, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func (r *deliverClient) seek(blockNumber uint64) error { |
| 60 | + return r.client.Send(&ab.DeliverUpdate{ |
| 61 | + Type: &ab.DeliverUpdate_Seek{ |
| 62 | + Seek: &ab.SeekInfo{ |
| 63 | + Start: ab.SeekInfo_SPECIFIED, |
| 64 | + SpecifiedNumber: blockNumber, |
| 65 | + WindowSize: r.windowSize, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func (r *deliverClient) readUntilClose() { |
| 72 | + for { |
| 73 | + msg, err := r.client.Recv() |
| 74 | + if err != nil { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + switch t := msg.Type.(type) { |
| 79 | + case *ab.DeliverResponse_Error: |
| 80 | + if t.Error == ab.Status_SUCCESS { |
| 81 | + fmt.Println("ERROR! Received success in error field") |
| 82 | + return |
| 83 | + } |
| 84 | + fmt.Println("Got error ", t) |
| 85 | + case *ab.DeliverResponse_Block: |
| 86 | + fmt.Println("Received block: ", t.Block) |
| 87 | + r.unAcknowledged++ |
| 88 | + if r.unAcknowledged >= r.windowSize/2 { |
| 89 | + fmt.Println("Sending acknowledgement") |
| 90 | + err = r.client.Send(&ab.DeliverUpdate{Type: &ab.DeliverUpdate_Acknowledgement{Acknowledgement: &ab.Acknowledgement{Number: t.Block.Number}}}) |
| 91 | + if err != nil { |
| 92 | + return |
| 93 | + } |
| 94 | + r.unAcknowledged = 0 |
| 95 | + } |
| 96 | + default: |
| 97 | + fmt.Println("Received unknock: ", t) |
| 98 | + return |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func main() { |
| 104 | + serverAddr := "127.0.0.1:5005" |
| 105 | + conn, err := grpc.Dial(serverAddr, grpc.WithInsecure()) |
| 106 | + if err != nil { |
| 107 | + fmt.Println("Error connecting:", err) |
| 108 | + return |
| 109 | + } |
| 110 | + client, err := ab.NewAtomicBroadcastClient(conn).Deliver(context.TODO()) |
| 111 | + if err != nil { |
| 112 | + fmt.Println("Error connecting:", err) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + s := newDeliverClient(client, 10) |
| 117 | + s.seekOldest() |
| 118 | + s.readUntilClose() |
| 119 | + |
| 120 | +} |
0 commit comments