1 #include <Atlas/Objects/Entity.h>
2 #include <Atlas/Objects/Operation.h>
3 #include <Atlas/Objects/Encoder.h>
4 #include <Atlas/Objects/Dispatcher.h>
5 #include <Atlas/Objects/loadDefaults.h>
6 //#include "../../src/Net/Stream.h"
7 #include "DebugBridge.h"
8 
9 #define DEBUG_PRINT(foo) //foo;
10 
11 #define USE_XML 1
12 #if USE_XML
13 #include <Atlas/Codecs/XML.h>
14 #else
15 #include <Atlas/Codecs/Packed.h>
16 #endif
17 
18 #include <iostream>
19 #include <fstream>
20 #include <sstream>
21 #include <cassert>
22 #include <vector>
23 #include <cstdlib>
24 
25 #if USE_XML
26 #define USE_FILE 0
27 #else
28 #define USE_FILE 0
29 #endif
30 
31 using Atlas::Message::Element;
32 using Atlas::Objects::Root;
33 using Atlas::Objects::Operation::RootOperation;
34 using Atlas::Objects::Operation::Create;
35 using Atlas::Objects::Operation::Login;
36 using Atlas::Objects::Operation::Look;
37 using Atlas::Objects::Operation::Move;
38 using Atlas::Objects::Entity::RootEntity;
39 using Atlas::Objects::Entity::Account;
40 using Atlas::Objects::Entity::Anonymous;
41 
object2String(const Root & obj)42 std::string object2String(const Root& obj)
43 {
44     DebugBridge bridge;
45     std::stringstream stream;
46     Atlas::Codec *codec;
47     codec = new Atlas::Codecs::XML(stream, bridge);
48     assert(codec);
49     codec->streamBegin();
50     Atlas::Objects::ObjectsEncoder eno(*codec);
51     eno.streamObjectsMessage(obj);
52     codec->streamEnd();
53     delete codec;
54     return stream.str();
55 }
56 
57 class TestDecoder : public Atlas::Objects::Dispatcher
58 {
59 protected:
objectRootArrived(const Root & r)60     virtual void objectRootArrived(const Root& r)
61     {
62 //        assert(r->getAttr("id").asString() == "root_instance");
63       std::cout << "got Root! " << object2String(r) << std::endl;
64     }
65 
objectLoginArrived(const Login & r)66     virtual void objectLoginArrived(const Login& r)
67     {
68         std::cout << "got Account!" << std::endl;
69 //        assert(r->getAttr("id").asString() == "root_instance");
70     }
71 
objectLookArrived(const Look & l)72     virtual void objectLookArrived(const Look& l)
73     {
74 //        assert(l->getAttr("id").asString() == "look_instance");
75         std::cout << "got Look!" << std::endl;
76     }
77 
objectAccountArrived(const Account & a)78     virtual void objectAccountArrived(const Account &a)
79     {
80         std::cout << "got Account!" << std::endl;
81     }
82 };
83 
testXML()84 void testXML()
85 {
86     RootEntity human;
87     human->setId("foo");
88 
89     Move move_op;
90     move_op->setFrom(std::string("bar"));
91     std::vector<Root> move_args(1);
92     move_args[0] = human;
93     move_op->setArgs(move_args);
94 
95     Atlas::Message::ListType velocity;
96     velocity.push_back(2.0);
97     velocity.push_back(1.0);
98     velocity.push_back(0.0);
99     human->setVelocityAsList(velocity);
100 
101 //    typedef BaseObjectData *(*alloc_func)();
102 //    alloc_func alloc_entity = &Entity::RootEntityDataInstance::alloc;
103 //    BaseObjectData *bod = alloc_entity();
104     //Root human2(bod);
105     Root human2 = Atlas::Objects::factory<Atlas::Objects::Entity::RootEntityData>("root_enitty", Atlas::Objects::Entity::RootEntity()->getClassNo());
106     std::cout<<"human.id="<<human->getId()<<std::endl;
107     std::cout<<"human2.id="<<human2->getId()<<std::endl;
108 #if 0
109     typedef std::list<Atlas::Factory<Atlas::Codec >*> FactoryCodecs;
110     FactoryCodecs *myCodecs = &Factory<Codec >::factories();
111     FactoryCodecs::iterator i;
112     std::cout<<"myCodecs: "<<myCodecs->size();
113     for (i = myCodecs->begin(); i != myCodecs->end(); ++i)
114         std::cout<<":"<<(*i)->getName();
115     std::cout<<std::endl;
116 #endif
117 
118     //DebugBridge bridge;
119     TestDecoder bridge;
120 #if USE_FILE
121     fstream stream;
122     std::string atlas_xml_path;
123     char * srcdir_env = getenv("srcdir");
124     if (srcdir_env != 0) {
125         atlas_xml_path = srcdir_env;
126         atlas_xml_path += "/";
127     }
128     atlas_xml_path += "../../protocol/spec/atlas.xml";
129     stream.open(atlas_xml_path, std::ios::in);
130     assert(!!stream);
131 #else
132     std::stringstream stream;
133 #endif
134 //     typedef std::list<Atlas::Factory<Atlas::Codec >*> FactoryCodecs;
135 //     FactoryCodecs *myCodecs = &Factory<Codec >::factories();
136 //     FactoryCodecs::iterator codec_i;
137 //     Atlas::Codec *codec = NULL;
138 //     for(codec_i = myCodecs->begin(); codec_i != myCodecs->end(); ++codec_i)
139 //     {
140 //         std::cout<<(*codec_i)->getName()<<std::endl;
141 //         if ((*codec_i)->getName() == "XML") {
142 //             codec = (*codec_i)->New(Codec::Parameters(stream, &bridge));
143 //         }
144 //     }
145 //     assert(codec);
146 
147 
148     Account account;
149     Login l;
150     account->setAttr("id", std::string("al"));
151     account->setAttr("password", std::string("ping"));
152     //list<Message::Object> args(1,account->asObject());
153     //l->setArgsAsList(args);
154     std::vector<Root> args(1);
155     args[0] = account;
156     l->setArgs(args);
157     //coder->streamObjectsMessage((Root&)l);
158 //<map><list name="args"><map><std::string name="id">al</strin
159 //g></map></list><list name="parents"><std::string>root</std::string></list><std::string name="ob
160 //jtype">op_definition</std::string></map>
161 
162 
163     Atlas::Codec *codec;
164 #if USE_XML
165     codec = new Atlas::Codecs::XML((std::iostream&)stream, bridge);
166 #else
167     codec = new Atlas::Codecs::Packed(stream, bridge);
168 #endif
169     assert(codec);
170 
171 #if USE_FILE
172     while(stream) {
173       codec->poll();
174       //std::cout<<"--------"<<std::endl;
175     }
176 #else
177     codec->streamBegin();
178 
179     Atlas::Objects::ObjectsEncoder eno(*codec);
180 //    eno.streamObjectsMessage(move_op);
181     eno.streamObjectsMessage(l);
182 
183     Anonymous e;
184     eno.streamObjectsMessage(e);
185     e->setId("foo");
186     eno.streamObjectsMessage(e);
187 //    Atlas::Message::Encoder en(codec);
188 //    en.streamObjectsMessage(human->asObject());
189 
190     codec->streamEnd();
191     std::cout<<std::endl<<stream.str()<<std::endl;
192     //[$from=bar(args=[$id=foo])][$id=foo]
193     //<atlas><map><std::string name="from">bar</std::string><list name="args"><map><std::string name="id">foo</std::string></map></list></map><map><std::string name="id">foo</std::string></map></atlas>
194 #endif
195 }
196 
197 
check_float_list3(const Atlas::Message::ListType & list,double el1,double el2,double el3)198 void check_float_list3(const Atlas::Message::ListType &list,
199                        double el1, double el2, double el3)
200 {
201     assert( list.size() == 3 );
202     Atlas::Message::ListType::const_iterator i = list.begin();
203     assert( (*i++) == el1 );
204     assert( (*i++) == el2 );
205     assert( (*i++) == el3 );
206     i++;
207 }
208 
testValues()209 void testValues()
210 {
211     Account account;
212     Login l;
213     account->setId("al");
214     account->setAttr("password", std::string("ping"));
215     l->setArgs1(account);
216 
217     // assert(l->getArgs()[0]->getLongDescription()=="Later in hierarchy tree objtype changes to 'object' when actual game objects are made.");
218     // assert(l->getArgs()[0]->getDescription()=="Base class for accounts");
219     assert(l->getId()=="");
220     assert(l->getParents().front()=="login");
221     assert(l->getObjtype()=="op");
222     // std::cout<<std::endl<<"account.long_description: "
223         // <<l->getArgs()[0]->getLongDescription()<<std::endl;
224 
225     {
226     Atlas::Message::MapType mobj;
227     Atlas::Message::ListType parents;
228     parents.push_back(std::string("account"));
229     mobj["parents"] = parents;
230     mobj["name"] = std::string("foo");
231     mobj["objtype"] = std::string("op");
232     Root obj = Atlas::Objects::Factories::instance()->createObject(mobj);
233     assert(obj->getClassNo() == Atlas::Objects::Entity::ACCOUNT_NO);
234     assert(obj->getId() == "");
235     assert(obj->isDefaultId() == true);
236     assert(obj->getName() == "foo");
237     assert(obj->isDefaultName() == false);
238     assert(obj->getParents().front() == "account");
239     //should this be true? modify MessageObject2ClassObject if yes
240     assert(obj->isDefaultParents() == false);
241     assert(obj->getObjtype() == "op");
242     assert(obj->isDefaultObjtype() == false); //should this be true?
243     // assert(obj->getDescription() == "Base class for accounts");
244     // assert(obj->isDefaultDescription() == true);
245     }
246 
247     {
248     Atlas::Message::MapType mobj;
249     Root obj = Atlas::Objects::objectDefinitions.find(std::string("account"))->second;
250     assert(obj->getClassNo() == Atlas::Objects::Entity::ACCOUNT_NO);
251     assert(obj->getId() == "account");
252     assert(obj->isDefaultId() == false);
253     assert(obj->getName() == "");
254     assert(obj->isDefaultName() == true);
255     assert(obj->getParents().front() == "admin_entity");
256     assert(obj->isDefaultParents() == false);
257     assert(obj->getObjtype() == "class");
258     assert(obj->isDefaultObjtype() == false);
259     // assert(obj->getDescription() == "Base class for accounts");
260     // assert(obj->isDefaultDescription() == false);
261     }
262 
263     {
264     Atlas::Message::MapType mobj;
265     Root obj = Atlas::Objects::Factories::instance()->createObject(mobj);
266     assert(obj->getClassNo() == Atlas::Objects::Entity::ANONYMOUS_NO);
267     assert(obj->getId() == "");
268     assert(obj->getName() == "");
269     assert(obj->getParents().size() == 0);
270     assert(obj->getObjtype() == "obj");
271     // assert(obj->getDescription() == "");
272     }
273 
274     {
275     Atlas::Message::MapType mobj;
276     mobj["id"] = std::string("bar");
277     mobj["name"] = std::string("foo");
278     Atlas::Message::ListType parents;
279     parents.push_back(std::string("account"));
280     mobj["parents"] = parents;
281     Root obj = Atlas::Objects::Factories::instance()->createObject(mobj);
282     assert(obj->getClassNo() == Atlas::Objects::Entity::ANONYMOUS_NO);
283     assert(obj->getId() == "bar");
284     assert(obj->getName() == "foo");
285     assert(obj->getParents().front() == "account");
286     assert(obj->getObjtype() == "obj");
287     // assert(obj->getDescription() == "");
288     }
289 
290     {
291     Atlas::Message::MapType maccount;
292     maccount["id"] = std::string("bar");
293     maccount["name"] = std::string("foo");
294     Atlas::Message::ListType parents;
295     parents.push_back(std::string("player"));
296     maccount["parents"] = parents;
297     maccount["objtype"] = "obj";
298 
299     Atlas::Message::MapType mcreate;
300     mcreate["from"] = std::string("bar");
301     Atlas::Message::ListType parents2;
302     parents2.push_back(std::string("create"));
303     mcreate["parents"] = parents2;
304     Atlas::Message::ListType args;
305     args.push_back(maccount);
306     mcreate["args"] = args;
307     mcreate["objtype"] = "op";
308 
309     Create op = Atlas::Objects::smart_dynamic_cast<Create>(Atlas::Objects::Factories::instance()->createObject(mcreate));
310     assert(op->getClassNo() == Atlas::Objects::Operation::CREATE_NO);
311     assert(op->instanceOf(Atlas::Objects::Operation::CREATE_NO));
312     assert(op->instanceOf(Atlas::Objects::Operation::ACTION_NO));
313     assert(op->instanceOf(Atlas::Objects::ROOT_NO));
314     assert(!op->instanceOf(Atlas::Objects::Operation::COMBINE_NO));
315     assert(!op->instanceOf(Atlas::Objects::Entity::ACCOUNT_NO));
316     assert(op->getFrom() == "bar");
317     assert(op->getParents().size() == 1);
318     assert(op->getParents().front() == "create");
319     assert(op->getObjtype() == "op");
320     // assert(op->getDescription() ==
321            // "Create new things from nothing using this operator.");
322     assert(op->getArgs().size() == 1);
323 
324     Account op_arg = (Account&)op->getArgs().front();
325     assert(op_arg->getClassNo() == Atlas::Objects::Entity::PLAYER_NO);
326     assert(!op_arg->instanceOf(Atlas::Objects::Operation::CREATE_NO));
327     assert(!op_arg->instanceOf(Atlas::Objects::Operation::ACTION_NO));
328     assert(op_arg->instanceOf(Atlas::Objects::ROOT_NO));
329     assert(!op_arg->instanceOf(Atlas::Objects::Operation::COMBINE_NO));
330     assert(op_arg->instanceOf(Atlas::Objects::Entity::ACCOUNT_NO));
331     assert(op_arg->instanceOf(Atlas::Objects::Entity::PLAYER_NO));
332     assert(op_arg->getId() == "bar");
333     assert(op_arg->getParents().size() == 1);
334     assert(op_arg->getParents().front() == "player");
335     assert(op_arg->getObjtype() == "obj");
336     // assert(op_arg->getDescription() == "Player accounts");
337     assert(op_arg->getName() == "foo");
338 #if 0 //tmp
339     assert(op_arg->hasAttr("password"));
340     assert(op_arg->getAttr("password").isString());
341     assert(op_arg->getAttr("password").asString() == "");
342 #endif
343     assert(op_arg->hasAttr("name"));
344     assert(!op_arg->hasAttr("foo"));
345     assert(!op_arg->hasAttr("pos"));
346     assert(!op_arg->isDefaultName());
347     assert(op_arg->isDefaultPos());
348     }
349 }
350 
test()351 void test()
352 {
353     const double x1 = 3.5;
354     const double y1 = -4.6;
355     const double z1 = 2.0;
356 
357     const double x2 = 42.0;
358     const double y2 = 7.0;
359 
360     std::vector<RootEntity> ent_vec(10);
361 
362     for(int i=0; i<10; i++) {
363         DEBUG_PRINT(std::cout<<std::endl<<"round:"<<i<<std::endl);
364         RootEntity human;
365 
366         //check for empty default:
367         DEBUG_PRINT(std::cout<<"empty ok?"<<std::endl);
368         Atlas::Message::ListType empty = human->getVelocityAsList();
369         if(i==0) check_float_list3(empty, 0.0, 0.0, 0.0);
370         else check_float_list3(empty, 0.0, y2, 0.0);
371 
372         //check after setting it
373         DEBUG_PRINT(std::cout<<"setting ok?"<<std::endl);
374         Atlas::Message::ListType velocity;
375         velocity.push_back(x1);
376         velocity.push_back(y1);
377         velocity.push_back(z1);
378         check_float_list3(velocity, x1, y1, z1);
379         human->setVelocityAsList(velocity);
380         Atlas::Message::ListType foo = human->getVelocityAsList();
381         check_float_list3(foo, x1, y1, z1);
382 
383         DEBUG_PRINT(std::cout<<"changing it?"<<std::endl);
384         std::vector<double> &foo2 = human->modifyVelocity();
385         *foo2.begin() = x2;
386         check_float_list3(human->getVelocityAsList(), x2, y1, z1);
387 
388         DEBUG_PRINT(std::cout<<"check change result?"<<std::endl);
389         foo = human->getVelocityAsList();
390         check_float_list3(foo, x2, y1, z1);
391 
392         DEBUG_PRINT(std::cout<<"std::vector of entities?"<<std::endl);
393         const Atlas::Message::ListType &ent_velocity = ent_vec[i]->getVelocityAsList();
394         if(i==0) check_float_list3(ent_velocity, 0.0, 0.0, 0.0);
395         else check_float_list3(ent_velocity, 0.0, y2, 0.0);
396 
397         DEBUG_PRINT(std::cout<<"base?"<<std::endl);
398         RootEntity base_entity = human.getDefaultObject();
399         std::vector<double> &base = base_entity->modifyVelocity();
400         base[1] = y2;
401         check_float_list3(base_entity->getVelocityAsList(), 0.0, y2, 0.0);
402 
403         RootOperation move_op;
404         std::vector<Root> move_args(1);
405         move_args[0] = human;
406         move_op->setArgs(move_args);
407 
408         RootOperation sight_op;
409         //sight_op->setFrom(humanent.asObjectPtr());
410         std::vector<Root> sight_args(1);
411         sight_args[0] = move_op;
412         sight_op->setArgs(sight_args);
413 
414         //test
415         DEBUG_PRINT(std::cout<<"get move_op?"<<std::endl);
416         const std::vector<Root>& test_args = sight_op->getArgs();
417         assert(test_args.size() == 1);
418         RootOperation test_op =
419             (RootOperation&)test_args[0];
420 
421         DEBUG_PRINT(std::cout<<"get human_ent?"<<std::endl);
422         const std::vector<Root>& test_args2 = test_op->getArgs();
423         assert(test_args2.size() == 1);
424         RootEntity test_ent =
425             (RootEntity&)test_args2[0];
426         Atlas::Message::ListType foo3 = test_ent->getVelocityAsList();
427         check_float_list3(foo3, x2, y1, z1);
428 
429         std::vector<double> coords(3, 0.0);
430         human->setPos(coords);
431         human->setVelocity(coords);
432         human->modifyVelocity()[0] = 1.0;
433         check_float_list3(human->getPosAsList(), 0.0, 0.0, 0.0);
434         check_float_list3(human->getVelocityAsList(), 1.0, 0.0, 0.0);
435     }
436 }
437 
main()438 int main()
439 {
440     std::string atlas_xml_path;
441     char * srcdir_env = getenv("srcdir");
442     if (srcdir_env != 0) {
443         atlas_xml_path = srcdir_env;
444         atlas_xml_path += "/";
445     }
446     atlas_xml_path += "../../protocol/spec/atlas.xml";
447     try {
448         Atlas::Objects::loadDefaults(atlas_xml_path);
449     } catch(Atlas::Objects::DefaultLoadingException e) {
450         std::cout << "DefaultLoadingException: "
451              << e.getDescription() << std::endl;
452     }
453     testXML();
454     testValues();
455     test();
456     return 0;
457 }
458