Esempio n. 1
0
int MojoSetup_testNetworkCode(int argc, char **argv)
{
    int i;
    fprintf(stderr, "Testing networking code...\n\n");
    for (i = 1; i < argc; i++)
    {
        static char buf[64 * 1024];
        uint32 start = 0;
        const char *url = argv[i];
        int64 length = -1;
        int64 total_br = 0;
        int64 br = 0;
        printf("\n\nFetching '%s' ...\n", url);
        MojoInput *io = MojoInput_newFromURL(url);
        if (io == NULL)
        {
            fprintf(stderr, "failed!\n");
            continue;
        } // if

        start = MojoPlatform_ticks();
        while (!io->ready(io))
            MojoPlatform_sleep(10);
        fprintf(stderr, "took about %d ticks to get started\n",
                (int) (MojoPlatform_ticks() - start));

        length = io->length(io);
        fprintf(stderr, "Ready to read (%lld) bytes.\n",
                (long long) length);

        do
        {
            start = MojoPlatform_ticks();
            if (!io->ready(io))
            {
                fprintf(stderr, "Not ready!\n");
                while (!io->ready(io))
                    MojoPlatform_sleep(10);
                fprintf(stderr, "took about %d ticks to get ready\n",
                        (int) (MojoPlatform_ticks() - start));
            } // if

            start = MojoPlatform_ticks();
            br = io->read(io, buf, sizeof (buf));
            fprintf(stderr, "read blocked for about %d ticks\n",
                    (int) (MojoPlatform_ticks() - start));
            if (br > 0)
            {
                total_br += br;
                fprintf(stderr, "read %lld bytes\n", (long long) br);
                fwrite(buf, br, 1, stdout);
            } // if
        } while (br > 0);

        if (br < 0)
            fprintf(stderr, "ERROR IN TRANSMISSION.\n\n");
        else
        {
            fprintf(stderr, "TRANSMISSION COMPLETE!\n\n");
            fprintf(stderr, "(Read %lld bytes, expected %lld.)\n",
                    (long long) total_br, length);
        } // else
        io->close(io);
    } // for

    return 0;
} // MojoSetup_testNetworkCode
Esempio n. 2
0
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
// !!! FIXME:  way right now...
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
                                 MojoChecksums *checksums, int64 maxbytes,
                                 MojoInput_FileCopyCallback cb, void *data)
{
    boolean retval = false;
    uint32 start = MojoPlatform_ticks();
    void *out = NULL;
    boolean iofailure = false;
    int64 flen = 0;
    int64 bw = 0;
    MojoChecksumContext sumctx;

    if (in == NULL)
        return false;

    if (checksums != NULL)
    {
        memset(checksums, '\0', sizeof (MojoChecksums));
        MojoChecksum_init(&sumctx);
    } // if

    // Wait for a ready(), so length() can be meaningful on network streams.
    while ((!in->ready(in)) && (!iofailure))
    {
        MojoPlatform_sleep(100);
        if (cb != NULL)
        {
            if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data))
                iofailure = true;
        } // if
    } // while

    flen = in->length(in);
    if ((maxbytes >= 0) && (flen > maxbytes))
        flen = maxbytes;

    MojoPlatform_unlink(fname);
    if (!iofailure)
    {
        const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE;
        const uint16 mode = MojoPlatform_defaultFilePerms();
        out = MojoPlatform_open(fname, flags, mode);
    } // if

    if (out != NULL)
    {
        while (!iofailure)
        {
            int64 br = 0;
            int64 maxread = sizeof (scratchbuf_128k);

            // see if we need to clamp to eof or maxbytes...
            if (flen >= 0)
            {
                const int64 avail = flen - bw;
                if (avail < maxread)
                {
                    maxread = avail;
                    if (maxread == 0)
                        break;  // nothing left to do, break out.
                } // if
            } // if

            // If there's a callback, then poll. Otherwise, just block on
            //  the reads from the MojoInput.
            if ((cb != NULL) && (!in->ready(in)))
                MojoPlatform_sleep(100);
            else
            {
                br = in->read(in, scratchbuf_128k, (uint32) maxread);
                if (br == 0)  // we're done!
                    break;
                else if (br < 0)
                    iofailure = true;
                else
                {
                    if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br)
                        iofailure = true;
                    else
                    {
                        if (checksums != NULL)
                            MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br);
                        bw += br;
                    } // else
                } // else
            } // else

            if (cb != NULL)
            {
                if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data))
                    iofailure = true;
            } // if
        } // while

        if (MojoPlatform_close(out) != 0)
            iofailure = true;
        else if (bw != flen)
            iofailure = true;

        if (iofailure)
            MojoPlatform_unlink(fname);
        else
        {
            MojoPlatform_chmod(fname, perms);
            if (checksums != NULL)
                MojoChecksum_finish(&sumctx, checksums);
            retval = true;
        } // else
    } // if

    in->close(in);
    return retval;
} // MojoInput_toPhysicalFile