|
| 1 | +# Copyright 2018 The Native Object Protocols Authors |
| 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 | +# http://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 | +# |
| 16 | +# Example of using libnop from python by wrapping operations in a shared |
| 17 | +# library. The shared library is loaded using the ctypes module. |
| 18 | +# |
| 19 | + |
| 20 | +from ctypes import * |
| 21 | +import sys |
| 22 | +import os |
| 23 | + |
| 24 | +# Define ctypes structures that mirror the basic protocol types. |
| 25 | + |
| 26 | +# Three component vector of floats. |
| 27 | +class Vec3(Structure): |
| 28 | + _fields_ = (('x', c_float), ('y', c_float), ('z', c_float)) |
| 29 | + |
| 30 | + def __str__(self): |
| 31 | + return '(%.1f, %.1f, %.1f)' % (self.x, self.y, self.z) |
| 32 | + |
| 33 | +# Triangle composed of three Vec3 structures. |
| 34 | +class Triangle(Structure): |
| 35 | + _fields_ = (('a', Vec3), ('b', Vec3), ('c', Vec3)) |
| 36 | + |
| 37 | + def __str__(self): |
| 38 | + return 'Triangle{%s, %s, %s}' % (self.a, self.b, self.c) |
| 39 | + |
| 40 | +# Base Polyhedron structure. This carries the size of the Triangle array |
| 41 | +# appended to the end of the structure. |
| 42 | +class PolyhedronBase(Structure): |
| 43 | + _fields_ = (('size', c_size_t),) |
| 44 | + |
| 45 | +# Builds a Polyhedron given either a list of Triangles or a capacity to reserve. |
| 46 | +# |
| 47 | +# This is equivalent to the following C header struct + dynamic array idiom: |
| 48 | +# |
| 49 | +# struct Polyhedron { |
| 50 | +# size_t size; |
| 51 | +# Triangle triangles[1]; |
| 52 | +# }; |
| 53 | +# |
| 54 | +# Polyhedron* p = malloc(offsetof(Polyhedron, triangles) + reserve * sizeof(Triangle)); |
| 55 | +# |
| 56 | +def Polyhedron(elements=[], reserve=None): |
| 57 | + if reserve is None: |
| 58 | + reserve = len(elements) |
| 59 | + |
| 60 | + # Triangle array of the required size. |
| 61 | + class ArrayType(Array): |
| 62 | + _type_ = Triangle |
| 63 | + _length_ = reserve |
| 64 | + |
| 65 | + # A subclass of PolyhedronBase with an array of Triangles appended. |
| 66 | + class PolyhedronType(PolyhedronBase): |
| 67 | + _fields_ = (('triangles', ArrayType),) |
| 68 | + |
| 69 | + def __str__(self): |
| 70 | + return 'Polyhedron{' + ', '.join( |
| 71 | + [str(self.triangles[i]) for i in range(self.size)] |
| 72 | + ) + '}' |
| 73 | + |
| 74 | + # Return an initialized instance of the Polyhedron. |
| 75 | + return PolyhedronType(reserve, ArrayType(*elements)) |
| 76 | + |
| 77 | +def LoadProtocolLibrary(): |
| 78 | + global ProtocolLibrary |
| 79 | + global GetSerializedPolyhedron |
| 80 | + global SerializePolyhedron |
| 81 | + global DeserializePolyhedron |
| 82 | + |
| 83 | + # Load the shared library. |
| 84 | + ProtocolLibrary = cdll.LoadLibrary('out/shared_protocol.so') |
| 85 | + |
| 86 | + # Load the exported API and setup the function pointer types. |
| 87 | + GetSerializedPolyhedron = ProtocolLibrary.GetSerializedPolyhedron |
| 88 | + GetSerializedPolyhedron.argtypes = (c_void_p, c_size_t) |
| 89 | + GetSerializedPolyhedron.restype = c_ssize_t |
| 90 | + |
| 91 | + SerializePolyhedron = ProtocolLibrary.SerializePolyhedron |
| 92 | + SerializePolyhedron.argtypes = (POINTER(PolyhedronBase), c_void_p, c_size_t) |
| 93 | + SerializePolyhedron.restype = c_ssize_t |
| 94 | + |
| 95 | + DeserializePolyhedron = ProtocolLibrary.DeserializePolyhedron |
| 96 | + DeserializePolyhedron.argtypes = (POINTER(PolyhedronBase), c_void_p, c_size_t) |
| 97 | + DeserializePolyhedron.restype = c_ssize_t |
| 98 | + |
| 99 | +def main(): |
| 100 | + LoadProtocolLibrary() |
| 101 | + |
| 102 | + # Create a buffer to hold the serialized payload. |
| 103 | + payload_buffer = create_string_buffer(1024) |
| 104 | + |
| 105 | + # Create a Polyhedron and serialize it to the buffer. |
| 106 | + polyhedron = Polyhedron( |
| 107 | + ( |
| 108 | + Triangle(Vec3(0, 0, 0), Vec3(1, 1, 1), Vec3(2, 2, 2)), |
| 109 | + Triangle(Vec3(3, 3, 3), Vec3(4, 4, 4), Vec3(5, 5, 5)), |
| 110 | + Triangle(Vec3(6, 6, 6), Vec3(7, 7, 7), Vec3(8, 8, 8)) |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + count = SerializePolyhedron(polyhedron, payload_buffer, len(payload_buffer)) |
| 115 | + if count >= 0: |
| 116 | + print count, 'bytes:', payload_buffer[0:count].encode('hex') |
| 117 | + else: |
| 118 | + print 'Error:', -count |
| 119 | + |
| 120 | + # Create an empty Polyhedron that can hold up to 10 triangles read the payload. |
| 121 | + polyhedron = Polyhedron(reserve=10) |
| 122 | + |
| 123 | + count = DeserializePolyhedron(polyhedron, payload_buffer, len(payload_buffer)) |
| 124 | + if count >= 0: |
| 125 | + print polyhedron |
| 126 | + else: |
| 127 | + print 'Error:', -count |
| 128 | + |
| 129 | + # Get a serialized Polyhedron from the library and then deserialize it. |
| 130 | + count = GetSerializedPolyhedron(payload_buffer, len(payload_buffer)) |
| 131 | + if count < 0: |
| 132 | + print 'Error:', -count |
| 133 | + return; |
| 134 | + |
| 135 | + count = DeserializePolyhedron(polyhedron, payload_buffer, len(payload_buffer)) |
| 136 | + if count >= 0: |
| 137 | + print polyhedron |
| 138 | + else: |
| 139 | + print 'Error:', -count |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + main() |
0 commit comments