OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2014 Google Inc. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. */ |
| 4 |
| 5 #include <python2.7/Python.h> |
| 6 #include <libtar.h> |
| 7 #include <locale.h> |
| 8 #include <stdio.h> |
| 9 #include <fcntl.h> |
| 10 #include <sys/mount.h> |
| 11 #include <errno.h> |
| 12 |
| 13 #include "ppapi/cpp/var.h" |
| 14 #include "ppapi/cpp/var_array.h" |
| 15 #include "ppapi/cpp/var_dictionary.h" |
| 16 #include "ppapi_simple/ps_interface.h" |
| 17 |
| 18 #include "nacl_io/nacl_io.h" |
| 19 #include "ppapi_simple/ps_main.h" |
| 20 #include "ppapi_simple/ps_instance.h" |
| 21 |
| 22 #ifdef __pnacl__ |
| 23 #define DATA_FILE "pydata_pnacl.tar" |
| 24 #else |
| 25 #error "Unknown arch" |
| 26 #endif |
| 27 |
| 28 static int setup_unix_environment() { |
| 29 int ret = umount("/"); |
| 30 if (ret) { |
| 31 printf("unmounting root fs failed\n"); |
| 32 return 1; |
| 33 } |
| 34 |
| 35 ret = mount("", "/", "memfs", 0, NULL); |
| 36 if (ret) { |
| 37 printf("mounting root fs failed\n"); |
| 38 return 1; |
| 39 } |
| 40 |
| 41 const char* data_url = getenv("NACL_DATA_URL"); |
| 42 if (!data_url) |
| 43 data_url = "./"; |
| 44 mkdir("/mnt/http", 0777); |
| 45 ret = mount(data_url, "/mnt/http", "httpfs", 0, "allow_cross_origin_requests
:true allow_credentials:false"); |
| 46 if (ret) { |
| 47 printf("mounting http filesystem failed\n"); |
| 48 return 1; |
| 49 } |
| 50 |
| 51 char filename[PATH_MAX]; |
| 52 strcpy(filename, "/mnt/http/"); |
| 53 strcat(filename, DATA_FILE); |
| 54 TAR* tar; |
| 55 ret = tar_open(&tar, filename, NULL, O_RDONLY, 0, 0); |
| 56 if (ret) { |
| 57 printf("error opening %s\n", filename); |
| 58 return 1; |
| 59 } |
| 60 |
| 61 mkdir("/lib", 0777); |
| 62 ret = tar_extract_all(tar, (char *)"/"); |
| 63 if (ret) { |
| 64 printf("error extracting %s\n", filename); |
| 65 return 1; |
| 66 } |
| 67 |
| 68 ret = tar_close(tar); |
| 69 |
| 70 setenv("PYTHONHOME", "", 1); |
| 71 return 0; |
| 72 } |
| 73 |
| 74 extern "C" { |
| 75 |
| 76 static PyObject * post_json_message(PyObject * self, PyObject * args) { |
| 77 char * stream; |
| 78 char * json; |
| 79 if (!PyArg_ParseTuple(args, "ss", &stream, &json)) { |
| 80 return NULL; |
| 81 } |
| 82 |
| 83 pp::VarDictionary message; |
| 84 message.Set("stream", pp::Var(stream)); |
| 85 message.Set("json", pp::Var(json)); |
| 86 |
| 87 const PPB_Messaging * messaging = PSInterfaceMessaging(); |
| 88 PP_Instance instance = PSGetInstanceId(); |
| 89 messaging->PostMessage(instance, message.pp_var()); |
| 90 |
| 91 Py_RETURN_NONE; |
| 92 } |
| 93 |
| 94 static PyObject * acquire_json_message_wait(PyObject *self, PyObject * args) { |
| 95 if (!PyArg_ParseTuple(args, "")) { |
| 96 return NULL; |
| 97 } |
| 98 |
| 99 const PPB_Messaging * messaging = PSInterfaceMessaging(); |
| 100 PP_Instance instance = PSGetInstanceId(); |
| 101 PSEventSetFilter(PSE_INSTANCE_HANDLEMESSAGE); |
| 102 PSEvent *event = PSEventWaitAcquire(); |
| 103 if (event->type != PSE_INSTANCE_HANDLEMESSAGE) |
| 104 Py_RETURN_NONE; |
| 105 |
| 106 pp::Var message(event->as_var); |
| 107 |
| 108 if (!message.is_dictionary()) |
| 109 Py_RETURN_NONE; |
| 110 |
| 111 pp::VarDictionary request(message); |
| 112 pp::Var json(request.Get("json")); |
| 113 if (!json.is_string()) |
| 114 Py_RETURN_NONE; |
| 115 |
| 116 return PyString_FromString(json.AsString().c_str()); |
| 117 } |
| 118 |
| 119 } |
| 120 |
| 121 static PyMethodDef PPMessageMethods[] = { |
| 122 { |
| 123 "_PostJSONMessage", |
| 124 post_json_message, |
| 125 METH_VARARGS, |
| 126 "Post a message encoded as JSON" |
| 127 }, |
| 128 { |
| 129 "_AcquireJSONMessageWait", |
| 130 acquire_json_message_wait, |
| 131 METH_VARARGS, |
| 132 "Acquire a message encoded as JSON (blocking)"}, |
| 133 {NULL, NULL, 0, NULL} |
| 134 }; |
| 135 |
| 136 int ipython_kernel_main(int argc, char **argv) { |
| 137 printf("Setting up unix environment...\n"); |
| 138 if (setup_unix_environment()) { |
| 139 printf("Error: %s\n", strerror(errno)); |
| 140 return -1; |
| 141 } |
| 142 printf("done\n"); |
| 143 |
| 144 // Initialize Pepper API |
| 145 PSInterfaceInit(); |
| 146 |
| 147 int quit = 0; |
| 148 |
| 149 while(!quit) { |
| 150 // Initialize Python interpreter |
| 151 Py_Initialize(); |
| 152 |
| 153 // Load module that provides access to Pepper messaging API |
| 154 // from within the interpreter |
| 155 Py_InitModule("ppmessage", PPMessageMethods); |
| 156 |
| 157 // Run the interpreter main loop. |
| 158 const char * main_filename = "/mnt/http/kernel.py"; |
| 159 FILE *main = fopen(main_filename, "r"); |
| 160 if (main == NULL) { |
| 161 printf("failed to load interpreter code\n"); |
| 162 return -1; |
| 163 } |
| 164 |
| 165 quit = PyRun_SimpleFileEx(main, main_filename, 1); |
| 166 |
| 167 Py_Finalize(); |
| 168 } |
| 169 |
| 170 return 0; |
| 171 } |
| 172 |
| 173 PPAPI_SIMPLE_REGISTER_MAIN(ipython_kernel_main) |
OLD | NEW |