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