//--------------------------------------------------------------------------- int CSendHost_Imgur::Send() { if (!g_hNetlibUser) { /// check Netlib Error(SS_ERR_INIT, m_pszSendTyp); Exit(ACKRESULT_FAILED); return !m_bAsync; } memset(&m_nlhr, 0, sizeof(m_nlhr)); char* tmp; tmp = mir_t2a(m_pszFile); HTTPFormData frm[] = { { "Authorization", HTTPFORM_HEADER("Client-ID 2a7303d78abe041") }, { "image", HTTPFORM_FILE(tmp) }, // {"name",""},// filename (detected if multipart / form-data) // {"title",""}, // {"description",""}, }; int error = HTTPFormCreate(&m_nlhr, REQUEST_POST, "https://api.imgur.com/3/image", frm, sizeof(frm) / sizeof(HTTPFormData)); mir_free(tmp); if (error) return !m_bAsync; /// start upload thread if (m_bAsync) { mir_forkthread(&CSendHost_Imgur::SendThread, this); return 0; } SendThread(this); return 1; }
void Sender::startSending() { std::thread SendThread( [&]() { do { try { IMessage *msg = sendQ.deQ(); /*Message* msg = dynamic_cast<Message*>(imsg); map<string, string> header = msg->getHeader();*/ if (msg->getCommand() == "send_stop") break; else if (msg->getCommand() == "file_upload") { if (connectToPeer(msg->getRecvIP(), stoi(msg->getRecvPort()))) { if (sendFile(msg)) { } } else Verbose::show("Connection failed!\n"); } else if (msg->getCommand() == "ack") { if (connectToPeer(msg->getRecvIP(), stoi(msg->getRecvPort()))) sendHeader(msg); } else { if (connectToPeer(msg->getRecvIP(), stoi(msg->getRecvPort()))) { sendHeader(msg); sendBody(msg); } } } catch (exception ex) { string s = ex.what(); Verbose::show("\n In send Thread: " + s); } } while (1); } ); SendThread.detach(); }
int main() { try { Sender sndr("8080","127.0.0.1" , "file", "hey.txt"); std::thread SendThread(&StartSenderThread::run, StartSenderThread(), sndr,ip, DestPort); std::cout << " Sender Port : " << ListPort; SendThread.join(); } catch (std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; } std::cout << "\n\n"; return 0; }
///////////////////// // MAIN ///////////// int main(int argc, char *argv[]) { int rv, i, j; long q; //Used for threading int rc; long t; // Check arguments and exit if they are not well constructed check_args(argc, argv); for(i=0; i<100; i++) { ackMessages[i] = -1; messageTimes[i] = -1; portOrder[i] = -1; socketOrder[i] = -1; outgoing[i] = -1; } queueLen = 0; queuePos = 0; outLen = 0; outPos = 0; port=argv[1]; for(i=0; i<argc-2; i++) { ports[i]=argv[i+2]; } pthread_mutex_init(&mutexsum, NULL); //Create sorted array of ports t = (long) (argc-2)/2; portOrder[0] = atoi(port); for(i=0; i<t; i++) { portOrder[i+1] = atoi(ports[i*2+1]); j = i; while(portOrder[j+1] < portOrder[j] && j >= 0) { q = portOrder[j+1]; portOrder[j+1] = portOrder[j]; portOrder[j] = q; j--; } } // Fill the 'hints' structure memset( &hints, 0, sizeof(hints) ); hints.ai_flags = AI_PASSIVE; // Use the local IP address hints.ai_family = AF_INET; // Use IPv4 address hints.ai_socktype = SOCK_STREAM; // TCP sockets // Get the address information of the server ('NULL' uses the local address ) if ((rv = getaddrinfo( NULL , argv[1], &hints, &serverInfo )) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // Make a socket (identified with a socket file descriptor) if ((socketFD = socket( serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol )) == -1) perror("gateway: socket"); // Bind to the specified port (in getaddrinfo()) if (bind( socketFD, serverInfo->ai_addr, serverInfo->ai_addrlen ) == -1) { close(socketFD); perror("prog1: bind"); } // This is only used to be able to reuse the same port int yes = 1; setsockopt( socketFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) ); // Start to listen (only for TCP) if((listen(socketFD, LISTEN_QUEUE))== -1) { perror("prog1: listen"); exit(1); } printf("Listening in port %s...", argv[1]); fflush(stdout); //initialize Queue for(q=0; q<NUM_THREADS; q++) { myQueue[q]=-1; } //Creating new thread for listening q=0; for(i=0; i<t; i++) { //check to see if any threads available go = 1; while(go==1) { for(q=0; q<NUM_THREADS; q++) { if(myQueue[q]==-1) { break; } } if( q==NUM_THREADS && go==1){ sleep(1); continue; } go=0; } rc = pthread_create(&threads[q], NULL, ListenThread, (void *)q); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } //Wait for user to hit ENTER printf("\nHit ENTER before the coordinator."); fflush(stdout); while(getchar() != '\n') { //wait for it... } for(i=0; i<t; i++) { // Get the address information of the server. if ((rv = getaddrinfo(ports[i*2], ports[i*2+1], &hints, &serverInfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return; } // Create a socket to communicate with the server if ((sockets[i] = socket( serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol )) == -1) perror("clientTCP: socket"); // Connect to the server if((connect(sockets[i], serverInfo->ai_addr, serverInfo->ai_addrlen)) == -1) { perror("Socket connect: connect"); } //Put the socket in its sorted position for(j=0; j<100; j++) { if(portOrder[j] == atoi(ports[i*2+1])) { socketOrder[j] = sockets[i]; break; } } } //Rank port numbers for time scaling rank = rankPorts(t); int caseCount = 1; while (1) { printf("\n\nCase %i\n",caseCount); printf("Waiting for VOTE_REQUEST from coordinator.\n"); //Wait for a request from the coordinator requestReceived = 0; for(i=0; i<10; i++) { if(requestReceived != 0) break; sleep(1); } if(requestReceived != 0) { if(caseCount == 2 && atoi(port) %2 == 0) //Fill this in to determine if COMMIT or ABORT is sent. { printf("Sending VOTE_COMMIT message.\n"); for(i=0; i<t; i++) { SendThread(sockets[i], VOTE_COMMIT); } } else { printf("Sending VOTE_ABORT message.\n"); for(i=0; i<t; i++) { SendThread(sockets[i], VOTE_ABORT); } } } else { printf("Timed out. Sending VOTE_ABORT message.\n"); for(i=0; i<t; i++) { SendThread(sockets[i], VOTE_ABORT); } } //Wait for a decision from the coordinator printf("Waiting for a decision from the coordinator.\n"); decisionReceived = 0; for(i=0; i<10; i++) { if(decisionReceived != 0) break; sleep(1); } if(decisionReceived == GLOBAL_COMMIT) { printf("Committed.\n"); continue; } else if(decisionReceived == GLOBAL_ABORT) { printf("Aborted.\n"); continue; } else { } caseCount++; sleep(60); } // Close the socket that was used to listen close(socketFD); /* Last thing that main() should do */ pthread_exit(NULL); return EXIT_SUCCESS; }
//--------------------------------------------------------------------------- void CSendImageShack::Send() { // check Netlib if( !hNetlibUser ) { //PrintError(1,TRUE); return; } if (!m_pszFileName) { m_pszFileName = (LPSTR)GetFileName(m_pszFile, DBVT_ASCIIZ); } if (!m_pszContentType) GetContentType(); // create new boundary MFDR_Reset(); // initialize the netlib request ZeroMemory(&m_nlhr, sizeof(m_nlhr)); m_nlhr.cbSize = sizeof(m_nlhr); m_nlhr.requestType = REQUEST_POST; m_nlhr.flags = NLHRF_HTTP11; //NLHRF_DUMPASTEXT; m_nlhr.szUrl = "http://www.imageshack.us/upload_api.php"; m_nlhr.headersCount = 6; { //NETLIBHTTPHEADER start m_nlhr.headers=(NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*m_nlhr.headersCount); m_nlhr.headers[0].szName = "Referer"; m_nlhr.headers[0].szValue = "http://www.imageshack.us/upload_api.php"; m_nlhr.headers[1].szName = "Connection"; m_nlhr.headers[1].szValue = "Keep-alive"; m_nlhr.headers[2].szName = "AcceptLanguage"; m_nlhr.headers[2].szValue = "en-us, pt-br"; m_nlhr.headers[3].szName = "Host"; m_nlhr.headers[3].szValue = "imageshack.us"; m_nlhr.headers[4].szName = "User-Agent"; m_nlhr.headers[4].szValue = __USER_AGENT_STRING; //szAgent; /; //nlhr.headers[x].szName = "Authorization"; //nlhr.headers[x].szValue = auth; //Basic base-64-authorization //$header .= "Content-type: multipart/form-data; boundary=" . part::getBoundary() . "\r\n"; mir_snprintf(m_nlheader_ContentType, SIZEOF(m_nlheader_ContentType), "multipart/form-data; boundary=%s", m_MFDRboundary); m_nlhr.headers[m_nlhr.headersCount-1].szName = "Content-Type"; m_nlhr.headers[m_nlhr.headersCount-1].szValue = m_nlheader_ContentType; } //NETLIBHTTPHEADER end //POST DATA file-header, init DATA with MultipartFormDataRequest //$params[] = new filepart('fileupload', $file, basename($file), $contentType, 'iso-8859-1'); //($this->sendStart($h);) AppendToData("--"); AppendToData(m_MFDRboundary); AppendToData("\r\n"); //($this->sendDispositionHeader($h);) AppendToData("Content-Disposition: form-data; name=\""); AppendToData("fileupload"); AppendToData("\"; filename=\""); AppendToData(m_pszFileName); AppendToData("\""); AppendToData("\r\n"); //($this->sendContentTypeHeader($h);) AppendToData("Content-Type: "); AppendToData(m_pszContentType); AppendToData("; charset="); AppendToData("iso-8859-1"); //($this->sendEndOfHeader($h);) AppendToData("\r\n"); AppendToData("\r\n"); //Now we add the file binary ($this->sendData($h)) FILE * fileId = _tfsopen(m_pszFile, _T("rb"), _SH_DENYWR ); if( !fileId) { //PrintError(1,TRUE); return; } fseek(fileId, NULL, SEEK_END); size_t lenFile = ftell(fileId); size_t sizeDest = sizeof(char)*(m_nlhr.dataLength + lenFile + 1); m_nlhr.pData = (char *) mir_realloc(m_nlhr.pData, sizeDest); fseek(fileId, NULL, SEEK_SET ); int i; int ch = fgetc( fileId ); for( i=0; (i < (int)lenFile ) && ( feof( fileId ) == 0 ); i++ ) { m_nlhr.pData[m_nlhr.dataLength+i] = (char)ch; ch = fgetc( fileId ); } m_nlhr.pData[sizeDest-1] = 0; //NULL Termination for binary data m_nlhr.dataLength = (int)sizeDest - 1; fclose(fileId); //($this->sendEnd($h);) AppendToData("\r\n"); //POST DATA footer (for "optimage", 1) //POST DATA footer (for "optsize", optsize) //POST DATA footer (for "tags", tags) //POST DATA footer (for "rembar", "yes" : "no") //POST DATA footer (for "public", "yes" : "no") //POST DATA footer (for "cookie", cookie) //POST DATA footer (for "key", DEVKEY_IMAGESHACK) //($this->sendStart($h);) AppendToData("--"); AppendToData(m_MFDRboundary); AppendToData("\r\n"); //($this->sendDispositionHeader($h);) AppendToData("Content-Disposition: form-data; name=\""); AppendToData("key"); AppendToData("\""); //($this->sendTransferEncodingHeader($h); ) AppendToData("\r\n"); AppendToData("Content-Transfer-Encoding: "); AppendToData("8bit"); //??"binary" //($this->sendEndOfHeader($h);) AppendToData("\r\n"); AppendToData("\r\n"); //($this->sendData($h);) AppendToData(DEVKEY_IMAGESHACK); //($this->sendEnd($h);) AppendToData("\r\n"); //POST DATA Exit //$postdata = "--" . part::getBoundary() . "--\r\n"; AppendToData("--"); AppendToData(m_MFDRboundary); AppendToData("--\r\n"); //start upload thread if (m_SendSync) { m_bFreeOnExit = FALSE; SendThread(); return; } m_bFreeOnExit = TRUE; mir_forkthread(&CSendImageShack::SendThreadWrapper, this); }