力战者
力战者,天酬之!

////

////  main.cpp

////  ResumeTest

////

////  Created by zhangguangming1 on 2019/5/12.

////  Copyright © 2019 zhangguangming1. All rights reserved.

////

//

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <string>

#include <curl/curl.h>

#include <unistd.h>

#include<fstream>

#include "curl_http.h"

#include "md5.h"

#include <thread>

#include <termios.h>

//

//

#define RUN 1

#define STOP 0

 

typedef struct{

    string url;

    string fileMD5;

    string targetname;

    string savepath;

}*pFileInfo,FileInfo;

 

pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

 

int status = STOP;

 

std::string getFileMd5(string file) {

    if (0!=access(file.c_str(),0)) {

        return "";

    }

    std::ifstream filestream(file, std::ios::binary|std::ios::in);

    MD5 md5;

    md5.update(filestream);

    return md5.toString();

}

 

int scanKeyboard()

{

    int in;

    struct termios new_settings;

    struct termios stored_settings;

    tcgetattr(0,&stored_settings);

    

    new_settings = stored_settings;

    new_settings.c_lflag &= (~ICANON);

    new_settings.c_cc[VTIME] = 0;

    tcgetattr(0,&stored_settings);

    new_settings.c_cc[VMIN] = 1;

    tcsetattr(0,TCSANOW,&new_settings);

    

    in = getchar();

    

    tcsetattr(0,TCSANOW,&stored_settings);

    return in;

}

 

void * thread_function(void*arg)

{

    pthread_mutex_lock(&mut);

    while (!status)

    {

        pthread_cond_wait(&cond, &mut);

    }

    pthread_mutex_unlock(&mut);

    pFileInfo info = (pFileInfo)arg;

    curl_http_downloader dwcurl(info->targetname);

    dwcurl.Download(info->url, info->savepath, nullptr,info->fileMD5);

    return NULL;

}

 

void thread_resume()

{

    if (status == STOP)

    {

        pthread_mutex_lock(&mut);

        status = RUN;

        pthread_cond_signal(&cond);

        printf("pthread run!\n");

        pthread_mutex_unlock(&mut);

    }

    else

    {

        printf("pthread run already\n");

    }

}

 

 

void thread_pause()

{

    if (status == RUN)

    {

        pthread_mutex_lock(&mut);

        status = STOP;

        printf("thread stop!\n");

        pthread_mutex_unlock(&mut);

    }

    else

    {

        printf("pthread pause already\n");

    }

}

int main(int argc, const char * argv[]) {

//    // insert code here...

//      http://192.168.34.72/pc_client/teacher-dev-mac/output/vipkid_mac_100/VIPKIDT-v1.7.5-mac-x64.dmg

//    string url = "http://192.168.34.72/pc_client/teacher-dev-mac-qt/output/vipkid_mac_10/VIPKIDT-v-mac-x64.dmg";

//    string CurrectMD5 = getFileMd5("./VIPKIDT-v-mac-x64.dmg");

//    string name = "VIPKIDT.dmg";

//    string path = "./";

//    curl_http_downloader dwcurl(name);

//    dwcurl.Download(url, path, nullptr,CurrectMD5);

    

     pFileInfo info = new FileInfo;

     info->url = "http://192.168.34.72/pc_client/teacher-dev-mac/output/vipkid_mac_100/VIPKIDT-v1.7.5-mac-x64.dmg";

     info->fileMD5 = getFileMd5("./VIPKIDT-v-mac-x64.dmg");

     info->targetname = "VIPKIDT.dmg";

     info->savepath = "./";

     pthread_t pthid;

     pthread_create(&pthid, NULL,thread_function, (void*)info );

     int key ;

     while(1)

     {

         key = scanKeyboard();

         switch (key) {

             case 115:

                 thread_pause();

                 break;

             case 114:

                 thread_resume();

                 break;

             case 112:

                 thread_resume();

                 break;

             default:

                 break;

         }

         

     }

 

    return 0;

 

}

 

posted on 2019-05-15 10:51  力战者  阅读(791)  评论(0编辑  收藏  举报