github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/service/service.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package service defines the interface that fleetspeak client side services must implement, 16 // along with some related types. 17 package service 18 19 import ( 20 "context" 21 "io" 22 "time" 23 24 "github.com/google/fleetspeak/fleetspeak/src/client/stats" 25 "github.com/google/fleetspeak/fleetspeak/src/common" 26 27 fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak" 28 ) 29 30 // AckMessage is a message that can be acknowledged. If no acknowledgement is 31 // required, Ack may be nil. 32 type AckMessage struct { 33 M *fspb.Message 34 Ack func() 35 } 36 37 // A Service represents a configured client component which can accept and send 38 // messages via Fleetspeak. 39 type Service interface { 40 // Start performs any necessary setup for the Service. 41 // 42 // If it returns nil Fleetspeak will assume that the service is ready to 43 // process messages. Otherwise Fleetspeak will consider the service 44 // unavailable. 45 Start(Context) error 46 47 // ProcessMessage processes one message. 48 // 49 // If it returns nil, Fleetspeak will acknowledge the message and it 50 // will be marked as successfully processed. If it returns an error, the 51 // message will be marked as failed. 52 // 53 // Note however that in either case, the delivery of the acknowledgement 54 // is not guaranteed and there is a chance that the message will be sent 55 // again. 56 ProcessMessage(context.Context, *fspb.Message) error 57 58 // Stop shuts down the service in an orderly manner. 59 Stop() error 60 } 61 62 // LocalInfo stores summary information about the local client. 63 type LocalInfo struct { 64 ClientID common.ClientID // The ClientID of the local client. 65 Labels []*fspb.Label // The client labels (ServiceName="client") set on the local client. 66 Services []string // The Fleetspeak services currently configured on the local client. 67 } 68 69 // A Context encapsulates the functionality that the Fleetspeak client 70 // installation provides to a configured service. 71 type Context interface { 72 // Send provides a message to the Fleetspeak system for delivery to the server or to other 73 // Fleetspeak services running on the local client. 74 Send(context.Context, AckMessage) error 75 76 // GetLocalInfo returns information about the local client. 77 GetLocalInfo() *LocalInfo 78 79 // GetFileIfModified attempts to retrieve a file from the server, if it been modified since 80 // modSince. Return values follow the semantics of Communicator.GetFileIfModified. 81 GetFileIfModified(ctx context.Context, name string, modSince time.Time) (data io.ReadCloser, mod time.Time, err error) 82 83 // StatsCollector returns the stats.Collector used by the Fleetspeak client. 84 Stats() stats.Collector 85 } 86 87 // A Factory creates a Service according to a provided configuration. 88 type Factory func(conf *fspb.ClientServiceConfig) (Service, error) 89 90 // NOOPFactory is a Factory which produces a service that does nothing. It 91 // accepts any message without processing it, and produces no messages for the 92 // server. 93 func NOOPFactory(conf *fspb.ClientServiceConfig) (Service, error) { 94 return noopService{}, nil 95 } 96 97 type noopService struct{} 98 99 func (s noopService) Start(Context) error { 100 return nil 101 } 102 103 func (s noopService) ProcessMessage(context.Context, *fspb.Message) error { 104 return nil 105 } 106 107 func (s noopService) Stop() error { 108 return nil 109 }