Beispiel #1
0
int main()
{
    init_python_api("cc4b05b9-4ff9-4127-85b0-300298d16c3c");

    setup_test_functions();

    PyWorldTime * world_time = newPyWorldTime();
    assert(world_time != 0);

    run_python_string("from server import WorldTime");
    expect_python_error("WorldTime()", PyExc_TypeError);
    run_python_string("WorldTime(23)");
    // FIXME This started failing with Python 2.7
    // run_python_string("WorldTime(23.1)");

    run_python_string("w=WorldTime(23)");
    run_python_string("w.season");
    expect_python_error("w.foo", PyExc_AttributeError);
    run_python_string("w.is_now('morning')");
    expect_python_error("w.is_now(1)", PyExc_TypeError);
    run_python_string("w.seconds()");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    run_python_string("sabotage.null(w)");
    // Hit the assert checks.
    expect_python_error("w.is_now('morning')", PyExc_AssertionError);
    expect_python_error("w.seconds()", PyExc_AssertionError);
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
int main()
{
    init_python_api();

    Py_InitModule("testmod", no_methods);

    run_python_string("import server");
    run_python_string("import testmod");
    run_python_string("from atlas import Operation");
    run_python_string("class settlerMind(server.Thing):\n"
                      " def look_operation(self, op): pass\n"
                      " def delete_operation(self, op):\n"
                      "  return Operation('sight') + Operation('move')\n"
                      " def test_hook(self, ent): pass\n"
                     );
    run_python_string("testmod.settlerMind=settlerMind");

    TypeNode * tn = new TypeNode();
    tn->name() = etype;

    MindFactory * mf = MindFactory::instance();

    mf->addMindType(etype, "testmod");

    mf->newMind("1", 1, tn);

    tn->name() = etype;

    mf->newMind("2", 2, tn);

    shutdown_python_api();
    return 0;
}
int main()
{
    PyImport_AppendInittab("testprop", [](){
        auto module = new TestProp();
        return module->module().ptr();
    });
    init_python_api({&CyPy_Server::init,
                     &CyPy_Rules::init,
                     &CyPy_Atlas::init,
                     &CyPy_Physics::init,
                     &CyPy_Common::init});


    Ref<Entity> wrld(new Entity("0", 0));
    TestWorld tw(wrld);

    run_python_string("from server import *");
    run_python_string("import testprop");
    run_python_string("t=Thing('1')");
    run_python_string("t.props.line == None");
    run_python_string("t.props.statistics == None");
    run_python_string("t.props.terrain == None");
    run_python_string("testprop.add_properties(t)");
    run_python_string("t.props.line");
    run_python_string("t.props.statistics");
    run_python_string("t.props.terrain");


    shutdown_python_api();
    return 0;
}
Beispiel #4
0
int main(int argc, char ** argv)
{
    int config_status = loadConfig(argc, argv, USAGE_CLIENT); 
    if (config_status < 0) {
        if (config_status == CONFIG_VERSION) {
            reportVersion(argv[0]);
            return 0;
        } else if (config_status == CONFIG_HELP) {
            showUsage(argv[0], USAGE_CLIENT, "[ [package.]function]");
            return 0;
        } else if (config_status != CONFIG_ERROR) {
            log(ERROR, "Unknown error reading configuration.");
        }
        // Fatal error loading config file
        return 1;
    }

    int optind = config_status;

    assert(optind <= argc);

    if (optind == (argc - 1)) {
        std::string arg(argv[optind]);
        std::string::size_type pos = arg.rfind(".");
        if (pos == std::string::npos) {
            // std::cout << "function " << arg << std::endl << std::flush;
            function = arg;
        } else {
            package = arg.substr(0, pos);
            function = arg.substr(pos + 1);
            // std::cout << "module.function " << package << "." << function << std::endl << std::flush;
        }
    } else if (optind != argc) {
        usage(argv[0]);
        return 1;
    }

    bool interactive = global_conf->findItem("", "interactive");
    int status = 0;

    init_python_api(ruleset_name, false);
    extend_client_python_api();
    new ClientPropertyManager();

    if (interactive) {
        python_prompt();
    } else {
        std::map<std::string, std::string> keywords;
        keywords["account"] = account;
        keywords["password"] = password;
        keywords["host"] = server;
        python_client_script(package, function, keywords);
    }

    shutdown_python_api();

    return status;
}
Beispiel #5
0
int main()
{
    init_python_api("c67ce8e7-d195-4806-b8e4-d905e7c9d928");

    setup_test_functions();

    PyRootEntity * ent = newPyRootEntity();
    assert(ent != 0);

    run_python_string("from atlas import Entity");
    run_python_string("from atlas import Location");
    run_python_string("Entity('1')");
    expect_python_error("Entity(1)", PyExc_TypeError);
    expect_python_error("Entity('1', location='loc')", PyExc_TypeError);
    run_python_string("l=Location()");
    run_python_string("Entity('1', location=l)");
    run_python_string("Entity('1', pos=())");
    run_python_string("Entity('1', pos=[])");
    expect_python_error("Entity('1', pos=(1,1.0,'1'))", PyExc_TypeError);
    expect_python_error("Entity('1', pos=[1,1.0,'1'])", PyExc_TypeError);
    run_python_string("Entity('1', tasks=[{'name': 'twist', 'param': 'value'}])");
    expect_python_error("Entity('1', pos=1)", PyExc_TypeError);
    expect_python_error("Entity('1', parent=1)", PyExc_TypeError);
    run_python_string("Entity('1', parent='0')");
    expect_python_error("Entity('1', type=1)", PyExc_TypeError);
    run_python_string("Entity('1', type='pig')");
    run_python_string("Entity('1', other=1)");
    expect_python_error("Entity('1', other=set([1,1]))", PyExc_TypeError);
    run_python_string("e=Entity()");
    run_python_string("e.get_name()");
    run_python_string("e.name");
    run_python_string("e.id");
    expect_python_error("e.foo", PyExc_AttributeError);
    run_python_string("e.name='Bob'");
    expect_python_error("e.name=1", PyExc_TypeError);
    run_python_string("e.foo='Bob'");
    run_python_string("e.bar=1");
    run_python_string("e.baz=[1,2.0,'three']");
    run_python_string("e.qux={'mim': 23}");
    run_python_string("e.ptr=set([1,2])");
    run_python_string("e.foo");
    run_python_string("e.ptr");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("get_name_methd=e.get_name");
    run_python_string("sabotage.null(e)");
    expect_python_error("get_name_methd()", PyExc_AssertionError);
    expect_python_error("e.name", PyExc_AssertionError);
    expect_python_error("e.name='Bob'", PyExc_AssertionError);
#endif // NDEBUG


    shutdown_python_api();
    return 0;
}
Beispiel #6
0
int main()
{
    init_python_api("df4d61a3-b435-47b7-862d-63bb27681219");

    run_python_string("from physics import Point3D");
    run_python_string("from atlas import Message");
    run_python_string("Point3D([Message(1), Message(0), Message(0)])");
    expect_python_error("Point3D([Message('1'), Message(0), Message(0)])",
                        PyExc_TypeError);
    run_python_string("p=Point3D(1,0,0)");
    run_python_string("p1=Point3D(0,1,0)");
    run_python_string("p2=Point3D(0,1,0)");
    run_python_string("print Point3D()");
    expect_python_error("print Point3D('1')", PyExc_TypeError);
    expect_python_error("print Point3D([1])", PyExc_ValueError);
    run_python_string("print Point3D([1,0,0])");
    run_python_string("print Point3D([1.1,0.0,0.0])");
    expect_python_error("print Point3D(['1','1','1'])", PyExc_TypeError);
    expect_python_error("print Point3D(1.1)", PyExc_TypeError);
    run_python_string("print Point3D(1.1,0.0,0.0)");
    expect_python_error("print Point3D('1','1','1')", PyExc_TypeError);
    expect_python_error("print Point3D(1.1,0.0,0.0,1.1)", PyExc_TypeError);
    run_python_string("print repr(p)");
    run_python_string("print p.mag()");
    run_python_string("print p.unit_vector_to(p1)");
    expect_python_error("print p.unit_vector_to(1.0)", PyExc_TypeError);
    run_python_string("print p.distance(p1)");
    expect_python_error("print p.distance(1.0)", PyExc_TypeError);
    run_python_string("print p.is_valid()");
    run_python_string("print p");
    run_python_string("print p.x");
    run_python_string("print p.y");
    run_python_string("print p.z");
    run_python_string("print p[0]");
    run_python_string("print p[1]");
    run_python_string("print p[2]");
    run_python_string("print p[-1]");
    expect_python_error("print p[3]", PyExc_IndexError);
    run_python_string("p[0]=1.0");
    run_python_string("p[1]=1.0");
    run_python_string("p[2]=1.0");
    expect_python_error("p[3]=1", PyExc_IndexError);
    run_python_string("print p == p1");
    run_python_string("print p1 == p2");
    run_python_string("from physics import Vector3D");
    run_python_string("v=Vector3D(1,0,0)");
    run_python_string("print p + v");
    expect_python_error("print p + p1", PyExc_TypeError);
    run_python_string("print p - v");
    run_python_string("print p - p1");
    expect_python_error("print p - 1.0", PyExc_TypeError);
    run_python_string("print p == v");

    shutdown_python_api();
    return 0;
}
int main()
{
    init_python_api("bac81904-0516-4dd0-b9d8-32e879339b96");

    setup_test_functions();

    run_python_string("from server import *");
    run_python_string("import testprop");
    run_python_string("t=Thing('1')");
    expect_python_error("t.terrain", PyExc_AttributeError);
    run_python_string("testprop.add_properties(t)");
    run_python_string("terrain = t.terrain");
    expect_python_error("terrain.foo = 1", PyExc_AttributeError);
    expect_python_error("terrain.get_height()", PyExc_TypeError);
    run_python_string("terrain.get_height(0,0)");
    expect_python_error("terrain.get_surface()", PyExc_TypeError);
    expect_python_error("terrain.get_surface('1')", PyExc_TypeError);
    run_python_string("from physics import *");
    expect_python_error("terrain.get_surface(Point3D(0,0,0))", PyExc_TypeError);
    expect_python_error("terrain.get_normal()", PyExc_TypeError);
    run_python_string("terrain.get_normal(0,0)");
    run_python_string("terrain.find_mods(Point3D(0,0,0))");

    run_python_string("points = { }");
    run_python_string("points['-1x-1'] = [-1, -1, -16.8]");
    run_python_string("points['0x-1'] = [0, -1, -3.8]");
    run_python_string("points['-1x0'] = [-1, 0, -2.8]");
    run_python_string("points['-1x1'] = [-1, 1, -1.8]");
    run_python_string("points['1x-1'] = [1, -1, 15.8]");
    run_python_string("points['0x0'] = [0, 0, 12.8]");
    run_python_string("points['1x0'] = [1, 0, 23.1]");
    run_python_string("points['0x1'] = [0, 1, 14.2]");
    run_python_string("points['1x1'] = [1, 1, 19.7]");
    run_python_string("t.terrain = {'points': points}");

    run_python_string("terrain.get_surface(Point3D(0,0,0))");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("method_get_height = terrain.get_height");
    run_python_string("method_get_surface = terrain.get_surface");
    run_python_string("method_get_normal = terrain.get_normal");
    run_python_string("sabotage.null(terrain)");
    expect_python_error("method_get_height(0,0)", PyExc_AssertionError);
    expect_python_error("method_get_surface(Point3D(0,0,0))",
                        PyExc_AssertionError);
    expect_python_error("method_get_normal(0,0)", PyExc_AssertionError);
#endif // NDEBUG
   

    shutdown_python_api();
    return 0;
}
int main()
{
    init_python_api();

    setup_test_functions();

    run_python_string("from server import *");
    run_python_string("import testprop");
    run_python_string("t=Thing('1')");
    fail_python_string("t.terrain");
    run_python_string("testprop.add_properties(t)");
    run_python_string("terrain = t.terrain");
    fail_python_string("terrain.foo = 1");
    fail_python_string("terrain.get_height()");
    run_python_string("terrain.get_height(0,0)");
    fail_python_string("terrain.get_surface()");
    fail_python_string("terrain.get_surface('1')");
    run_python_string("from physics import *");
    fail_python_string("terrain.get_surface(Point3D(0,0,0))");
    fail_python_string("terrain.get_normal()");
    run_python_string("terrain.get_normal(0,0)");

    run_python_string("points = { }");
    run_python_string("points['-1x-1'] = [-1, -1, -16.8]");
    run_python_string("points['0x-1'] = [0, -1, -3.8]");
    run_python_string("points['-1x0'] = [-1, 0, -2.8]");
    run_python_string("points['-1x1'] = [-1, 1, -1.8]");
    run_python_string("points['1x-1'] = [1, -1, 15.8]");
    run_python_string("points['0x0'] = [0, 0, 12.8]");
    run_python_string("points['1x0'] = [1, 0, 23.1]");
    run_python_string("points['0x1'] = [0, 1, 14.2]");
    run_python_string("points['1x1'] = [1, 1, 19.7]");
    run_python_string("t.terrain = {'points': points}");

    run_python_string("terrain.get_surface(Point3D(0,0,0))");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("method_get_height = terrain.get_height");
    run_python_string("method_get_surface = terrain.get_surface");
    run_python_string("method_get_normal = terrain.get_normal");
    run_python_string("sabotage.null(terrain)");
    fail_python_string("terrain.foo");
    fail_python_string("terrain.foo = 1");
    fail_python_string("method_get_height(0,0)");
    fail_python_string("method_get_surface(Point3D(0,0,0))");
    fail_python_string("method_get_normal(0,0)");
#endif // NDEBUG
   

    shutdown_python_api();
    return 0;
}
Beispiel #9
0
int main()
{
    init_python_api({&CyPy_Atlas::init,
                     &CyPy_Physics::init,
                     &CyPy_Common::init});

    run_python_string("from physics import BBox");
    run_python_string("b=BBox()");
    expect_python_error("b=BBox([1])", PyExc_ValueError);
    run_python_string("b=BBox([1,1,1])");
    run_python_string("b=BBox([1.0,1.0,1.0])");
    run_python_string("b=BBox([1,1,1])");
    expect_python_error("b=BBox(['1','1','1'])", PyExc_TypeError);
    run_python_string("from atlas import ElementList");
    run_python_string("b=BBox(ElementList(1,1,1))");
    expect_python_error("b=BBox(ElementList('1', '1','1'))",
                        PyExc_TypeError);
    expect_python_error("b=BBox(1)", PyExc_TypeError);
    expect_python_error("b=BBox(1,1)", PyExc_TypeError);
    run_python_string("b=BBox(1,1,1)");
    run_python_string("b=BBox(1.0,1.0,1.0)");
    expect_python_error("b=BBox('1','1','1')", PyExc_TypeError);
    run_python_string("b=BBox(0.0,0.0,0.0,1.0,1.0,1.0)");
    run_python_string("from physics import Point3D");
    run_python_string("assert b.low_corner == Point3D(0.0,0.0,0.0)");
    run_python_string("assert b.high_corner == Point3D(1.0,1.0,1.0)");
    run_python_string("assert b.as_sequence() == [0.0,0.0,0.0,1.0,1.0,1.0]");
    run_python_string("assert b.square_bounding_radius() > 1");
    expect_python_error("b.low_corner=1", PyExc_TypeError);
    expect_python_error("b.low_corner=Point3D()", PyExc_ValueError);
    expect_python_error("b.high_corner=Point3D()", PyExc_ValueError);
    run_python_string("b.low_corner=Point3D(0,0,0)");
    run_python_string("b.high_corner=Point3D(2,2,2)");
    expect_python_error("b.other=Point3D(0,0,0)", PyExc_AttributeError);
    run_python_string("assert b.high_corner == Point3D(2,2,2)");
    run_python_string("assert b.square_bounding_radius() > 2");
    run_python_string("b2=BBox()");
    run_python_string("assert not b == b2");
    run_python_string("assert b != b2");
    run_python_string("b2.low_corner=Point3D(0,0,0)");
    run_python_string("b2.high_corner=Point3D(2,2,2)");
    run_python_string("assert b == b2");
    run_python_string("assert not b != b2");
    run_python_string("assert not b == '0'");
    

    shutdown_python_api();
    return 0;
}
Beispiel #10
0
int main()
{
    init_python_api();

    PyBBox * bbox = newPyBBox();
    assert(bbox != 0);

    run_python_string("from physics import BBox");
    run_python_string("b=BBox()");
    fail_python_string("b=BBox([1])");
    run_python_string("b=BBox([1,1,1])");
    run_python_string("b=BBox([1.0,1.0,1.0])");
    run_python_string("b=BBox([1,1,1])");
    fail_python_string("b=BBox(['1','1','1'])");
    run_python_string("from atlas import Message");
    run_python_string("b=BBox([Message(1),Message(1),Message(1)])");
    fail_python_string("b=BBox([Message('1'),Message('1'),Message('1')])");
    fail_python_string("b=BBox(1)");
    fail_python_string("b=BBox(1,1)");
    run_python_string("b=BBox(1,1,1)");
    run_python_string("b=BBox(1.0,1.0,1.0)");
    run_python_string("b=BBox(1.0,1.0,1.0,1.0,1.0,1.0)");
    fail_python_string("b=BBox('1','1','1')");
    run_python_string("print b.near_point");
    run_python_string("print b.far_point");
    run_python_string("print b.square_bounding_radius()");
    run_python_string("from physics import Point3D");
    fail_python_string("b.near_point=1");
    fail_python_string("b.near_point=Point3D()");
    fail_python_string("b.far_point=Point3D()");
    run_python_string("b.near_point=Point3D(0,0,0)");
    run_python_string("b.far_point=Point3D(1,1,1)");
    fail_python_string("b.other=Point3D(0,0,0)");
    run_python_string("print b.square_bounding_radius()");
    run_python_string("b2=BBox()");
    run_python_string("print b == b2");
    run_python_string("b2.near_point=Point3D(0,0,0)");
    run_python_string("b2.far_point=Point3D(1,1,1)");
    run_python_string("print b == b2");
    run_python_string("print b == '0'");
    

    shutdown_python_api();
    return 0;
}
Beispiel #11
0
int main()
{
    init_python_api("cb44c6cc-64fa-46c4-83d1-f43c6a2bb56c");

    Entity wrld("0", 0);
    TestWorld tw(wrld);


    run_python_string("from server import World");
    run_python_string("w=World()");
    run_python_string("w.get_time()");
    run_python_string("w.get_object('0')");
    run_python_string("w.get_object('1')");
    expect_python_error("w.get_object(1)", PyExc_TypeError);
    run_python_string("w == World()");

    shutdown_python_api();
    return 0;
}
Beispiel #12
0
int main()
{
    init_python_api();

    run_python_string("from physics import Point3D");
    run_python_string("from atlas import Message");
    run_python_string("Point3D([Message(1), Message(0), Message(0)])");
    fail_python_string("Point3D([Message('1'), Message(0), Message(0)])");
    run_python_string("p=Point3D(1,0,0)");
    run_python_string("p1=Point3D(0,1,0)");
    run_python_string("p2=Point3D(0,1,0)");
    run_python_string("print Point3D()");
    fail_python_string("print Point3D([1])");
    run_python_string("print Point3D([1,0,0])");
    run_python_string("print Point3D([1.1,0.0,0.0])");
    fail_python_string("print Point3D(['1','1','1'])");
    fail_python_string("print Point3D(1.1)");
    run_python_string("print Point3D(1.1,0.0,0.0)");
    fail_python_string("print Point3D('1','1','1')");
    fail_python_string("print Point3D(1.1,0.0,0.0,1.1)");
    run_python_string("print repr(p)");
    run_python_string("print p.mag()");
    run_python_string("print p.unit_vector_to(p1)");
    fail_python_string("print p.unit_vector_to(1.0)");
    run_python_string("print p.distance(p1)");
    fail_python_string("print p.distance(1.0)");
    run_python_string("print p.is_valid()");
    run_python_string("print p");
    run_python_string("print p == p1");
    run_python_string("print p1 == p2");
    run_python_string("from physics import Vector3D");
    run_python_string("v=Vector3D(1,0,0)");
    run_python_string("print p + v");
    fail_python_string("print p + p1");
    run_python_string("print p - v");
    run_python_string("print p - p1");
    fail_python_string("print p - 1.0");
    run_python_string("print p == v");

    shutdown_python_api();
    return 0;
}
Beispiel #13
0
int main(int argc, char ** argv)
{
    loadConfig(argc, argv);

    init_python_api("f5a8a981-e9ac-4f3b-a8f6-528add44da87");

    PyVector3D * pv = newPyVector3D();

    if (PyErr_Occurred() != 0) {
        PyErr_Print();
    }

    PyObject * pv2 = PyInstance_New((PyObject*)&PyVector3D_Type, 0, 0);

    if (PyErr_Occurred() != 0) {
        PyErr_Print();
    }

    shutdown_python_api();
}
Beispiel #14
0
int main()
{
    init_python_api();

    setup_test_functions();

    run_python_string("import atlas");
    run_python_string("import server");
    run_python_string("import physics");
    run_python_string("physics.Shape()");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("s=physics.Shape()");
    run_python_string("sabotage.null(s)");
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
int main()
{
    init_python_api();

    run_python_string("from physics import Quaternion");
    run_python_string("q=Quaternion()");
    fail_python_string("q=Quaternion([1])");
    run_python_string("q=Quaternion([0,0,0,1])");
    run_python_string("q=Quaternion([0.0,0.0,0.0,1.0])");
    fail_python_string("q=Quaternion(['0.0',0.0,0.0,1.0])");
    fail_python_string("q=Quaternion(1,0)");
    run_python_string("from physics import Vector3D");
    fail_python_string("q=Quaternion(Vector3D(1,0,0),0)");
    run_python_string("q=Quaternion(Vector3D(1,0,0),0.0)");
    run_python_string("q=Quaternion(Vector3D(1,0,0),Vector3D(0,1,0))");
    fail_python_string("q=Quaternion(1,0,0)");
    run_python_string("q=Quaternion(0,0,0,1)");
    run_python_string("q=Quaternion(0.0,0.0,0.0,1.0)");
    fail_python_string("q=Quaternion('0.0',0.0,0.0,1.0)");
    run_python_string("q=Quaternion()");
    run_python_string("q2=Quaternion()");
    run_python_string("q3=Quaternion(1,0,0,0)");
    run_python_string("print q == q2");
    run_python_string("print q == q3");
    run_python_string("print q * q2");
    fail_python_string("print q * 2");
    run_python_string("print q.is_valid()");
    run_python_string("print q.as_list()");
    fail_python_string("print q.rotation()");
    fail_python_string("print q.rotation('foo',1.0)");
    run_python_string("from physics import Vector3D");
    run_python_string("v=Vector3D(0,0,0)");
    run_python_string("print q == v");
    run_python_string("print v == q");
    run_python_string("print q.rotation(v,1.0)");
    run_python_string("print repr(q)");

    shutdown_python_api();
    return 0;
}
int main()
{
    init_python_api("b513b7b1-b0d8-4495-b3f0-54c2ef3f27f6");

    setup_test_functions();

    Entity wrld("0", 0);
    TestWorld tw(wrld);

    run_python_string("from server import *");
    run_python_string("import testprop");
    run_python_string("t=Thing('1')");
    expect_python_error("t.line", PyExc_AttributeError);
    expect_python_error("t.statistics", PyExc_AttributeError);
    expect_python_error("t.terrain", PyExc_AttributeError);
    run_python_string("testprop.add_properties(t)");
    run_python_string("t.line");
    run_python_string("t.statistics");
    run_python_string("t.terrain");


    shutdown_python_api();
    return 0;
}
Beispiel #17
0
int main(int argc, char ** argv)
{
    interactive_signals();

    if (loadConfig(argc, argv, true) < 0) {
        // Fatal error loading config file
        return EXIT_CONFIG_ERROR;
    }

    if (daemon_flag) {
        int pid = daemonise();
        if (pid == -1) {
            return EXIT_FORK_ERROR;
        } else if (pid > 0) {
            return EXIT_SUCCESS;
        }
    }

    // If we are a daemon logging to syslog, we need to set it up.
    initLogger();

    // Initialise the persistance subsystem. If we have been built with
    // database support, this will open the various databases used to
    // store server data.
    if (database_flag) {
        Persistence * p = Persistence::instance();
        int dbstatus = p->init();
        if (dbstatus < 0) {
            database_flag = false;
            log(ERROR, "Error opening database. Database disabled.");
            if (dbstatus == DATABASE_TABERR) {
                log(INFO, "Database connection established, "
                          "but unable to create required tables.");
                log(INFO, "Please ensure that any obsolete database "
                          "tables have been removed.");
            } else {
                log(INFO, "Unable to connect to the RDBMS.");
                log(INFO, "Please ensure that the RDBMS is running, "
                          "the cyphesis database exists and is accessible "
                          "to the user running cyphesis.");
            }
            log(INFO, String::compose("To disable this message please run:\n\n"
                                      "    cyconfig --%1:usedatabase=false\n\n"
                                      "to permanently disable database usage.",
                                      instance));
        }
    }

    // If the restricted flag is set in the config file, then we
    // don't allow connecting users to create accounts. Accounts must
    // be created manually by the server administrator.
    if (readConfigItem("cyphesis","restricted", restricted_flag) == 0) {
        if (restricted_flag) {
            log(INFO, "Setting restricted mode.");
        }
    }

    readConfigItem("cyphesis","inittime", timeoffset);

    std::string mserver("metaserver.worldforge.org");
    readConfigItem("cyphesis", "metaserver", mserver);

    std::string serverName;
    if (readConfigItem("cyphesis","servername", serverName) != 0) {
        serverName = get_hostname();
    }

    std::string serverHostname("localhost");
    readConfigItem("slave","server", serverHostname);
    
    // Start up the python subsystem.
    init_python_api();

    { // scope for CommServer

    // Create commserver instance that will handle connections from clients.
    // The commserver will create the other server related objects, and the
    // world object pair (World + WorldRouter), and initialise the admin
    // account. The primary ruleset name is passed in so it
    // can be stored and queried by clients.
    WorldRouter world;

    // This ID is currently generated every time, but should perhaps be
    // persistent in future.
    std::string server_id, lobby_id;
    long int_id, lobby_int_id;

    if (((int_id = newId(server_id)) < 0) ||
        ((lobby_int_id = newId(lobby_id)) < 0)) {
        log(CRITICAL, "Unable to get server IDs from Database");
        return EXIT_DATABASE_ERROR;
    }

    ServerRouting server(world, ruleset, serverName,
                         server_id, int_id,
                         lobby_id, lobby_int_id);

    CommServer commServer(server);

    // This is where we should restore the database, before
    // the listen sockets are open. Unlike earlier code, we are
    // attempting to construct the internal state from the database,
    // not creating a new world using the contents of the database as a
    // template

    CommUnixListener * listener = new CommUnixListener(commServer,
          *new CommClientFactory<SlaveClientConnection>());
    if (listener->setup(slave_socket_name) != 0) {
        log(ERROR, "Could not create listen socket. Init failed.");
        return EXIT_SOCKET_ERROR;
    }
    commServer.addSocket(listener);

    std::string master_id;
    if (newId(master_id) < 0) {
        log(CRITICAL, "Unable to get master ID from Database");
        return EXIT_DATABASE_ERROR;
    }

    CommMaster * master = new CommMaster(commServer);
    if (master->connect(serverHostname) != 0) {
        log(ERROR, "Could not connect to master. Init failed.");
        return EXIT_SOCKET_ERROR;
    }
    master->setup(new Master(*master, commServer.m_server, master_id));
    commServer.addSocket(master);

    log(INFO, "Running");

    // Inform things that want to know that we are running.
    running();

    // Loop until the exit flag is set. The exit flag can be set anywhere in
    // the code easily.
    while (!exit_flag) {
        try {
            commServer.poll();
        }
        catch (...) {
            // It is hoped that commonly thrown exception, particularly
            // exceptions that can be caused  by external influences
            // should be caught close to where they are thrown. If
            // an exception makes it here then it should be debugged.
            log(ERROR, "Exception caught in main()");
        }
    }
    // exit flag has been set so we close down the databases, and indicate
    // to the metaserver (if we are using one) that this server is going down.
    // It is assumed that any preparation for the shutdown that is required
    // by the game has been done before exit flag was set.
    log(NOTICE, "Performing clean shutdown...");

    } // close scope of CommServer, which cause the destruction of the
      // server and world objects, and the entire world contents

    Persistence::instance()->shutdown();

    EntityBuilder::instance()->flushFactories();
    EntityBuilder::del();
    MindFactory::del();

    Inheritance::clear();

    // Shutdown the python interpretter. This frees lots of memory, and if
    // the malloc heap is in any way corrupt, a segfault is likely to
    // occur at this point. Previous occassions where pointers have been
    // deleted twice elsewhere in the code, have resulted in a segfault
    // at this point. AlRiddoch 10th November 2001
    shutdown_python_api();

    delete global_conf;

    log(INFO, "Clean shutdown complete.");
    return 0;
}
Beispiel #18
0
int main()
{
    init_python_api("16799987-a321-43a2-aa66-f7bc8ed8e9b2");

    setup_test_functions();

    run_python_string("from atlas import Message");
    run_python_string("from atlas import Operation");
    run_python_string("from atlas import Oplist");
    run_python_string("from atlas import Location");
    run_python_string("from physics import Vector3D");
    run_python_string("Message()");
    run_python_string("Message(1)");
    run_python_string("Message(1.1)");
    run_python_string("Message('1')");
    run_python_string("Message([1, 1])");
    run_python_string("Message((1, 1))");
    run_python_string("Message({'foo': 1})");
    run_python_string("Message(Message(1))");
    run_python_string("Message(Operation('get'))");
    run_python_string("Message(Oplist(Operation('get')))");
    run_python_string("Message(Location())");
    expect_python_error("Message(Vector3D())", PyExc_TypeError);
    run_python_string("Message([Message(1)])");
    expect_python_error("Message([Vector3D()])", PyExc_TypeError);
    run_python_string("Message({'foo': Message(1)})");
    expect_python_error("Message({'foo': Vector3D()})", PyExc_TypeError);
    expect_python_error("Message(1, 1)", PyExc_TypeError);

    run_python_string("m=Message(1)");
    run_python_string("print m.get_name()");
    expect_python_error("print m.foo", PyExc_AttributeError);
    expect_python_error("m.foo = 1", PyExc_AttributeError);
    run_python_string("m=Message({})");
    expect_python_error("print m.foo", PyExc_AttributeError);
    expect_python_error("m.foo = Vector3D()", PyExc_TypeError);
    run_python_string("m.foo = 1");
    run_python_string("print m.foo");
    run_python_string("m.foo = 1.1");
    run_python_string("print m.foo");
    run_python_string("m.foo = '1'");
    run_python_string("print m.foo");
    run_python_string("m.foo = ['1']");
    run_python_string("print m.foo");
    run_python_string("m.foo = {'foo': 1}");
    run_python_string("print m.foo");
    run_python_string("m=Message(1)");
    run_python_string("assert m == 1");
    run_python_string("assert not m == 1.0");
    run_python_string("assert not m == '1'");
    run_python_string("assert not m != 1");
    run_python_string("assert m != 1.0");
    run_python_string("assert m != '1'");
    run_python_string("assert m != 2");
    run_python_string("m=Message(1.0)");
    run_python_string("assert not m == 1");
    run_python_string("assert m == 1.0");
    run_python_string("assert not m == '1'");
    run_python_string("assert m != 1");
    run_python_string("assert not m != 1.0");
    run_python_string("assert m != '1'");
    run_python_string("assert m != 2.0");
    run_python_string("m=Message('1')");
    run_python_string("assert not m == 1");
    run_python_string("assert not m == 1.0");
    run_python_string("assert m == '1'");
    run_python_string("assert m != 1");
    run_python_string("assert m != 1.0");
    run_python_string("assert not m != '1'");
    run_python_string("assert m != '2'");
    run_python_string("m=Message([])");
    run_python_string("assert not m == 1");
    run_python_string("assert not m == 1.0");
    run_python_string("assert not m == '1'");
    run_python_string("assert m != 1");
    run_python_string("assert m != 1.0");
    run_python_string("assert m != '1'");
    run_python_string("m=Message({})");
    run_python_string("assert not m == 1");
    run_python_string("assert not m == 1.0");
    run_python_string("assert not m == '1'");
    run_python_string("assert m != 1");
    run_python_string("assert m != 1.0");
    run_python_string("assert m != '1'");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    run_python_string("get_name_method=m.get_name");
    run_python_string("sabotage.null(m)");
    // Hit the assert checks.
    expect_python_error("print get_name_method()", PyExc_AssertionError);
    expect_python_error("print m.foo", PyExc_AssertionError);
    expect_python_error("m.foo = 1", PyExc_AssertionError);
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
Beispiel #19
0
int main()
{
    init_python_api("3f71a0b3-8b4c-4efc-9835-e32928c049c3");

    setup_test_functions();

    run_python_string("import atlas");
    run_python_string("import server");
    run_python_string("import physics");
    run_python_string("physics.Shape()");
    expect_python_error("physics.Shape(object(), object())", PyExc_TypeError);
    run_python_string("s = physics.Shape({'type': 'polygon',"
                                         "'points': [[ 0.0, 0.0 ],"
                                                    "[ 1.0, 0.0 ],"
                                                    "[ 1.0, 1.0 ]] })");
    expect_python_error("physics.Shape({})", PyExc_TypeError);
    expect_python_error("physics.Shape(object())", PyExc_TypeError);
    expect_python_error("physics.Shape({'type': 'polygon',"
                                       "'data': object(),"
                                       "'points': [[ 0.0, 0.0 ],"
                                                  "[ 1.0, 0.0 ],"
                                                  "[ 1.0, 1.0 ]] })",
                        PyExc_TypeError);
    run_python_string("physics.Shape(atlas.Message({'type': 'polygon',"
                                                   "'points': [[ 0.0, 0.0 ],"
                                                              "[ 1.0, 0.0 ],"
                                                              "[ 1.0, 1.0 ]] }))");
    expect_python_error("physics.Shape(atlas.Message('foo'))", PyExc_TypeError);
    expect_python_error("physics.Shape(atlas.Message({'type': 'polygon',"
                                                     "'points': [[ 0.0, 0.0 ],"
                                                                "[ 1.0 ],"
                                                                "[ 1.0, 1.0 ]] }))",
                        PyExc_TypeError);
    run_python_string("s.area()");
    run_python_string("s.footprint()");
    run_python_string("s.as_data()");
    run_python_string("repr(s)");
    run_python_string("assert(len(s) == 3)");
    run_python_string("s *= 5.0");
    expect_python_error("s *= 5", PyExc_TypeError);
    // Can't send attributes yet
    expect_python_error("s.points =  [[ 0.0, 0.0 ],"
                                     "[ 1.0, 0.0 ],"
                                     "[ 1.0, 1.0 ]]", PyExc_AttributeError);

    //////////////////////////////// Box /////////////////////////////////
    run_python_string("b = physics.Box()");
    expect_python_error("physics.Box([[ 0.0, 0.0 ],"
                                     "[ 1.0, 0.0 ],"
                                     "[ 1.0, 1.0 ]])", PyExc_TypeError);
    run_python_string("b.area()");
    run_python_string("b.centre()");
    run_python_string("b.footprint()");
    run_python_string("b.low_corner()");
    run_python_string("b.high_corner()");
    run_python_string("b.extrude(0, 1)");
    expect_python_error("b.extrude()", PyExc_TypeError);
    run_python_string("d = b.as_data()");
    run_python_string("repr(b)");
    run_python_string("assert(len(b) == 4)");
    run_python_string("b[0]");
    run_python_string("b[1]");
    run_python_string("b[2]");
    run_python_string("b[3]");
    expect_python_error("b[4]", PyExc_IndexError);
    run_python_string("b *= 5.0");

    /////////////////////////////// Course ///////////////////////////////
    run_python_string("c = physics.Course()");
    run_python_string("c.area()");
    run_python_string("c.centre()");
    run_python_string("c.footprint()");
    run_python_string("c.low_corner()");
    run_python_string("c.high_corner()");
    run_python_string("c.as_data()");
    run_python_string("repr(c)");
    run_python_string("assert(len(c) == 0)");
    expect_python_error("c[0]", PyExc_IndexError);
    run_python_string("c *= 5.0");

    //////////////////////////////// Line ////////////////////////////////
    expect_python_error("physics.Line()", PyExc_TypeError);
    run_python_string("l = physics.Line([[ 0.0, 0.0 ],"
                                        "[ 1.0, 0.0 ],"
                                        "[ 1.0, 1.0 ]])");
    run_python_string("l.area()");
    run_python_string("l.centre()");
    run_python_string("l.footprint()");
    run_python_string("l.low_corner()");
    run_python_string("l.high_corner()");
    run_python_string("d = l.as_data()");
    run_python_string("repr(l)");
    run_python_string("len(l)");
    run_python_string("l *= 5.0");

    ////////////////////////////// Polygon ///////////////////////////////
    expect_python_error("physics.Polygon()", PyExc_TypeError);
    expect_python_error("physics.Polygon(object())", PyExc_TypeError);
    expect_python_error("physics.Polygon([[ object(), object() ],"
                                         "[ 1.0, 0.0 ],"
                                         "[ 1.0, 1.0 ]])", PyExc_TypeError);
    // One vector too short
    expect_python_error("physics.Polygon([[ 0.0, 0.0 ],"
                                         "[ 1.0 ],"
                                         "[ 1.0, 1.0 ]])", PyExc_TypeError);
    // Sequence too short for complete polygon
    expect_python_error("physics.Polygon([[ 0.0, 0.0 ],"
                                         "[ 1.0, 1.0 ]])", PyExc_TypeError);
    run_python_string("physics.Polygon(atlas.Message([[ 0.0, 0.0 ],"
                                                     "[ 1.0, 0.0 ],"
                                                     "[ 1.0, 1.0 ]]))");
    expect_python_error("physics.Polygon(atlas.Message('foo'))", PyExc_TypeError);
    run_python_string("p = physics.Polygon([[ 0.0, 0.0 ],"
                                           "[ 1.0, 0.0 ],"
                                           "[ 1.0, 1.0 ]])");
    run_python_string("p.area()");
    run_python_string("p.centre()");
    run_python_string("p.footprint()");
    run_python_string("p.low_corner()");
    run_python_string("p.high_corner()");
    run_python_string("d = p.as_data()");
    run_python_string("repr(p)");
    run_python_string("len(p)");
    run_python_string("p *= 5.0");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("s=physics.Shape()");
    run_python_string("sabotage.null(s)");
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
Beispiel #20
0
int main()
{
    init_python_api("93b8eac3-9ab9-40f7-b419-d740c18c09e4");

    setup_test_functions();

    PyMap * map = newPyMap();
    assert(map != 0);

    run_python_string("from server import Map");
    run_python_string("from atlas import Location");
    run_python_string("from atlas import Entity");
    run_python_string("from atlas import Message");
    run_python_string("m=Map()");
    expect_python_error("m.find_by_location()", PyExc_TypeError);
    run_python_string("l=Location()");
    expect_python_error("m.find_by_location(l)", PyExc_TypeError);
    expect_python_error("m.find_by_location(l, 5.0, 'foo')",
                        PyExc_RuntimeError);
    expect_python_error("m.find_by_location(5, 5.0, 'foo')", PyExc_TypeError);
    expect_python_error("m.find_by_type()", PyExc_TypeError);
    expect_python_error("m.find_by_type(1)", PyExc_TypeError);
    run_python_string("m.find_by_type('foo')");
    expect_python_error("m.add()", PyExc_TypeError);
    expect_python_error("m.add('2')", PyExc_TypeError);
    expect_python_error("m.add('2', 1.2)", PyExc_TypeError);
    expect_python_error("m.add(Message())", PyExc_TypeError);
    expect_python_error("m.add(Message(), 1.2)", PyExc_TypeError);
    expect_python_error("m.add(Message({'objtype': 'op', 'parents': ['get']}), 1.2)",
                        PyExc_TypeError);
    expect_python_error("m.add(Message({}), 1.2)", PyExc_TypeError);
    expect_python_error("m.add(Message({'parents': 'get'}), 1.2)",
                        PyExc_TypeError);
    run_python_string("m.add(Message({'id': '2'}), 1.2)");
    run_python_string("m.add(Message({'id': '2'}), 1.2)");
    expect_python_error("m.add(Entity())", PyExc_TypeError);
    expect_python_error("m.add(Entity('1', type='oak'))", PyExc_TypeError);
    run_python_string("m.add(Entity('1', type='thing'), 1.1)");
    run_python_string("m.find_by_type('thing')");
    expect_python_error("m.get()", PyExc_TypeError);
    expect_python_error("m.get(1)", PyExc_TypeError);
    run_python_string("m.get('1')");
    run_python_string("m.get('23')");
    expect_python_error("m.get_add()", PyExc_TypeError);
    expect_python_error("m.get_add(3)", PyExc_TypeError);
    run_python_string("m.get_add('3')");
    run_python_string("m.update(Entity('3', type='thing'), 1.1)");
    expect_python_error("m.update()", PyExc_TypeError);
    expect_python_error("m.delete()", PyExc_TypeError);
    expect_python_error("m.delete(1)", PyExc_TypeError);
    run_python_string("m.delete('1')");
    expect_python_error("m.add_hooks_append()", PyExc_TypeError);
    expect_python_error("m.add_hooks_append(1)", PyExc_TypeError);
    run_python_string("m.add_hooks_append('add_map')");
    expect_python_error("m.update_hooks_append()", PyExc_TypeError);
    expect_python_error("m.update_hooks_append(1)", PyExc_TypeError);
    run_python_string("m.update_hooks_append('update_map')");
    expect_python_error("m.delete_hooks_append()", PyExc_TypeError);
    expect_python_error("m.delete_hooks_append(1)", PyExc_TypeError);
    run_python_string("m.delete_hooks_append('delete_map')");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    run_python_string("sabotage.null(m)");
    // Hit the assert checks.
    expect_python_error("m.find_by_location(l, 5.0, 'foo')",
                        PyExc_AssertionError);
    expect_python_error("m.find_by_type('foo')", PyExc_AssertionError);
    expect_python_error("m.add(Entity('1', type='thing'), 1.1)",
                        PyExc_AssertionError);
    expect_python_error("m.delete('1')", PyExc_AssertionError);
    expect_python_error("m.get('1')", PyExc_AssertionError);
    expect_python_error("m.get_add('3')", PyExc_AssertionError);
    expect_python_error("m.add_hooks_append('add_map')",
                        PyExc_AssertionError);
    expect_python_error("m.update_hooks_append('update_map')",
                        PyExc_AssertionError);
    expect_python_error("m.delete_hooks_append('delete_map')",
                        PyExc_AssertionError);
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
Beispiel #21
0
int main(int argc, char ** argv)
{
    if (security_init() != 0) {
        log(CRITICAL, "Security initialisation Error. Exiting.");
        return EXIT_SECURITY_ERROR;
    }

    if (security_check() != SECURITY_OKAY) {
        log(CRITICAL, "Security check error. Exiting.");
        return EXIT_SECURITY_ERROR;
    }

    interactive_signals();

    int config_status = loadConfig(argc, argv, USAGE_SERVER);
    if (config_status < 0) {
        if (config_status == CONFIG_VERSION) {
            std::cout << argv[0] << " (cyphesis) " << consts::version
                      << " (Cyphesis build " << consts::buildId << ")"
                      << std::endl << std::flush;

            return 0;
        } else if (config_status == CONFIG_HELP) {
            showUsage(argv[0], USAGE_SERVER);
            return 0;
        } else if (config_status != CONFIG_ERROR) {
            log(ERROR, "Unknown error reading configuration.");
        }
        // Fatal error loading config file.
        return EXIT_CONFIG_ERROR;
    }

    if (daemon_flag) {
        int pid = daemonise();
        if (pid == -1) {
            return EXIT_FORK_ERROR;
        } else if (pid > 0) {
            return EXIT_SUCCESS;
        }
    }

    readConfigItem(instance, "usedatabase", database_flag);

    // If we are a daemon logging to syslog, we need to set it up.
    initLogger();

    // Initialise the persistance subsystem. If we have been built with
    // database support, this will open the various databases used to
    // store server data.
    if (database_flag) {
        Persistence * p = Persistence::instance();
        int dbstatus = p->init();
        if (dbstatus < 0) {
            database_flag = false;
            log(ERROR, "Error opening database. Database disabled.");
            if (dbstatus == DATABASE_TABERR) {
                log(INFO, "Database connection established, "
                          "but unable to create required tables.");
                log(INFO, "Please ensure that any obsolete database "
                          "tables have been removed.");
            } else {
                log(INFO, "Unable to connect to the RDBMS.");
                log(INFO, "Please ensure that the RDBMS is running, "
                          "the cyphesis database exists and is accessible "
                          "to the user running cyphesis.");
            }
            log(INFO, String::compose("To disable this message please run:\n\n"
                                      "    cyconfig --%1:usedatabase=false\n\n"
                                      "to permanently disable database usage.",
                                      instance));
        }
    }

    // If the restricted flag is set in the config file, then we
    // don't allow connecting users to create accounts. Accounts must
    // be created manually by the server administrator.
    if (restricted_flag) {
        log(INFO, "Setting restricted mode.");
    }

    readConfigItem(instance, "inittime", timeoffset);

    std::string server_name;
    if (readConfigItem(instance, "servername", server_name) != 0) {
        if (instance == CYPHESIS) {
            server_name = get_hostname();
        } else {
            server_name = instance;
        }
    }

    int nice = 1;
    readConfigItem(instance, "nice", nice);
    
    // Start up the python subsystem.
    init_python_api();

    { // scope for CommServer

    // Create commserver instance that will handle connections from clients.
    // The commserver will create the other server related objects, and the
    // world object pair (World + WorldRouter), and initialise the admin
    // account. The primary ruleset name is passed in so it
    // can be stored and queried by clients.
    Inheritance::instance();

    WorldRouter world;

    Ruleset::init();

    TeleportAuthenticator::init();

    StorageManager store(world);

    // This ID is currently generated every time, but should perhaps be
    // persistent in future.
    std::string server_id, lobby_id;
    long int_id, lobby_int_id;

    if (((int_id = newId(server_id)) < 0) ||
        ((lobby_int_id = newId(lobby_id)) < 0)) {
        log(CRITICAL, "Unable to get server IDs from Database");
        return EXIT_DATABASE_ERROR;
    }

    ServerRouting server(world, ruleset, server_name,
                         server_id, int_id,
                         lobby_id, lobby_int_id);

    CommServer commServer(server);

    if (commServer.setup() != 0) {
        log(CRITICAL, "Internal error setting up server infrastructure");
        return EXIT_SOCKET_ERROR;
    }

    // This is where we should restore the database, before
    // the listen sockets are open. Unlike earlier code, we are
    // attempting to construct the internal state from the database,
    // not creating a new world using the contents of the database as a
    // template

    if (database_flag) {
        // log(INFO, _("Restoring world from database..."));

        store.restoreWorld();
        // FIXME Do the following steps.
        // Read the world entity if any from the database, or set it up.
        // If it was there, make sure it did not get any of the wrong
        // position or orientation data.
        store.initWorld();

        // log(INFO, _("Restored world."));

        CommPSQLSocket * dbsocket = new CommPSQLSocket(commServer,
                                        Persistence::instance()->m_connection);
        commServer.addSocket(dbsocket);
        commServer.addIdle(dbsocket);

        IdleConnector * storage_idle = new IdleConnector(commServer);
        storage_idle->idling.connect(sigc::mem_fun(&store, &StorageManager::tick));
        commServer.addIdle(storage_idle);
    } else {
        std::string adminId;
        long intId = newId(adminId);
        assert(intId >= 0);

        Admin * admin = new Admin(0, "admin", "BAD_HASH", adminId, intId);
        server.addAccount(admin);
    }

    // Add the test object, and call it regularly so it can do what it does.
    // UpdateTester * update_tester = new UpdateTester(commServer);
    // commServer.addIdle(update_tester);

    CommTCPListener * listener = new CommTCPListener(commServer,
          *new CommClientFactory<Connection>());
    if (client_port_num < 0) {
        client_port_num = dynamic_port_start;
        for (; client_port_num <= dynamic_port_end; client_port_num++) {
            if (listener->setup(client_port_num) == 0) {
                break;
            }
        }
        if (client_port_num > dynamic_port_end) {
            log(ERROR, String::compose("Could not find free client listen "
                                       "socket in range %1-%2. Init failed.",
                                       dynamic_port_start, dynamic_port_end));
            log(INFO, String::compose("To allocate 8 more ports please run:"
                                      "\n\n    cyconfig "
                                      "--cyphesis:dynamic_port_end=%1\n\n",
                                      dynamic_port_end + 8));
            return EXIT_PORT_ERROR;
        }
        log(INFO, String::compose("Auto configuring new instance \"%1\" "
                                  "to use port %2.",
                                  instance, client_port_num));
        global_conf->setItem(instance, "tcpport", client_port_num,
                             varconf::USER);
        global_conf->setItem(CYPHESIS, "dynamic_port_start",
                             client_port_num + 1, varconf::USER);
    } else {
        if (listener->setup(client_port_num) != 0) {
            log(ERROR, String::compose("Could not create client listen socket "
                                       "on port %1. Init failed.",
                                       client_port_num));
            return EXIT_SOCKET_ERROR;
        }
    }
    commServer.addSocket(listener);

#ifdef HAVE_SYS_UN_H
    CommUnixListener * localListener = new CommUnixListener(commServer,
          *new CommClientFactory<TrustedConnection>());
    if (localListener->setup(client_socket_name) != 0) {
        log(ERROR, String::compose("Could not create local listen socket "
                                   "with address \"%1\"",
                                   localListener->getPath()));
        delete localListener;
    } else {
        commServer.addSocket(localListener);
    }

    CommUnixListener * pythonListener = new CommUnixListener(commServer,
          *new CommPythonClientFactory());
    if (pythonListener->setup(python_socket_name) != 0) {
        log(ERROR, String::compose("Could not create python listen socket "
                                   "with address %1.",
                                   pythonListener->getPath()));
        delete pythonListener;
    } else {
        commServer.addSocket(pythonListener);
    }
#endif

    CommTCPListener * httpListener = new CommTCPListener(commServer,
          *new CommHttpClientFactory());
    if (httpListener->setup(http_port_num) != 0) {
        log(ERROR, String::compose("Could not create http listen socket on "
                                   "port %1.", http_port_num));
        delete httpListener;
    } else {
        commServer.addSocket(httpListener);
    }

    if (useMetaserver) {
        CommMetaClient * cmc = new CommMetaClient(commServer);
        if (cmc->setup(mserver) == 0) {
            commServer.addIdle(cmc);
        } else {
            log(ERROR, "Error creating metaserver comm channel.");
            delete cmc;
        }
    }

#if defined(HAVE_LIBHOWL) || defined(HAVE_AVAHI)

    CommMDNSPublisher * cmdns = new CommMDNSPublisher(commServer);
    if (cmdns->setup() == 0) {
        commServer.addSocket(cmdns);
        commServer.addIdle(cmdns);
    } else {
        log(ERROR, "Unable to register service with MDNS daemon.");
        delete cmdns;
    }

#endif // defined(HAVE_LIBHOWL) || defined(HAVE_AVAHI)

    // Configuration is now complete, and verified as somewhat sane, so
    // we save the updated user config.

    updateUserConfiguration();

    log(INFO, "Running");
    logEvent(START, "- - - Standalone server startup");

    // Inform things that want to know that we are running.
    running();

    // Reduce our system priority to make it easier to debug a runaway
    // server.
    if (nice != 0) {
        reduce_priority(nice);
    }

    // Loop until the exit flag is set. The exit flag can be set anywhere in
    // the code easily.
    while (!exit_flag) {
        try {
            commServer.poll();
        }
        catch (...) {
            // It is hoped that commonly thrown exception, particularly
            // exceptions that can be caused  by external influences
            // should be caught close to where they are thrown. If
            // an exception makes it here then it should be debugged.
            log(ERROR, "Exception caught in main()");
        }
    }
    // exit flag has been set so we close down the databases, and indicate
    // to the metaserver (if we are using one) that this server is going down.
    // It is assumed that any preparation for the shutdown that is required
    // by the game has been done before exit flag was set.
    log(NOTICE, "Performing clean shutdown...");

    } // close scope of CommServer, WorldRouter, and ServerRouting, which
      // cause the destruction of the server and world objects, and the entire
      // world contents

    Persistence::instance()->shutdown();

    EntityBuilder::instance()->flushFactories();
    EntityBuilder::del();
    ArithmeticBuilder::del();
    MindFactory::del();
    TeleportAuthenticator::del();

    Inheritance::clear();

    // Shutdown the python interpretter. This frees lots of memory, and if
    // the malloc heap is in any way corrupt, a segfault is likely to
    // occur at this point. Previous occassions where pointers have been
    // deleted twice elsewhere in the code, have resulted in a segfault
    // at this point. AlRiddoch 10th November 2001
    shutdown_python_api();

    delete global_conf;

    log(INFO, "Clean shutdown complete.");
    logEvent(STOP, "- - - Standalone server shutdown");
    return 0;
}
int main()
{
    init_python_api();

    Py_InitModule("testmod", no_methods);

    run_python_string("import server");
    run_python_string("import testmod");
    run_python_string("from atlas import Operation");
    run_python_string("class TestEntity(server.Thing):\n"
                      " def look_operation(self, op): pass\n"
                      " def delete_operation(self, op):\n"
                      "  raise AssertionError, 'deliberate'\n"
                      " def tick_operation(self, op):\n"
                      "  raise AssertionError, 'deliberate'\n"
                      " def talk_operation(self, op):\n"
                      "  return 'invalid result'\n"
                      " def set_operation(self, op):\n"
                      "  return Operation('sight')\n"
                      " def move_operation(self, op):\n"
                      "  return Operation('sight') + Operation('move')\n"
                      " def test_hook(self, ent): pass\n");
    run_python_string("testmod.TestEntity=TestEntity");

    // PyObject * package_name = PyString_FromString("testmod");
    // PyObject * testmod = PyImport_Import(package_name);
    // Py_DECREF(package_name);
    // assert(testmod);

    PythonScriptFactory psf("testmod", "TestEntity");
    Entity * e = new Entity("1", 1);
    int ret = psf.addScript(e);
    assert(ret == 0);

    OpVector res;
    Atlas::Objects::Operation::Look op1;
    e->operation(op1, res);

    Atlas::Objects::Operation::Create op2;
    e->operation(op2, res);

    Atlas::Objects::Operation::Delete op3;
    e->operation(op3, res);

    Atlas::Objects::Operation::Talk op4;
    e->operation(op4, res);

    Atlas::Objects::Operation::Set op5;
    e->operation(op5, res);

    Atlas::Objects::Operation::Move op6;
    e->operation(op6, res);

    Atlas::Objects::Operation::Tick op7;
    e->operation(op7, res);

    Script * script = e->script();
    assert(script != 0);
    assert(script != &noScript);

    script->hook("nohookfunction", e);
    script->hook("test_hook", e);

    delete e;

    shutdown_python_api();
    return 0;
}
Beispiel #23
0
int main()
{
    init_python_api();

    setup_test_functions();

    PyTask * task = newPyTask();
    assert(task != 0);

    run_python_string("from server import Task");
    fail_python_string("Task()");
    fail_python_string("Task(1)");
    fail_python_string("Task('1')");
    run_python_string("from server import Character");
    run_python_string("c=Character('1')");
    run_python_string("t=Task(c)");
    run_python_string("Task(t)");
    run_python_string("t==Task(c)");
    run_python_string("print t.character");
    run_python_string("print t.progress");
    run_python_string("print t.rate");
    fail_python_string("print t.foo");
    run_python_string("t.progress = 0");
    run_python_string("t.progress = 0.5");
    fail_python_string("t.progress = '1'");
    run_python_string("t.rate = 0");
    run_python_string("t.rate = 0.5");
    fail_python_string("t.rate = '1'");
    run_python_string("t.foo = 1");
    run_python_string("t.foo = 1.1");
    run_python_string("t.foo = 'foois1'");
    run_python_string("print t.foo");
    run_python_string("print t.obsolete()");
    run_python_string("print t.count()");
    run_python_string("print t.new_tick()");
    run_python_string("print t.next_tick(1)");
    run_python_string("print t.next_tick(1.1)");
    fail_python_string("print t.next_tick('1')");
    run_python_string("t.irrelevant()");
    run_python_string("print t.obsolete()");

#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");
    // Hit the assert checks.
    run_python_string("irrelevant_methd=t.irrelevant");
    run_python_string("obsolete_methd=t.obsolete");
    run_python_string("count_methd=t.count");
    run_python_string("new_tick_methd=t.new_tick");
    run_python_string("next_tick_methd=t.next_tick");

    run_python_string("sabotage.null(t)");

    fail_python_string("irrelevant_methd()");
    fail_python_string("obsolete_methd()");
    fail_python_string("count_methd()");
    fail_python_string("new_tick_methd()");
    fail_python_string("next_tick_methd(1.1)");

    fail_python_string("t.progress");
    fail_python_string("t.progress = 0");
    fail_python_string("t==Task(c)");
    fail_python_string("Task(t)");

    run_python_string("c2=Character('2')");
    run_python_string("sabotage.null(c2)");
    fail_python_string("t=Task(c2)");
#endif // NDEBUG

    shutdown_python_api();
    return 0;
}
Beispiel #24
0
int main()
{
    init_python_api("3622159a-de3c-42e6-858c-f6bd7cf8e7b1");

    setup_test_functions();

    PyOperation * op = newPyOperation();
    assert(op != 0);
    op = newPyConstOperation();
    assert(op != 0);

    run_python_string("from atlas import *");
    run_python_string("o=Operation('get')");
    // This should fail, but the error throwing has been disabled to
    // allow cooler stuff to be done from the client.
    // FIXME Once the client is constrained to real operations, go back
    // to disallowing arbitrary operation names.
    // expect_python_error("o=Operation('not_valid')");
    run_python_string("o=Operation('not_valid')");
    run_python_string("o=Operation('get', to='1', from_='1')");
    expect_python_error("o=Operation('get', from_=Message({'nonid': '1'}))",
                        PyExc_TypeError);
    expect_python_error("o=Operation('get', from_=Message({'id': 1}))",
                        PyExc_TypeError);
    expect_python_error("o=Operation('get', to=Message({'nonid': '1'}))",
                        PyExc_TypeError);
    expect_python_error("o=Operation('get', to=Message({'id': 1}))",
                        PyExc_TypeError);
    run_python_string("e=Entity('1')");
    run_python_string("o=Operation('get', to=e, from_=e)");
    run_python_string("o=Operation('get', Entity(), to='1', from_='1')");
    run_python_string("o=Operation('get', Operation('set'), to='1', from_='1')");
    expect_python_error("o=Operation('get', Location(), to='1', from_='1')",
                        PyExc_TypeError);
    run_python_string("o=Operation('get', Entity(), Entity(), Entity(), to='1', from_='1')");
    expect_python_error("Operation('get', Message('1'))", PyExc_TypeError);
    run_python_string("Operation('get', Message({'objtype': 'obj', 'parents': ['thing']}))");
    expect_python_error("o=Operation()", PyExc_TypeError);
    expect_python_error("o=Operation(1)", PyExc_TypeError);
    run_python_string("o=Operation('get')");
    run_python_string("o.setSerialno(1)");
    expect_python_error("o.setSerialno('1')", PyExc_TypeError);
    run_python_string("o.setRefno(1)");
    expect_python_error("o.setRefno('1')", PyExc_TypeError);
    run_python_string("o.setTo('1')");
    expect_python_error("o.setTo(1)", PyExc_TypeError);
    run_python_string("o.setFrom('2')");
    expect_python_error("o.setFrom(2)", PyExc_TypeError);
    run_python_string("o.setSeconds(2)");
    run_python_string("o.setSeconds(2.0)");
    expect_python_error("o.setSeconds('2.0')", PyExc_TypeError);
    run_python_string("o.setFutureSeconds(2)");
    run_python_string("o.setFutureSeconds(2.0)");
    expect_python_error("o.setFutureSeconds('2.0')", PyExc_TypeError);
    expect_python_error("o.setArgs()", PyExc_TypeError);
    run_python_string("o.setArgs([])");
    expect_python_error("o.setArgs(1)", PyExc_TypeError);
    expect_python_error("o.setArgs([1])", PyExc_TypeError);
    run_python_string("o.setArgs([Operation('get')])");
    run_python_string("o.setArgs([Entity(parents=[\"oak\"])])");
    run_python_string("o.setArgs([Message({'parents': ['root']})])");
    expect_python_error("o.setArgs([Message('1')])", PyExc_TypeError);
    run_python_string("import types");
    run_python_string("assert type(o.getSerialno()) == types.IntType");
    run_python_string("assert type(o.getRefno()) == types.IntType");
    run_python_string("assert type(o.getTo()) == types.StringType");
    run_python_string("assert type(o.getFrom()) == types.StringType");
    run_python_string("assert type(o.getSeconds()) == types.FloatType");
    run_python_string("assert type(o.getFutureSeconds()) == types.FloatType");
    run_python_string("assert type(o.getArgs()) == types.ListType");
    run_python_string("assert type(o.get_name()) == types.StringType");
    run_python_string("assert len(o) == 1");
    run_python_string("o.setArgs([Operation('get'), Entity(parents=[\"oak\"]), Message({'parents': ['root'], 'objtype': 'obj'})])");
    run_python_string("assert type(o[0]) == Operation");
    run_python_string("assert type(o[1]) == Entity");
    run_python_string("assert type(o[2]) == Message");
    expect_python_error("o[3]", PyExc_IndexError);
    run_python_string("assert o + None == o");
    expect_python_error("o + 1", PyExc_TypeError);
    run_python_string("assert type(o + Oplist()) == Oplist");
    run_python_string("assert type(o + Operation('get')) == Oplist");
    run_python_string("assert type(o.from_) == types.StringType");
    run_python_string("assert type(o.to) == types.StringType");
    run_python_string("assert type(o.id) == types.StringType");
    expect_python_error("o.from_='1'", PyExc_TypeError);
    expect_python_error("o.from_=1", PyExc_TypeError);
    expect_python_error("o.from_=Message({'id': 1})", PyExc_TypeError);
    run_python_string("o.from_=Message({'id': '1'})");
    expect_python_error("o.to='1'", PyExc_TypeError);
    expect_python_error("o.to=1", PyExc_TypeError);
    expect_python_error("o.to=Message({'id': 1})", PyExc_TypeError);
    run_python_string("o.to=Message({'id': '1'})");
    expect_python_error("o.other=1", PyExc_AttributeError);
    
#ifdef CYPHESIS_DEBUG
    run_python_string("import sabotage");

    // Hit the assert checks.
    run_python_string("arg1=Message({'objtype': 'obj', 'parents': ['thing']})");
    run_python_string("sabotage.null(arg1)");
    expect_python_error("Operation('get', arg1)", PyExc_AssertionError);

    run_python_string("arg1=Entity()");
    run_python_string("sabotage.null(arg1)");
    expect_python_error("Operation('get', arg1)", PyExc_AssertionError);

    run_python_string("arg1=Operation('get')");
    run_python_string("sabotage.null(arg1)");
    expect_python_error("Operation('get', arg1)", PyExc_AssertionError);

    expect_python_error("Operation('get', Entity(), arg1)",
                        PyExc_AssertionError);
    expect_python_error("Operation('get', Entity(), Entity(), arg1)",
                        PyExc_AssertionError);

    run_python_string("o2=Operation('get')");
    run_python_string("sabotage.null(o2)");
    expect_python_error("o + o2", PyExc_AssertionError);

    run_python_string("sabotage.clear_parents(o)");
    expect_python_error("print o.id", PyExc_AttributeError);

    run_python_string("ol = Oplist()");
    run_python_string("sabotage.null(ol)");
    expect_python_error("o + ol", PyExc_AssertionError);

    run_python_string("method_setSerialno=o.setSerialno");
    run_python_string("method_setRefno=o.setRefno");
    run_python_string("method_setTo=o.setTo");
    run_python_string("method_setFrom=o.setFrom");
    run_python_string("method_setSeconds=o.setSeconds");
    run_python_string("method_setFutureSeconds=o.setFutureSeconds");
    run_python_string("method_setArgs=o.setArgs");
    run_python_string("method_getSerialno=o.getSerialno");
    run_python_string("method_getRefno=o.getRefno");
    run_python_string("method_getTo=o.getTo");
    run_python_string("method_getFrom=o.getFrom");
    run_python_string("method_getSeconds=o.getSeconds");
    run_python_string("method_getFutureSeconds=o.getFutureSeconds");
    run_python_string("method_getArgs=o.getArgs");
    run_python_string("method_get_name=o.get_name");

    run_python_string("sabotage.null(o)");
    expect_python_error("print o.to", PyExc_AssertionError);
    expect_python_error("len(o)", PyExc_AssertionError);
    expect_python_error("o[0]", PyExc_AssertionError);
    expect_python_error("o + None", PyExc_AssertionError);
    expect_python_error("o.to='1'", PyExc_AssertionError);

    expect_python_error("method_setSerialno(1)", PyExc_AssertionError);
    expect_python_error("method_setRefno(1)", PyExc_AssertionError);
    expect_python_error("method_setTo('1')", PyExc_AssertionError);
    expect_python_error("method_setFrom('2')", PyExc_AssertionError);
    expect_python_error("method_setSeconds(2.0)", PyExc_AssertionError);
    expect_python_error("method_setFutureSeconds(2.0)", PyExc_AssertionError);
    expect_python_error("method_setArgs([])", PyExc_AssertionError);
    expect_python_error("method_getSerialno()", PyExc_AssertionError);
    expect_python_error("method_getRefno()", PyExc_AssertionError);
    expect_python_error("method_getTo()", PyExc_AssertionError);
    expect_python_error("method_getFrom()", PyExc_AssertionError);
    expect_python_error("method_getSeconds()", PyExc_AssertionError);
    expect_python_error("method_getFutureSeconds()", PyExc_AssertionError);
    expect_python_error("method_getArgs()", PyExc_AssertionError);
    expect_python_error("method_get_name()", PyExc_AssertionError);

#endif // NDEBUG


    shutdown_python_api();
    return 0;
}
Beispiel #25
0
int main(int argc, char ** argv)
{
    int config_status = loadConfig(argc, argv, USAGE_CLIENT); 
    if (config_status < 0) {
        if (config_status == CONFIG_VERSION) {
            reportVersion(argv[0]);
            return 0;
        } else if (config_status == CONFIG_HELP) {
            showUsage(argv[0], USAGE_CLIENT, "[ [package.]function]");
            return 0;
        } else if (config_status != CONFIG_ERROR) {
            log(ERROR, "Unknown error reading configuration.");
        }
        // Fatal error loading config file
        return 1;
    }

    int optind = config_status;

    assert(optind <= argc);

    if (optind == (argc - 1)) {
        std::string arg(argv[optind]);
        std::string::size_type pos = arg.rfind('.');
        if (pos == std::string::npos) {
            // std::cout << "function " << arg << std::endl << std::flush;
            function = arg;
        } else {
            package = arg.substr(0, pos);
            function = arg.substr(pos + 1);
            // std::cout << "module.function " << package << "." << function << std::endl << std::flush;
        }
    } else if (optind != argc) {
        usage(argv[0]);
        return 1;
    }

    bool interactive = global_conf->findItem("", "interactive");
    int status = 0;

    std::vector<std::string> python_directories;
    // Add the path to the non-ruleset specific code.
    python_directories.push_back(share_directory + "/cyphesis/scripts");
    python_directories.push_back(share_directory + "/cyphesis/rulesets/basic/scripts");
    // Add the path to the ruleset specific code.
    python_directories.push_back(share_directory + "/cyphesis/rulesets/" + ruleset_name + "/scripts");

    init_python_api({&CyPy_Physics::init,
                     &CyPy_Rules::init,
                     &CyPy_EntityFilter::init,
                     &CyPy_Atlas::init,
                     &CyPy_Common::init},
                    python_directories, false);

    extend_client_python_api();
    new ClientPropertyManager();

    if (interactive) {
        python_prompt();
    } else {
        std::map<std::string, std::string> keywords;
        keywords["account"] = account;
        keywords["password"] = password;
        keywords["host"] = server;
        python_client_script(package, function, keywords);
    }

    shutdown_python_api();

    return status;
}