github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/service/services.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package service 4 5 import ( 6 "../protocol" 7 ) 8 9 // Services store all application service 10 type Services struct { 11 poolByID map[uint64]protocol.Service 12 poolByMediaType map[string]protocol.Service 13 poolByURIPath map[string]protocol.Service 14 } 15 16 // Init use to initialize 17 func (ss *Services) Init() { 18 ss.poolByID = make(map[uint64]protocol.Service, 512) 19 ss.poolByURIPath = make(map[string]protocol.Service, 512) 20 ss.poolByMediaType = make(map[string]protocol.Service, 512) 21 } 22 23 // RegisterService use to register application services. 24 // Due to minimize performance impact, This method isn't safe to use concurrently and 25 // must register all service before use GetService methods. 26 func (ss *Services) RegisterService(s protocol.Service) { 27 if s.ID() == 0 && s.URI() == "" { 28 // This condition must be true just in the dev phase. 29 panic("Service must have a valid URI or mediatype. It is rule to add more detail about service. Initialize inner s.MediaType.Init() first if use libgo/service package") 30 } 31 32 ss.registerServiceByMediaType(s) 33 ss.registerServiceByURI(s) 34 } 35 36 func (ss *Services) registerServiceByMediaType(s protocol.Service) { 37 var serviceID = s.ID() 38 var exitingServiceByID = ss.GetServiceByID(serviceID) 39 if exitingServiceByID != nil { 40 // This condition will just be true in the dev phase. 41 panic("ID associated for '" + s.MediaType() + "' Used before for other service and not legal to reuse same ID for other services\n" + 42 "Exiting service MediaType is: " + exitingServiceByID.MediaType()) 43 } else { 44 ss.poolByID[serviceID] = s 45 } 46 47 var serviceMediaType = s.MediaType() 48 var exitingServiceByMediaType = ss.GetServiceByMediaType(serviceMediaType) 49 if exitingServiceByMediaType != nil { 50 // This condition will just be true in the dev phase. 51 panic("This mediatype '" + serviceMediaType + "' register already before for other service and not legal to reuse same mediatype for other services\n") 52 } else { 53 ss.poolByMediaType[serviceMediaType] = s 54 } 55 } 56 57 func (ss *Services) registerServiceByURI(s protocol.Service) { 58 var serviceURI = s.URI() 59 if serviceURI != "" { 60 var exitingServiceByURI = ss.GetServiceByURI(serviceURI) 61 if exitingServiceByURI != nil { 62 // This condition will just be true in the dev phase. 63 panic("URI associated for '" + s.MediaType() + " service with `" + serviceURI + "` as URI, Used before for other service and not legal to reuse URI for other services\n" + 64 "Exiting service MediaType is: " + exitingServiceByURI.MediaType()) 65 } else { 66 ss.poolByMediaType[serviceURI] = s 67 } 68 } 69 } 70 71 // GetServiceByID use to get specific service handler by service ID 72 func (ss *Services) GetServiceByID(serviceID uint64) protocol.Service { 73 return ss.poolByID[serviceID] 74 } 75 76 // GetServiceByMediaType use to get specific service handler by service URI 77 func (ss *Services) GetServiceByMediaType(mt string) protocol.Service { 78 return ss.poolByMediaType[mt] 79 } 80 81 // GetServiceByURI use to get specific service handler by service URI path 82 func (ss *Services) GetServiceByURI(uri string) protocol.Service { 83 return ss.poolByURIPath[uri] 84 } 85 86 // DeleteService use to delete specific service in services list. 87 func (ss *Services) DeleteService(s protocol.Service) { 88 delete(ss.poolByID, s.ID()) 89 delete(ss.poolByMediaType, s.MediaType()) 90 delete(ss.poolByMediaType, s.URI()) 91 }