sz-po/go-distributed-kvm-switch

Create device initialization function with hooks and abstraction layers in SDK

sz-po opened this issue · 0 comments

The purpose of this task is to create a function in the SDK that simplifies device initialization for developers. The goal is to abstract away the underlying processes, such as reading configurations from environment variables, installing the logger, starting the wire protocol, starting RPC, starting the event dispatcher, and connecting all these components.

Moreover, the function should support the ability to attach hooks to the start and stop processes, and install event handlers and RPC services with ease.

Ideally, the device developer should have a straightforward interface to work with. The example code below illustrates how it should look:

func main() {
  device.Serve[Config](
    device.WithBeforeStartFn(func(ctx device.Context) {
      ctx.GetConfig() // returns kernel provided config as a struct

      ctx.DeviceManager() // example - returns RPC of some kernel service
      ctx.MemoryManager() // example - returns RPC of some kernel service

      ctx.EventDispatcher().Dispatch(event) // example - uses device event dispatcher to send an event to kernel
    }), 
    device.WithAfterStartFn(func(...) {}), 

    device.WithBeforeStopFn(func(...) {}), 
    device.WithAfterStopFn(func(...) {}),

    device.WithRPCService(),

    device.WithEventListener(),
  )
}

Finally, the readme should include usage examples for all the above cases, as well as for events and RPCs.