Ejemplo n.º 1
0
bool
IcePHP::createStringArray(zval* zv, const Ice::StringSeq& seq)
{
    array_init(zv);
    for(Ice::StringSeq::const_iterator p = seq.begin(); p != seq.end(); ++p)
    {
        if(add_next_index_stringl(zv, STRCAST(p->c_str()), static_cast<uint>(p->length())) == FAILURE)
        {
            return false;
        }
    }

    return true;
}
Ejemplo n.º 2
0
bool
IcePy::stringSeqToList(const Ice::StringSeq& seq, PyObject* l)
{
    assert(PyList_Check(l));

    for(Ice::StringSeq::const_iterator p = seq.begin(); p != seq.end(); ++p)
    {
        PyObject* str = Py_BuildValue(STRCAST("s"), p->c_str());
        if(!str)
        {
            Py_DECREF(l);
            return false;
        }
        int status = PyList_Append(l, str);
        Py_DECREF(str); // Give ownership to the list.
        if(status < 0)
        {
            Py_DECREF(l);
            return false;
        }
    }

    return true;
}
Ejemplo n.º 3
0
static int
communicatorInit(CommunicatorObject* self, PyObject* args, PyObject* /*kwds*/)
{
    //
    // The argument options are:
    //
    // Ice.initialize()
    // Ice.initialize(args)
    // Ice.initialize(initData)
    // Ice.initialize(configFile)
    // Ice.initialize(args, initData)
    // Ice.initialize(args, configFile)
    //

    PyObject* arg1 = 0;
    PyObject* arg2 = 0;
    if(!PyArg_ParseTuple(args, STRCAST("|OO"), &arg1, &arg2))
    {
        return -1;
    }

    PyObject* argList = 0;
    PyObject* initData = 0;
    PyObject* configFile = 0;

    if(arg1 == Py_None)
    {
        arg1 = 0;
    }

    if(arg2 == Py_None)
    {
        arg2 = 0;
    }

    PyObject* initDataType = lookupType("Ice.InitializationData");

    if(arg1)
    {
        if(PyList_Check(arg1))
        {
            argList = arg1;
        }
        else if(PyObject_IsInstance(arg1, initDataType))
        {
            initData = arg1;
        }
        else if(checkString(arg1))
        {
            configFile = arg1;
        }
        else
        {
            PyErr_Format(PyExc_ValueError,
                STRCAST("initialize expects an argument list, Ice.InitializationData or a configuration filename"));
            return -1;
        }
    }

    if(arg2)
    {
        if(PyList_Check(arg2))
        {
            if(argList)
            {
                PyErr_Format(PyExc_ValueError, STRCAST("unexpected list argument to initialize"));
                return -1;
            }
            argList = arg2;
        }
        else if(PyObject_IsInstance(arg2, initDataType))
        {
            if(initData)
            {
                PyErr_Format(PyExc_ValueError, STRCAST("unexpected Ice.InitializationData argument to initialize"));
                return -1;
            }
            initData = arg2;
        }
        else if(checkString(arg2))
        {
            if(configFile)
            {
                PyErr_Format(PyExc_ValueError, STRCAST("unexpected string argument to initialize"));
                return -1;
            }
            configFile = arg2;
        }
        else
        {
            PyErr_Format(PyExc_ValueError,
                STRCAST("initialize expects an argument list, Ice.InitializationData or a configuration filename"));
            return -1;
        }
    }

    if(initData && configFile)
    {
        PyErr_Format(PyExc_ValueError,
            STRCAST("initialize accepts either Ice.InitializationData or a configuration filename"));
        return -1;
    }

    Ice::StringSeq seq;
    if(argList && !listToStringSeq(argList, seq))
    {
        return -1;
    }

    Ice::InitializationData data;
    DispatcherPtr dispatcherWrapper;

    try
    {
        if(initData)
        {
            PyObjectHandle properties = getAttr(initData, "properties", false);
            PyObjectHandle logger = getAttr(initData, "logger", false);
            PyObjectHandle threadHook = getAttr(initData, "threadHook", false);
            PyObjectHandle threadStart = getAttr(initData, "threadStart", false);
            PyObjectHandle threadStop = getAttr(initData, "threadStop", false);
            PyObjectHandle batchRequestInterceptor = getAttr(initData, "batchRequestInterceptor", false);
            PyObjectHandle dispatcher = getAttr(initData, "dispatcher", false);

            if(properties.get())
            {
                //
                // Get the properties implementation.
                //
                PyObjectHandle impl = getAttr(properties.get(), "_impl", false);
                assert(impl.get());
                data.properties = getProperties(impl.get());
            }

            if(logger.get())
            {
                data.logger = new LoggerWrapper(logger.get());
            }

            if(threadHook.get() || threadStart.get() || threadStop.get())
            {
                data.threadHook = new ThreadHook(threadHook.get(), threadStart.get(), threadStop.get());
            }

            if(dispatcher.get())
            {
                dispatcherWrapper = new Dispatcher(dispatcher.get());
                data.dispatcher = dispatcherWrapper;
            }

            if(batchRequestInterceptor.get())
            {
                data.batchRequestInterceptor = new BatchRequestInterceptor(batchRequestInterceptor.get());
            }
        }

        //
        // We always supply our own implementation of ValueFactoryManager.
        //
        data.valueFactoryManager = new ValueFactoryManager;

        if(!data.properties)
        {
            data.properties = Ice::createProperties();
        }

        if(configFile)
        {
            data.properties->load(getString(configFile));
        }

        if(argList)
        {
            data.properties = Ice::createProperties(seq, data.properties);
        }
    }
    catch(const Ice::Exception& ex)
    {
        setPythonException(ex);
        return -1;
    }

    //
    // Remaining command line options are passed to the communicator
    // as an argument vector in case they contain plug-in properties.
    //
    int argc = static_cast<int>(seq.size());
    char** argv = new char*[argc + 1];
    int i = 0;
    for(Ice::StringSeq::const_iterator s = seq.begin(); s != seq.end(); ++s, ++i)
    {
        argv[i] = strdup(s->c_str());
    }
    argv[argc] = 0;

    data.compactIdResolver = new IdResolver;

    Ice::CommunicatorPtr communicator;
    try
    {
        AllowThreads allowThreads;
        if(argList)
        {
            communicator = Ice::initialize(argc, argv, data);
        }
        else
        {
            communicator = Ice::initialize(data);
        }
    }
    catch(const Ice::Exception& ex)
    {
        for(i = 0; i < argc; ++i)
        {
            free(argv[i]);
        }
        delete[] argv;

        setPythonException(ex);
        return -1;
    }

    //
    // Replace the contents of the given argument list with the filtered arguments.
    //
    if(argList)
    {
        PyList_SetSlice(argList, 0, PyList_Size(argList), 0); // Clear the list.

        for(i = 0; i < argc; ++i)
        {
            PyObjectHandle str = Py_BuildValue(STRCAST("s"), argv[i]);
            PyList_Append(argList, str.get());
        }
    }

    for(i = 0; i < argc; ++i)
    {
        free(argv[i]);
    }
    delete[] argv;

    self->communicator = new Ice::CommunicatorPtr(communicator);

    CommunicatorMap::iterator p = _communicatorMap.find(communicator);
    if(p != _communicatorMap.end())
    {
        _communicatorMap.erase(p);
    }
    _communicatorMap.insert(CommunicatorMap::value_type(communicator, reinterpret_cast<PyObject*>(self)));

    if(dispatcherWrapper)
    {
        self->dispatcher = new DispatcherPtr(dispatcherWrapper);
        dispatcherWrapper->setCommunicator(communicator);
    }

    return 0;
}
Ejemplo n.º 4
0
static int
communicatorInit(CommunicatorObject* self, PyObject* args, PyObject* /*kwds*/)
{
    PyObject* argList = 0;
    PyObject* initData = 0;
    if(!PyArg_ParseTuple(args, STRCAST("|OO"), &argList, &initData))
    {
        return -1;
    }

    if(argList == Py_None)
    {
        argList = 0;
    }

    if(initData == Py_None)
    {
        initData = 0;
    }

    PyObject* initDataType = lookupType("Ice.InitializationData");

    if(argList && !initData)
    {
        if(PyObject_IsInstance(argList, initDataType))
        {
            initData = argList;
            argList = 0;
        }
        else if(!PyList_Check(argList))
        {
            PyErr_Format(PyExc_ValueError, STRCAST("initialize expects an argument list or Ice.InitializationData"));
            return -1;
        }
    }
    else if(argList && initData)
    {
        if(!PyList_Check(argList) || !PyObject_IsInstance(initData, initDataType))
        {
            PyErr_Format(PyExc_ValueError, STRCAST("initialize expects an argument list and Ice.InitializationData"));
            return -1;
        }
    }

    Ice::StringSeq seq;
    if(argList && !listToStringSeq(argList, seq))
    {
        return -1;
    }

    //
    // Use the with-args or the without-args version of initialize()?
    //
    bool hasArgs = argList != 0;

    Ice::InitializationData data;

    if(initData)
    {
        PyObjectHandle properties = PyObject_GetAttrString(initData, STRCAST("properties"));
        PyObjectHandle logger = PyObject_GetAttrString(initData, STRCAST("logger"));
        PyObjectHandle threadHook = PyObject_GetAttrString(initData, STRCAST("threadHook"));
        PyObjectHandle batchRequestInterceptor = PyObject_GetAttrString(initData, STRCAST("batchRequestInterceptor"));

        PyErr_Clear(); // PyObject_GetAttrString sets an error on failure.

        if(properties.get() && properties.get() != Py_None)
        {
            //
            // Get the properties implementation.
            //
            PyObjectHandle impl = PyObject_GetAttrString(properties.get(), STRCAST("_impl"));
            assert(impl.get());
            data.properties = getProperties(impl.get());
        }

        if(logger.get() && logger.get() != Py_None)
        {
            data.logger = new LoggerWrapper(logger.get());
        }

        if(threadHook.get() && threadHook.get() != Py_None)
        {
            data.threadHook = new ThreadHook(threadHook.get());
        }

        if(batchRequestInterceptor.get() && batchRequestInterceptor.get() != Py_None)
        {
            data.batchRequestInterceptor = new BatchRequestInterceptor(batchRequestInterceptor.get());
        }
    }

    //
    // We always supply our own implementation of ValueFactoryManager.
    //
    data.valueFactoryManager = new ValueFactoryManager;

    try
    {
        if(argList)
        {
            data.properties = Ice::createProperties(seq, data.properties);
        }
        else if(!data.properties)
        {
            data.properties = Ice::createProperties();
        }
    }
    catch(const Ice::Exception& ex)
    {
        setPythonException(ex);
        return -1;
    }

    //
    // Remaining command line options are passed to the communicator
    // as an argument vector in case they contain plug-in properties.
    //
    int argc = static_cast<int>(seq.size());
    char** argv = new char*[argc + 1];
    int i = 0;
    for(Ice::StringSeq::const_iterator s = seq.begin(); s != seq.end(); ++s, ++i)
    {
        argv[i] = strdup(s->c_str());
    }
    argv[argc] = 0;

    data.compactIdResolver = new IdResolver;

    Ice::CommunicatorPtr communicator;
    try
    {
        AllowThreads allowThreads;
        if(hasArgs)
        {
            communicator = Ice::initialize(argc, argv, data);
        }
        else
        {
            communicator = Ice::initialize(data);
        }
    }
    catch(const Ice::Exception& ex)
    {
        for(i = 0; i < argc; ++i)
        {
            free(argv[i]);
        }
        delete[] argv;

        setPythonException(ex);
        return -1;
    }

    //
    // Replace the contents of the given argument list with the filtered arguments.
    //
    if(argList)
    {
        PyList_SetSlice(argList, 0, PyList_Size(argList), 0); // Clear the list.

        for(i = 0; i < argc; ++i)
        {
            PyObjectHandle str = Py_BuildValue(STRCAST("s"), argv[i]);
            PyList_Append(argList, str.get());
        }
    }

    for(i = 0; i < argc; ++i)
    {
        free(argv[i]);
    }
    delete[] argv;

    self->communicator = new Ice::CommunicatorPtr(communicator);

    CommunicatorMap::iterator p = _communicatorMap.find(communicator);
    if(p != _communicatorMap.end())
    {
        _communicatorMap.erase(p);
    }
    _communicatorMap.insert(CommunicatorMap::value_type(communicator, reinterpret_cast<PyObject*>(self)));

    return 0;
}