コード例 #1
0
ファイル: serverInstance.cpp プロジェクト: ledinhminh/cs244b
void ServerInstance::handleCommitReady(PacketBase &pb)
{
    if(pb.opCode == OPCODE_COMMIT) {
        /* Flush blocks to disk (sort by client order) */
        PRINT("[%s] FileID[%d] flushing writes...\n", N->hostname, curFd);
        for(mapit it = blocks.begin(); it != blocks.end(); ++it) {
            PacketWriteBlock &blk = it->second;
            if(newFile) {
                curFile = fopen(filepath.c_str(), "wb");
                if(curFile == NULL) {
                    throw FSException("Error creating empty file.");
                }
                newFile = false;
            }
            fseek(curFile, blk.offset, SEEK_SET);
            fwrite(blk.payload.str().c_str(), 1, blk.size, curFile);
            if(fflush(curFile)<0){
                PRINT("Error flushing writes: %s\n", strerror(errno));
            }
        }
        PRINT("[%s] FileID[%d] %lu writes committed!\n", 
                N->hostname, curFd, blocks.size());
        PacketCommitSuccess pcs;
        pcs.fileID = curFd;
        N->send(pcs);
        /* Clear block cache and wait for more writes */
        blocks.clear();
        transit(Write);
    } else if(pb.opCode == OPCODE_WRITEBLOCK) {
        /* Mostly like other servers' resend response */
        PacketWriteBlock p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        blocks[p.blockID] = p;
        //transit(Write);
    } else if(pb.opCode == OPCODE_COMMITPREPARE) {
        PacketCommitReady pcr;
        pcr.fileID = curFd;
        N->send(pcr);
        PRINT("Ready to commit\n");
    } else if(pb.opCode == OPCODE_CLOSE) {
        /* Close file; Clear block cache; Go to Idle */
        blocks.clear();
        if(curFile) fclose(curFile);
        curFile = NULL;
        transit(Idle);
        PacketCloseAck pca;
        pca.fileID = closedFd = curFd;
        N->send(pca);
        PRINT("FileID[%d] closed.\n", curFd);
    }
}
コード例 #2
0
ファイル: serverInstance.cpp プロジェクト: ledinhminh/cs244b
void ServerInstance::run()
{
    while(true) {
        PacketBase pb;
        if(N->recv(pb) == 0) {
            continue;
        }
        if(pb.type == TYPE_SERVER) {
            continue;
        }
        switch(state) {
        case Idle:
            handleIdle(pb);
            break;
        case Write:
            handleWrite(pb);
            break;
        case CommitReady:
            handleCommitReady(pb);
            break;
        default:
            throw FSException("Unknown server state");
        }
    }
}
コード例 #3
0
ファイル: init.cpp プロジェクト: e-learning/aptu-os
int main (int argc, char *argv[]) {
    try
    {
        if (argc != 2)
        {
            cout << "Usage: init root" << endl;
            return -2;
        }
        else
        {
            Meta_FS fs(argv[1]);
            Config config = fs.get_config();
            for (int i = 0; i < config.blocks_num; ++i)
            {
                const string block_name = fs.get_block_path(i);
                ofstream(block_name, ios::trunc).close();
                if (truncate(block_name.c_str(), config.block_size))
                {
                    throw FSException("Can't create blocks");
                }
            }
        }
        return 0;
    }
    catch (const FSException exc)
    {
        cerr << exc.what() << endl;
        return -1;
    }
}
コード例 #4
0
    StreamingSounds(int argc, const char * argv[]) : UnitTest(argc, argv)
    {
        vector<string> files;
    
        string dirIn = stringOption(options, "dirIn");
        string dirOut = stringOption(options, "dirOut");
        string extension = stringOption(options, "ext");

        string fileIn = "";
        string fileOut = "";
    
        findFiles(dirIn, files, extension);
        
        for (vector<string>::iterator i = files.begin(); i != files.end(); ++i)
        {
            fileIn = *i;
            fileOut = fileIn.substr(0, fileIn.find_first_of('.')) + ".swf";

            cout << fileIn << endl;

    #ifdef WIN32
            fileOut = dirOut + "\\" + fileOut;
    #else
            fileOut = dirOut + "/" + fileOut;
    #endif

            try 
            {
                FSMovie movie;
                FSSoundConstructor* soundGenerator = SoundConstructor();
                
                if (soundGenerator->setSoundFromFile(fileIn.c_str()) != TransformUtil::OK)
                {
                    throw FSException("Could not load sound file");
                }

                float framesPerSecond = 12.0f;

                movie.setFrameSize(FSBounds(0, 0, 8000, 4000));
                movie.setFrameRate(framesPerSecond);
            
                movie.add(new FSSetBackgroundColor(FSColorTable::lightblue()));

                /*
                * Calculate the time it takes to play the sound and the number of frames this
                * represents.
                */
                float duration = ((float) soundGenerator->getSamplesPerChannel()) / ((float) soundGenerator->getSampleRate());
                int numberOfFrames = (int) (duration * framesPerSecond);
                
                /*
                * Play the Streaming Sound.
                *
                * Calculate the number of decoded sound samples played for each frame and
                * the size, in bytes, of each block compressed sound data.
                */
                int samplesPerBlock = soundGenerator->getSampleRate() / (int) framesPerSecond;
                int numberOfBlocks = soundGenerator->getSamplesPerChannel() / samplesPerBlock;

                /* 
                * An FSSoundStreamHeader2 object defines the attributes of the streaming sound.
		        */
                movie.add(soundGenerator->streamHeader(samplesPerBlock));

                /* 
                * Add a streaming block for each frame so the sound is played as each frame 
                * is displayed.
                */
                for (int i=0; i<numberOfBlocks; i++)
                {
                    movie.add(soundGenerator->streamBlock(i, samplesPerBlock));
                    movie.add(new FSShowFrame());
                }
		        movie.encodeToFile(fileOut.c_str());

                delete soundGenerator;
            }
            catch (FSException e)
            {
                cerr << e.what();
            }
        }
    }
コード例 #5
0
ファイル: serverInstance.cpp プロジェクト: ledinhminh/cs244b
void ServerInstance::handleWrite(PacketBase &pb)
{
    if(pb.opCode == OPCODE_OPENFILE) {
        if(pb.fileID == curFd && lastState == Idle) {
            PacketOpenFileAck pr;
            pr.fileID = curFd;
            pr.status = FILEOPENACK_OK;
            N->send(pr);
        }
    } else if(pb.opCode == OPCODE_WRITEBLOCK) {
        PacketWriteBlock p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        blocks[p.blockID] = p;
    } else if(pb.opCode == OPCODE_COMMITPREPARE) {
        PacketCommitPrepare p;
        p.deserialize(pb.buf);
        if(p.fileID != curFd) {
            throw FSException("Unknown file descriptor");
        }
        /* Clean up extra blocks due to missed abort */
        mapit mit = blocks.begin();
        while(mit != blocks.end()) {
            if(p.blockIDs.count(mit->first) == 0) {
                mapit toErase = mit;
                ++mit;
                blocks.erase(toErase);
            } else {
                ++mit;
            }
        }
        /* Find missing blocks */
        PacketResendBlock prb;
        for(blockIDit it = p.blockIDs.begin(); it != p.blockIDs.end(); ++it) {
            if(blocks.find(*it) == blocks.end()) {
                prb.blockIDs.insert(*it);
            }
        }
        if(prb.blockIDs.size() > 0) {
            /* Missing blocks; ask for resend */
            prb.fileID = curFd;
            N->send(prb);
            PRINT("[%s] Missing %lu blocks\n", N->hostname, prb.blockIDs.size());
        } else {
            /* I'm ready */
            PacketCommitReady pcr;
            pcr.fileID = curFd;
            N->send(pcr);
            transit(CommitReady);
            PRINT("[%s] Ready to commit\n", N->hostname);
        }
    } else if(pb.opCode == OPCODE_ABORT) {
        PRINT("FileID[%d] %lu writes aborted!\n", curFd, blocks.size());
        blocks.clear();
    } else if(pb.opCode == OPCODE_CLOSE) {
        /* Close file; Clear block cache; Go to Idle */
        blocks.clear();
        if(curFile) fclose(curFile);
        curFile = NULL;
        transit(Idle);
        PacketCloseAck pca;
        pca.fileID = closedFd = curFd;
        N->send(pca);
        PRINT("FileID[%d] closed.\n", curFd);
    } else if(pb.opCode == OPCODE_COMMIT) {
        if(lastState == CommitReady) {
            PRINT("[%s] Re-ack commitSuccess\n", N->hostname);
            PacketCommitSuccess pcs;
            pcs.fileID = curFd;
            N->send(pcs);
        }
    }
}