Giter VIP home page Giter VIP logo

Comments (3)

honglei avatar honglei commented on September 25, 2024

caused by the follwing line:

tx_pending_path[0] = '\0';

from norm.

bebopagogo avatar bebopagogo commented on September 25, 2024

Setting the tx_pending_path[0] = '\0' is used to indicate there is no pending transmit file. The TxFilePending() method is checked before EnqueueFileObject() is called when it returns false, EnqueueFileObject() should not be called. So I am not sure how this problem is occurring. I am investigating it further.

from norm.

honglei avatar honglei commented on September 25, 2024

The C++17 std::filesystem provides much eacher way to iterate dir:

#include <iostream>
#include <string>
#include <filesystem> // C++ 17
#include "normApi.h"   

namespace fs = std::filesystem;

// find first normal file 
bool fileEnqueue(NormSessionHandle session, fs::recursive_directory_iterator& itEntry)
{
    while (itEntry != fs::recursive_directory_iterator()) {
        if (!itEntry->is_regular_file()) {
            ++itEntry;
            continue;
        };
        const auto filePathStr = itEntry->path().string(); //.filename().string();
        auto info = std::string(filePathStr);
        std::replace(info.begin(), info.end(), '\\', '/');
        NormObjectHandle objHandler = NormFileEnqueue(session, filePathStr.c_str(), info.c_str(), info.length());
        if (objHandler != NORM_OBJECT_INVALID) {
            std::cout << "push:" << filePathStr<<std::endl;
        };
        ++itEntry;
        return true;
    } 
    return false;
}

NormSessionHandle create_Session(NormInstanceHandle normInstance, fs::recursive_directory_iterator& itEntry, const char* addr, uint16_t port, const char* localAddr, NormNodeId localID) {
    auto session = NormCreateSession(normInstance, addr, port, localID);
    NormSetTxPort(session, 0, true, localAddr);
    NormSetTxOnly(session, true);
    NormSetCongestionControl(session, true);
    //NormSetTxRateBounds
    NormSessionId instanceId = NormGetRandomSessionId();

    NormStartSender(session, 1, 100 * 1024 * 1024, 1400, 128, 0);
    //auto& dirEntry : std::filesystem::recursive_directory_iterator(path)
    fileEnqueue(session, itEntry);
    return session;
}


int main()
{
    NormInstanceHandle normInstance = NormCreateInstance();
    NormSetDebugLevel(3);
    uint16_t port = 6003;
    int localID = 191;
    auto localAddr = "10.65.39.191";

    auto addr = "224.1.2.3";
    const char* dirPath = "E:\\PythonPrj\\sendFiles\\ch1"; //Change this to real dir
    auto itEntry = fs::recursive_directory_iterator(dirPath);

    auto session1 = create_Session(normInstance, itEntry, addr, port, localAddr, localID);

    auto addr2 = "224.1.2.4";
    const char* dirPath2 = "E:\\PythonPrj\\sendFiles\\ch2"; //Change this to real dir
    auto itEntry2 = fs::recursive_directory_iterator(dirPath2);

    auto session2 = create_Session(normInstance, itEntry2, addr2, port, localAddr, localID);

    char buffer[512];
    bool success = false;
    bool keepGoing = true;
    int count1 = 0;
    int count2 = 0;
    NormEvent theEvent =NormEvent();
    while (keepGoing)
    {
        
        auto session = theEvent.session;
        if (!NormGetNextEvent(normInstance, &theEvent)) continue;
        switch (theEvent.type)
        {
        case NORM_TX_QUEUE_EMPTY:
            if (theEvent.session == session1)
            {
                fileEnqueue(session, itEntry);
            }
            else if (theEvent.session == session2)
            {
                fileEnqueue(session, itEntry2);
            };
            break;

        case NORM_TX_OBJECT_PURGED:

            success = NormFileGetName(theEvent.object, buffer, sizeof(buffer));
            if (success) {
                fs::remove(fs::path(buffer) ); //remove file
            }
            else 
             {
                std::cout << "xxx"<<std::endl;
            }
            if (theEvent.session == session1) {
                ++count1;
            }
            else if (theEvent.session == session2)
            {
                ++count2;
            }
            std::cerr << "count1:" << count1 << " count2:" << count2 << std::endl;
            if (theEvent.session == session1)
            {
                fileEnqueue(session, itEntry);
            }
            else if (theEvent.session == session2)
            {
                fileEnqueue(session, itEntry2);
            };
            break;

        //case NORM_TX_FLUSH_COMPLETED:
        //    std::cerr << "normFileSend: NORM_TX_FLUSH_COMPLETED event ...\n";
        //    keepGoing = false;  // our file has been sent (we think)
        //    break;

        default:
            std::cerr <<"type:"<< theEvent.type<<std::endl;
        }  // end switch(theEvent.type)
    }  // end while (NormGetNextEvent())

    NormStopSender(session1);
    NormDestroySession(session1);
    NormStopSender(session2);
    NormDestroySession(session2);
    NormDestroyInstance(normInstance);
    //listFiles("E:\\PythonPrj\\NORM\\norm159\\examples");
    return 0;
}

from norm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.