Skip to content

Commit

Permalink
added support for python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Akilan1999 committed Nov 10, 2023
1 parent 979e856 commit 708eacd
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ logs/
server/docker/containers/
*.h
*.so

# generic folder to ignore
export/
138 changes: 108 additions & 30 deletions Bindings/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,141 @@ 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
// are manually converted to the
// 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("")
}

// --------------------------------- Helper Functions ----------------------------------------

func ConvertStructToJSONString(Struct interface{}) *C.char {
jsonBytes, err := json.Marshal(Struct)
if err != nil {
return C.CString(err.Error())
}
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))
// Convert the JSON bytes to a string
return C.CString(string(jsonBytes))
}

func main() {}
11 changes: 11 additions & 0 deletions Bindings/python/p2prc.py
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ run:

sharedObjects:
sh build-bindings.sh

python:
sh build-python-package.sh
28 changes: 28 additions & 0 deletions build-python-package.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 708eacd

Please sign in to comment.