github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/events/producer/producer.go (about) 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 producer 18 19 import ( 20 "fmt" 21 "io" 22 "time" 23 24 "github.com/hyperledger/fabric/common/flogging" 25 pb "github.com/hyperledger/fabric/protos/peer" 26 ) 27 28 var logger = flogging.MustGetLogger("eventhub_producer") 29 30 // EventsServer implementation of the Peer service 31 type EventsServer struct { 32 } 33 34 //singleton - if we want to create multiple servers, we need to subsume events.gEventConsumers into EventsServer 35 var globalEventsServer *EventsServer 36 37 // NewEventsServer returns a EventsServer 38 func NewEventsServer(bufferSize uint, timeout time.Duration) *EventsServer { 39 if globalEventsServer != nil { 40 panic("Cannot create multiple event hub servers") 41 } 42 globalEventsServer = new(EventsServer) 43 initializeEvents(bufferSize, timeout) 44 //initializeCCEventProcessor(bufferSize, timeout) 45 return globalEventsServer 46 } 47 48 // Chat implementation of the Chat bidi streaming RPC function 49 func (p *EventsServer) Chat(stream pb.Events_ChatServer) error { 50 handler, err := newEventHandler(stream) 51 if err != nil { 52 return fmt.Errorf("error creating handler during handleChat initiation: %s", err) 53 } 54 defer handler.Stop() 55 for { 56 in, err := stream.Recv() 57 if err == io.EOF { 58 logger.Debug("Received EOF, ending Chat") 59 return nil 60 } 61 if err != nil { 62 e := fmt.Errorf("error during Chat, stopping handler: %s", err) 63 logger.Error(e.Error()) 64 return e 65 } 66 err = handler.HandleMessage(in) 67 if err != nil { 68 logger.Errorf("Error handling message: %s", err) 69 return err 70 } 71 } 72 }