def main(): port = mach.BootstrapServer.checkin("org.freenas.test.mach.ipc-server") print 'Service port: {0}'.format(port) while True: msg = port.receive() print 'Received message: {0}'.format(msg.body) if msg.body == 'EXIT': print 'Exiting cleanly' break else: # Send response print 'Sending response to remote port {0}'.format(msg.remote_port) reply = mach.Message() reply.body = bytearray(msg.body) reply.bits = mach.make_msg_bits( mach.MessageType.MACH_MSG_TYPE_COPY_SEND, 0) mach.null_port.send(msg.remote_port, reply)
# Create send port try: send = mach.Port() send.insert_right(mach.MessageType.MACH_MSG_TYPE_MAKE_SEND) print 'Send port: {0}'.format(send) except mach.MachException, e: fail('Cannot create send port: {0}'.format(e)) # Create receive port try: receive = mach.Port() receive.insert_right(mach.MessageType.MACH_MSG_TYPE_MAKE_SEND) print 'Receive port: {0}'.format(receive) except mach.MachException, e: fail('Cannot create receive port: {0}'.format(e)) threading.Thread(target=server, args=(receive, )).start() msg = mach.Message() msg.bits = mach.make_msg_bits(mach.MessageType.MACH_MSG_TYPE_COPY_SEND, mach.MessageType.MACH_MSG_TYPE_MAKE_SEND) msg.body = bytearray(struct.pack('256p', 'Hello World')) print 'Sent text: {0}'.format('Hello World') try: send.send(receive, msg) except mach.SendMessageException, e: fail('Cannot send message: {0}'.format(e)) if __name__ == '__main__': main()