LoadDefaultsDecoder::LoadDefaultsDecoder(const std::string& filename)
{
    std::fstream stream;
    stream.open(filename.c_str(), std::ios::in);
    if (!stream) {
        throw DefaultLoadingException("Failed to open file " + filename);
    }
  
  //replace following code with:
  //getCodecByName(const std::string& name, Stream& stream, Bridge* bridge)

  //typedef list<Atlas::Factory<Atlas::Codec<std::iostream> >*> FactoryCodecs;
  //FactoryCodecs *myCodecs = Factory<Codec<std::iostream> >::factories();
  //FactoryCodecs::iterator i;
  //Atlas::Codec<std::iostream> *codec = NULL;
  //for (i = myCodecs->begin(); i != myCodecs->end(); ++i)
    //if((*i)->getName() == "XML")
      //codec = (*i)->New(Codec<std::iostream>::Parameters((iostream&)stream, this));
  //end of replace
    Atlas::Codec *codec = new Atlas::Codecs::XML(stream, *this);

    while(stream) {
        codec->poll();
    }

    delete codec;
  
    MapType anonymous_obj;
    m_objects["anonymous"] = anonymous_obj;
    MapType generic_obj;
    m_objects["generic"] = generic_obj;
    fillDefaults();
}
Example #2
0
// This sends a very simple message to c
void helloWorld(Atlas::Codec & c)
{
    std::cout << "Sending hello world message... " << std::flush;
    c.streamMessage();
    c.mapStringItem("hello", "world");
    c.mapEnd();
    std::cout << "done." << std::endl;
}
Example #3
0
std::string object2String(const Root& obj)
{
    DebugBridge bridge;
    std::stringstream stream;
    Atlas::Codec *codec;
    codec = new Atlas::Codecs::XML(stream, bridge);
    assert(codec);
    codec->streamBegin();
    Atlas::Objects::ObjectsEncoder eno(*codec);
    eno.streamObjectsMessage(obj);
    codec->streamEnd();
    delete codec;
    return stream.str();
}
Example #4
0
int main(int argc, char** argv)
{
    // The socket that connects us to the server
    tcp_socket_stream connection;

    std::cout << "Connecting..." << std::flush;
    
    // Connect to the server
    if(argc>1) {
      connection.open(argv[1], 6767);
    } else {
      connection.open("127.0.0.1", 6767);
    }
    
    // The DebugBridge puts all that comes through the codec on cout
    DebugBridge bridge;
    // Do client negotiation with the server
    Atlas::Net::StreamConnect conn("simple_client", connection);

    std::cout << "Negotiating... " << std::flush;
    // conn.poll() does all the negotiation
    while (conn.getState() == Atlas::Net::StreamConnect::IN_PROGRESS) {
        conn.poll();
    }
    std::cout << "done" << std::endl;

    // Check whether negotiation was successful
    if (conn.getState() == Atlas::Net::StreamConnect::FAILED) {
        std::cerr << "Failed to negotiate" << std::endl;
        return 2;
    }
    // Negotiation was successful

    // Get the codec that negotiation established
    Atlas::Codec * codec = conn.getCodec(bridge);

    // This should always be sent at the beginning of a session
    codec->streamBegin();
    
    // Say hello to the server
    helloWorld(*codec);
    connection << std::flush;

    std::cout << "Sleeping for 2 seconds... " << std::flush;
    // Sleep a little
    sleep(2);
    std::cout << "done." << std::endl;

    // iosockinet::operator bool() returns false if the connection was broken
    if (!connection) {
        std::cout << "Server exited." << std::endl;
    } else {
        // It was not broken by the server, so we'll close ourselves
        std::cout << "Closing connection... " << std::flush;
        // This should always be sent at the end of a session
        codec->streamEnd();
        connection << std::flush;
        // Close the socket
        connection.close();
        std::cout << "done." << std::endl;
    }

    return 0;
}
Example #5
0
void testXML()
{
    RootEntity human;
    human->setId("foo");

    Move move_op;
    move_op->setFrom(std::string("bar"));
    std::vector<Root> move_args(1);
    move_args[0] = human;
    move_op->setArgs(move_args);

    Atlas::Message::ListType velocity;
    velocity.push_back(2.0);
    velocity.push_back(1.0);
    velocity.push_back(0.0);
    human->setVelocityAsList(velocity);

//    typedef BaseObjectData *(*alloc_func)();
//    alloc_func alloc_entity = &Entity::RootEntityDataInstance::alloc;
//    BaseObjectData *bod = alloc_entity();
    //Root human2(bod);
    Root human2 = Atlas::Objects::factory<Atlas::Objects::Entity::RootEntityData>("root_enitty", Atlas::Objects::Entity::RootEntity()->getClassNo());
    std::cout<<"human.id="<<human->getId()<<std::endl;
    std::cout<<"human2.id="<<human2->getId()<<std::endl;
#if 0
    typedef std::list<Atlas::Factory<Atlas::Codec >*> FactoryCodecs;
    FactoryCodecs *myCodecs = &Factory<Codec >::factories();
    FactoryCodecs::iterator i;
    std::cout<<"myCodecs: "<<myCodecs->size();
    for (i = myCodecs->begin(); i != myCodecs->end(); ++i)
        std::cout<<":"<<(*i)->getName();
    std::cout<<std::endl;
#endif

    //DebugBridge bridge;
    TestDecoder bridge;
#if USE_FILE
    fstream stream;
    std::string atlas_xml_path;
    char * srcdir_env = getenv("srcdir");
    if (srcdir_env != 0) {
        atlas_xml_path = srcdir_env;
        atlas_xml_path += "/";
    }
    atlas_xml_path += "../../protocol/spec/atlas.xml";
    stream.open(atlas_xml_path, std::ios::in);
    assert(!!stream);
#else
    std::stringstream stream;
#endif
//     typedef std::list<Atlas::Factory<Atlas::Codec >*> FactoryCodecs;
//     FactoryCodecs *myCodecs = &Factory<Codec >::factories();
//     FactoryCodecs::iterator codec_i;
//     Atlas::Codec *codec = NULL;
//     for(codec_i = myCodecs->begin(); codec_i != myCodecs->end(); ++codec_i)
//     {
//         std::cout<<(*codec_i)->getName()<<std::endl;
//         if ((*codec_i)->getName() == "XML") {
//             codec = (*codec_i)->New(Codec::Parameters(stream, &bridge));
//         }
//     }
//     assert(codec);

    
    Account account;
    Login l;
    account->setAttr("id", std::string("al"));
    account->setAttr("password", std::string("ping"));
    //list<Message::Object> args(1,account->asObject());
    //l->setArgsAsList(args);
    std::vector<Root> args(1);
    args[0] = account;
    l->setArgs(args);
    //coder->streamObjectsMessage((Root&)l);
//<map><list name="args"><map><std::string name="id">al</strin
//g></map></list><list name="parents"><std::string>root</std::string></list><std::string name="ob
//jtype">op_definition</std::string></map>


    Atlas::Codec *codec;
#if USE_XML
    codec = new Atlas::Codecs::XML((std::iostream&)stream, bridge);
#else
    codec = new Atlas::Codecs::Packed(stream, bridge);
#endif
    assert(codec);

#if USE_FILE
    while(stream) {
      codec->poll();
      //std::cout<<"--------"<<std::endl;
    }
#else
    codec->streamBegin();

    Atlas::Objects::ObjectsEncoder eno(*codec);
//    eno.streamObjectsMessage(move_op);
    eno.streamObjectsMessage(l);

    Anonymous e;
    eno.streamObjectsMessage(e);
    e->setId("foo");
    eno.streamObjectsMessage(e);
//    Atlas::Message::Encoder en(codec);
//    en.streamObjectsMessage(human->asObject());

    codec->streamEnd();
    std::cout<<std::endl<<stream.str()<<std::endl;
    //[$from=bar(args=[$id=foo])][$id=foo]
    //<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>
#endif
    delete codec;
}
int convert(const std::string & file_in, const std::string & codec_in,
            const std::string & file_out, const std::string & codec_out)
{
    std::cout << "Convert " << codec_in << " to " << codec_out << std::endl;

    std::fstream in, out;

    in.open( file_in.c_str(), std::ios::in );

    if (!in.is_open()) {
        std::cerr << "Unable to open " << file_in << " for input"
                  << std::endl << std::flush;
        return 1;
    }

    out.open( file_out.c_str(), std::ios::out );

    if (!out.is_open()) {
        std::cerr << "Unable to open " << file_out << " for output"
                  << std::endl << std::flush;
        return 1;
    }

    std::cout << "Reading... ";

    Atlas::Message::QueuedDecoder decoder;
    Atlas::Codec *inCodec = getCodec(codec_in, in, decoder);
    while (!in.eof()) {
        inCodec->poll(true);
    }

    std::cout << "done." << std::endl;
    std::cout << "Writing... ";

    Atlas::Codec * outCodec = getCodec(codec_out, out, decoder);
    Atlas::Bridge * bridge;

    if (option_format) {
        Atlas::Formatter * format;
        bridge = format = new Atlas::Formatter(out, *outCodec);
        if (option_spacing != -1) {
            format->setSpacing(option_spacing);
        }
    } else {
        bridge = outCodec;
    }

    Atlas::Message::Encoder encoder(*bridge);
    encoder.streamBegin();
    while (decoder.queueSize() > 0 ) {
        Atlas::Message::MapType msg(decoder.popMessage());
        encoder.streamMessageElement(msg);
    }
    encoder.streamEnd();

    std::cout << "done." << std::endl;

    out.close();
    in.close();

    return 0;
}