From 708eacd1dbc78ad1a9980b4221ab3705c08e2f66 Mon Sep 17 00:00:00 2001 From: Akilan Date: Fri, 10 Nov 2023 21:00:24 +0000 Subject: [PATCH] added support for python bindings --- .gitignore | 3 + Bindings/Client.go | 140 ++++++++++++++++++++++++++++++--------- Bindings/python/p2prc.py | 11 +++ Makefile | 3 + build-python-package.sh | 28 ++++++++ 5 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 Bindings/python/p2prc.py create mode 100644 build-python-package.sh diff --git a/.gitignore b/.gitignore index 6e4cb28..f436bcc 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ logs/ server/docker/containers/ *.h *.so + +# generic folder to ignore +export/ diff --git a/Bindings/Client.go b/Bindings/Client.go index 4d6318e..13d9f30 100644 --- a/Bindings/Client.go +++ b/Bindings/Client.go @@ -2,10 +2,13 @@ package main import "C" import ( - "encoding/json" - "github.com/Akilan1999/p2p-rendering-computation/abstractions" - "github.com/Akilan1999/p2p-rendering-computation/client" - "github.com/Akilan1999/p2p-rendering-computation/server" + "encoding/json" + "github.com/Akilan1999/p2p-rendering-computation/abstractions" + "github.com/Akilan1999/p2p-rendering-computation/client" + "github.com/Akilan1999/p2p-rendering-computation/client/clientIPTable" + "github.com/Akilan1999/p2p-rendering-computation/p2p" + "github.com/Akilan1999/p2p-rendering-computation/plugin" + "github.com/Akilan1999/p2p-rendering-computation/server" ) // The Client package where data-types @@ -13,52 +16,127 @@ import ( // to a string so that it can // be export +// --------------------------------- Container Control ---------------------------------------- + //export StartContainer func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string, baseImage string) (output *C.char) { - container, err := client.StartContainer(IP, NumPorts, GPU, ContainerName, baseImage) - if err != nil { - return C.CString(err.Error()) - } - return ConvertStructToJSONString(container) + container, err := client.StartContainer(IP, NumPorts, GPU, ContainerName, baseImage) + if err != nil { + return C.CString(err.Error()) + } + return ConvertStructToJSONString(container) } +//export RemoveContainer +func RemoveContainer(IP string, ID string) (output *C.char) { + err := client.RemoveContianer(IP, ID) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("Success") +} + +// --------------------------------- Plugin Control ---------------------------------------- + +//export ViewPlugin +func ViewPlugin() (output *C.char) { + plugins, err := plugin.DetectPlugins() + if err != nil { + return C.CString(err.Error()) + } + return ConvertStructToJSONString(plugins) +} + +//export PullPlugin +func PullPlugin(pluginUrl string) (output *C.char) { + err := plugin.DownloadPlugin(pluginUrl) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("Success") +} + +//export DeletePlugin +func DeletePlugin(pluginName string) (output *C.char) { + err := plugin.DeletePlugin(pluginName) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("Success") +} + +//export ExecutePlugin +func ExecutePlugin(pluginname string, ContainerID string) (output *C.char) { + err := plugin.RunPluginContainer(pluginname, ContainerID) + if err != nil { + return C.CString(err.Error()) + } + return C.CString("Success") +} + +// --------------------------------- Get Specs ---------------------------------------- + //export GetSpecs func GetSpecs(IP string) (output *C.char) { - specs, err := client.GetSpecs(IP) - if err != nil { - return C.CString(err.Error()) - } - return ConvertStructToJSONString(specs) + specs, err := client.GetSpecs(IP) + if err != nil { + return C.CString(err.Error()) + } + return ConvertStructToJSONString(specs) } //export Init func Init(customConfig string) (output *C.char) { - init, err := abstractions.Init(customConfig) - if err != nil { - return C.CString(err.Error()) - } - return ConvertStructToJSONString(init) + init, err := abstractions.Init(customConfig) + if err != nil { + return C.CString(err.Error()) + } + return ConvertStructToJSONString(init) } +// --------------------------------- P2P Controls ----------------------------------- + +//export ViewIPTable +func ViewIPTable() (output *C.char) { + table, err := p2p.ReadIpTable() + if err != nil { + return C.CString(err.Error()) + } + return ConvertStructToJSONString(table) +} + +//export UpdateIPTable +func UpdateIPTable() (output *C.char) { + err := clientIPTable.UpdateIpTableListClient() + if err != nil { + return C.CString(err.Error()) + } + return C.CString("Success") +} + +// --------------------------------- Controlling Server ---------------------------------------- + //export Server func Server() (output *C.char) { - _, err := server.Server() - if err != nil { - return C.CString(err.Error()) - } + _, err := server.Server() + if err != nil { + return C.CString(err.Error()) + } - return ConvertStructToJSONString("") + return ConvertStructToJSONString("") } -func ConvertStructToJSONString(Struct interface{}) *C.char { - jsonBytes, err := json.Marshal(Struct) - if err != nil { - return C.CString(err.Error()) - } +// --------------------------------- Helper Functions ---------------------------------------- - // Convert the JSON bytes to a string - return C.CString(string(jsonBytes)) +func ConvertStructToJSONString(Struct interface{}) *C.char { + jsonBytes, err := json.Marshal(Struct) + if err != nil { + return C.CString(err.Error()) + } + + // Convert the JSON bytes to a string + return C.CString(string(jsonBytes)) } func main() {} diff --git a/Bindings/python/p2prc.py b/Bindings/python/p2prc.py new file mode 100644 index 0000000..07376d2 --- /dev/null +++ b/Bindings/python/p2prc.py @@ -0,0 +1,11 @@ +import ctypes + +p2prc = ctypes.CDLL("SharedOBjects/p2prc.so") + +p2prc.Init("") + +def StartServer(): + # Starting P2PRC as a server mode + p2prc.Server() + for _ in iter(int, 1): + pass \ No newline at end of file diff --git a/Makefile b/Makefile index 4795b7c..b9dc0e3 100644 --- a/Makefile +++ b/Makefile @@ -11,3 +11,6 @@ run: sharedObjects: sh build-bindings.sh + +python: + sh build-python-package.sh diff --git a/build-python-package.sh b/build-python-package.sh new file mode 100644 index 0000000..93a11d1 --- /dev/null +++ b/build-python-package.sh @@ -0,0 +1,28 @@ +#!/usr/bin/bash + +# Create export directory for python +mkdir Bindings/python/export + +# Creating SharedObjects directory for python +mkdir Bindings/python/export/SharedObjects + +sh build-bindings.sh + +cp Bindings/p2prc.h Bindings/python/export/SharedObjects/ +cp Bindings/p2prc.so Bindings/python/export/SharedObjects/ + +cp Bindings/python/p2prc.py Bindings/python/export/ + +echo "Output is in the Directory Bindings/python/export/" + +# Architectures for Linux +#archs=(amd64 arm64) +# +#for arch in ${archs[@]} +#do +# mkdir Bindings/python/export/SharedObjects/linux-${arch} +# cd Bindings/ +# env GOOS=linux GOARCH=${arch} go build -buildmode=c-shared -o python/export/SharedObjects/linux-${arch}/p2prc.so +# echo "GOOS=linux GOARCH=${arch} go build -buildmode=c-shared -o python/export/SharedObjects/linux-${arch}/p2prc.so" +# cd .. +#done \ No newline at end of file