added support for python bindings
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -28,3 +28,6 @@ logs/
|
||||
server/docker/containers/
|
||||
*.h
|
||||
*.so
|
||||
|
||||
# generic folder to ignore
|
||||
export/
|
||||
|
||||
@@ -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() {}
|
||||
|
||||
11
Bindings/python/p2prc.py
Normal file
11
Bindings/python/p2prc.py
Normal file
@@ -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
|
||||
3
Makefile
3
Makefile
@@ -11,3 +11,6 @@ run:
|
||||
|
||||
sharedObjects:
|
||||
sh build-bindings.sh
|
||||
|
||||
python:
|
||||
sh build-python-package.sh
|
||||
|
||||
28
build-python-package.sh
Normal file
28
build-python-package.sh
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user