Application::Application() : mTransferWindow(&mTransferModel), #ifdef BUILD_APPINDICATOR mIcon(Platform::useIndicator() ? static_cast<Icon*>(new IndicatorIcon) : static_cast<Icon*>(new TrayIcon)), #else mIcon(new TrayIcon), #endif mStartTime(QDateTime::currentMSecsSinceEpoch()) { connect(&mDeviceModel, &DeviceModel::rowsInserted, this, &Application::notifyDevicesAdded); connect(&mDeviceModel, &DeviceModel::rowsAboutToBeRemoved, this, &Application::notifyDevicesRemoved); connect(&mTransferModel, &TransferModel::dataChanged, this, &Application::notifyTransfersChanged); connect(&mTransferServer, &TransferServer::error, this, &Application::notifyError); connect(&mTransferServer, &TransferServer::newTransfer, &mTransferModel, &TransferModel::addReceiver); #ifdef BUILD_UPDATE_CHECKER connect(&mUpdateChecker, &UpdateChecker::newVersion, this, &Application::notifyNewVersion); #endif mIcon->addAction(tr("Send Files..."), this, SLOT(sendFiles())); mIcon->addAction(tr("Send Directory..."), this, SLOT(sendDirectory())); mIcon->addSeparator(); mIcon->addAction(tr("View Transfers..."), &mTransferWindow, SLOT(show())); mIcon->addSeparator(); mIcon->addAction(tr("Settings"), this, SLOT(onOpenSettings())); mIcon->addSeparator(); mIcon->addAction(tr("About..."), this, SLOT(onOpenAbout())); mIcon->addAction(tr("About Qt..."), this, SLOT(onOpenAboutQt())); mIcon->addSeparator(); mIcon->addAction(tr("Exit"), QApplication::instance(), SLOT(quit())); // Start the transfer server mTransferServer.start(); }
void* firstLineThread(void* args) { MyThreadArgs* mta = (MyThreadArgs*) args; //get the arguments int sock = mta->getSock(); //set them to local variables MyQueue* mq = mta->getQueue(); char buff[PATH_MAX + 1]; //set a buffer for the name memset(buff, '\0', PATH_MAX + 1); //initialise it readName(sock, buff); //read the name of the request if(!mq->increaseFirstLineThreads()) //if you can, increase the number of workerThreads { //if not, it means it's no longer active, so sendNumber(sock, 0); //send a negative response sendNumber(sock, 0); if(close(sock) == -1) //close the socket perror("close"); delete mta; //free the memory pthread_detach(pthread_self()); //detach the thread pthread_exit(NULL); //and exit } if(strcmp(buff, "exit") == 0) //if you receive exit command { sendNumber(sock, 0); //send negative response sendNumber(sock, 0); mq->deactivate(); //deactivate the queue mq->decreaseFirstLineThreads(); //decrease the active worker threads if(close(sock) == -1) //close the socket perror("close"); pthread_t pt; //create the terminator pthread_create(&pt, NULL, terminator, args); pthread_detach(pthread_self()); //detach yourself pthread_exit(NULL); //and exit } delete mta; //free the memory of arguments you no longer need MyINodeList myFiles, myDirectories; if(buff[0] == '/') //check if the path is relative or absolute getDirectoryItems(buff, "", &myFiles, &myDirectories); //and fill the lists with files and directories else getDirectoryItems(buff, "./", &myFiles, &myDirectories); int numberOfFiles = myFiles.length(); //get the number of files contained int numberOfDirectories = myDirectories.length(); //and the number of directories std::cout << "[" << pthread_self() << "]: " << myDirectories.length() << " directories containing " << myFiles.length() << " files to be sent to: " << sock << std::endl << std::endl; //let the user know sendNumber(sock, numberOfDirectories); //send the number of directories to the client sendNumber(sock, numberOfFiles); //and the number of files while(!myDirectories.isEmpty()) //send the directories to the client { sendDirectory(sock, myDirectories.getTop()); myDirectories.removeTopEntry(); } pthread_mutex_t* m = new pthread_mutex_t; //create a mutex for this client int* i = new int; //and a counter for jobs done *i = 0; pthread_mutex_init(m, NULL); //initialize the mutex while(!myFiles.isEmpty()) { MyINode* mind = myFiles.popTop(); //print info std::cout << "[" << pthread_self() << "]: " << "Adding <" << mind->getName() << ", " << sock << "> to queue." << std::endl; MyJob* mj = new MyJob(mind, sock, m, i, numberOfFiles); //create jobs mq->insertEntry(mj); //add them to the queue } mq->decreaseFirstLineThreads(); //decrease active FirstLineThreads pthread_detach(pthread_self()); //detach yourself pthread_exit(NULL); //and exit }
//Main will read in port number to pass to "createconnection", //then send the successful connection to the chatFunction loop. int main(int argc, char * argv[]) { int new_fd; //int dp; int data_fd; int numbytes; char commandIn[MAXCOMMAND]; char tempCommand[MAXCOMMAND]; char* command; char* hostname; char* data_port; char* filename; char* temp; const char delimeters[] = ","; if (argc != 2) { fprintf(stderr,"usage: server <portnumber>\n"); exit(1); } //Connect to client new_fd = createConnection(argv[1]); //Error if failed receipt if ((numbytes = recv(new_fd, commandIn, MAXCOMMAND-1,0)) == -1) { printf("Receipt failed"); perror("recv"); exit(1); } commandIn[numbytes] = '\0'; bzero(tempCommand, MAXCOMMAND); strcpy(tempCommand, commandIn); printf("Server: Received command: %s\n", tempCommand); command = strtok(tempCommand, delimeters); hostname = strtok(NULL, delimeters); data_port = strtok(NULL, delimeters); filename = strtok(NULL, delimeters); //dp = atoi(data_port); //printf("\nPlease type message below after the prompt.\n"); //printf("Type '\\quit' to end chat connection.\n\n"); if(strcmp(command, "-l") == 0) { //directoryListing(dataConnection_fd); printf("directory list control flow\n"); if(send(new_fd, "Command '-l' received. Establishing Data connection\n", 55, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } data_fd = createDataConnection(data_port); sendDirectory(data_fd); } else if(strcmp(command, "-g") == 0) { printf("sendfile control flow\n"); if(send(new_fd, "Command '-g' received. Establishing Data connection\n", 55, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } data_fd = createDataConnection(data_port); sendFile(data_port, filename); //sendRequestedFile(dataConnection_fd, requestedFile); } else { if(send(new_fd, "Invalid command. Closing connection.\n", 38, 0) == -1) { printf("Problem Establishing connetion.\n"); perror("send"); exit(1); } close(new_fd); printf("Command not recognized\n Closing connection.\n"); perror("Invalid Command"); return 1; } printf("Command: %s\n", command); printf("hostname: %s\n", hostname); printf("data_port: %s\n", data_port); //Enter chat loop with client handle and file descriptor for current connection //chatFunction(new_fd, clientHandle); return 0; }