void MainWindow::createActions() { newAct = new QAction(tr("&Reset keyboard"), this); newAct->setShortcuts(QKeySequence::New); newAct->setStatusTip(tr("You could reset USB keyboard module")); connect(newAct, SIGNAL(triggered()), this, SLOT(reloadKeyboard())); openAct = new QAction(tr("&Get data from keyboard..."), this); openAct->setShortcuts(QKeySequence::Open); openAct->setStatusTip(tr("Grab keyboard data")); connect(openAct, SIGNAL(triggered()), this, SLOT(open())); logsAct = new QAction(tr("&Read logs"), this); logsAct->setShortcuts(QKeySequence::Open); logsAct->setStatusTip(tr("Read recent data logs")); connect(logsAct, SIGNAL(triggered()), this, SLOT(readLogs())); clearLogsAct = new QAction(tr("&Clear logs"), this); clearLogsAct->setShortcuts(QKeySequence::Open); clearLogsAct->setStatusTip(tr("Clear recent data logs")); connect(clearLogsAct, SIGNAL(triggered()), this, SLOT(truncateLogs())); exitAct = new QAction(tr("&Exit"), this); exitAct->setShortcuts(QKeySequence::Quit); exitAct->setStatusTip(tr("Exit the application")); connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); listUsersAct = new QAction(tr("&List users"), this); listUsersAct->setStatusTip(tr("Lists all keylock users")); connect(listUsersAct, SIGNAL(triggered()), this, SLOT(listUsers())); addUserAct = new QAction(tr("&Add user"), this); addUserAct->setStatusTip(tr("Ads new user")); connect(addUserAct, SIGNAL(triggered()), this, SLOT(addUser())); aboutAct = new QAction(tr("&About"), this); aboutAct->setStatusTip(tr("Show the application's About box")); connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); aboutQtAct = new QAction(tr("About &Qt"), this); aboutQtAct->setStatusTip(tr("Show the Qt library's About box")); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); connect(aboutQtAct, SIGNAL(triggered()), this, SLOT(aboutQt())); }
int main(int argc, char* argv[]) { std::ofstream logFile("serverLogFile.txt", std::ofstream::trunc); Logger::addOutput(Logger::Level::TRACE, logFile); Logger::addOutput(Logger::Level::INFO, std::cout); Logger::log(Logger::Level::INFO, "Starting server"); TweakSettings::initializeMaster(); server.initialize(); server.run(); std::string input; std::cout << "> "; while (true) { if (!std::getline(std::cin, input)) break; std::transform(input.begin(), input.end(), input.begin(), ::tolower); if (input == "exit") break; else if (input == "send") server.sendTestData(); else if (input == "help") printHelp(); else if (input == "list") listUsers(); else if (input == "games") listGames(); else if (input == "pulse") server.sendPulseObject(); else printUnknownCommand(); std::cout << "> "; } server.shutdown(); TweakSettings::shutdown(); }
int main(int argc, char **argv) { GWEN_DB_NODE *db; const char *cmd; const char *pinFile; int nonInteractive=0; const char *s; int rv; AB_BANKING *ab; GWEN_GUI *gui; const GWEN_ARGS args[]={ { GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ GWEN_ArgsType_Char, /* type */ "cfgfile", /* name */ 0, /* minnum */ 1, /* maxnum */ "C", /* short option */ "cfgfile", /* long option */ "Specify the configuration file", /* short description */ "Specify the configuration file" /* long description */ }, { GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ GWEN_ArgsType_Char, /* type */ "pinfile", /* name */ 0, /* minnum */ 1, /* maxnum */ "P", /* short option */ "pinfile", /* long option */ "Specify the PIN file", /* short description */ "Specify the PIN file" /* long description */ }, { 0, /* flags */ GWEN_ArgsType_Int, /* type */ "nonInteractive", /* name */ 0, /* minnum */ 1, /* maxnum */ "n", /* short option */ "noninteractive", /* long option */ "Select non-interactive mode",/* short description */ "Select non-interactive mode.\n" /* long description */ "This automatically returns a confirmative answer to any non-critical\n" "message." }, { 0, GWEN_ArgsType_Int, "verbosity", 0, 10, "v", 0, "Increase the verbosity level", "Increase the verbosity level" }, { GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */ GWEN_ArgsType_Char, /* type */ "charset", /* name */ 0, /* minnum */ 1, /* maxnum */ 0, /* short option */ "charset", /* long option */ "Specify the output character set", /* short description */ "Specify the output character set" /* long description */ }, { GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */ GWEN_ArgsType_Int, /* type */ "help", /* name */ 0, /* minnum */ 0, /* maxnum */ "h", /* short option */ "help", /* long option */ "Show this help screen", /* short description */ "Show this help screen" /* long description */ } }; rv=GWEN_Init(); if (rv<0) { fprintf(stderr, "ERROR: Unable to init GWEN (%d).\n", rv); return 2; } GWEN_Logger_Open("aqpaypal-tool", "aqpaypal-tool", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User); GWEN_Logger_SetLevel("aqpaypal-tool", GWEN_LoggerLevel_Warning); GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning); db=GWEN_DB_Group_new("arguments"); rv=GWEN_Args_Check(argc, argv, 1, GWEN_ARGS_MODE_ALLOW_FREEPARAM | GWEN_ARGS_MODE_STOP_AT_FREEPARAM, args, db); if (rv==GWEN_ARGS_RESULT_ERROR) { fprintf(stderr, "ERROR: Could not parse arguments main\n"); return -1; } else if (rv==GWEN_ARGS_RESULT_HELP) { GWEN_BUFFER *ubuf; ubuf=GWEN_Buffer_new(0, 1024, 0, 1); GWEN_Buffer_AppendString(ubuf, I18N("Usage: ")); GWEN_Buffer_AppendString(ubuf, argv[0]); GWEN_Buffer_AppendString(ubuf, I18N(" [GLOBAL OPTIONS] COMMAND " "[LOCAL OPTIONS]\n")); GWEN_Buffer_AppendString(ubuf, I18N("\nGlobal Options:\n")); if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) { fprintf(stderr, "ERROR: Could not create help string\n"); return 1; } GWEN_Buffer_AppendString(ubuf, I18N("\nCommands:\n\n")); GWEN_Buffer_AppendString(ubuf, I18N(" listusers:\n" " blurb " "file\n\n")); GWEN_Buffer_AppendString(ubuf, I18N(" listaccounts:\n" " blurb " "file\n\n")); GWEN_Buffer_AppendString(ubuf, I18N(" adduser:\n" " blurb " "file\n\n")); GWEN_Buffer_AppendString(ubuf, I18N(" addaccount:\n" " blurb " "file\n\n")); GWEN_Buffer_AppendString(ubuf, I18N(" setsecret:\n" " blurb " "file\n\n")); fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf)); GWEN_Buffer_free(ubuf); return 0; } if (rv) { argc-=rv-1; argv+=rv-1; } cmd=GWEN_DB_GetCharValue(db, "params", 0, 0); if (!cmd) { fprintf(stderr, "ERROR: Command needed.\n"); return 1; } gui=GWEN_Gui_CGui_new(); s=GWEN_DB_GetCharValue(db, "charset", 0, "ISO-8859-15"); GWEN_Gui_SetCharSet(gui, s); nonInteractive=GWEN_DB_GetIntValue(db, "nonInteractive", 0, 0); if (nonInteractive) GWEN_Gui_AddFlags(gui, GWEN_GUI_FLAGS_NONINTERACTIVE); else GWEN_Gui_SubFlags(gui, GWEN_GUI_FLAGS_NONINTERACTIVE); pinFile=GWEN_DB_GetCharValue(db, "pinFile", 0, NULL); if (pinFile) { GWEN_DB_NODE *dbPins; dbPins=GWEN_DB_Group_new("pins"); if (GWEN_DB_ReadFile(dbPins, pinFile, GWEN_DB_FLAGS_DEFAULT | GWEN_PATH_FLAGS_CREATE_GROUP)) { fprintf(stderr, "Error reading pinfile \"%s\"\n", pinFile); return 2; } /* set argument "persistent" to one in non-interactive mode */ GWEN_Gui_CGui_SetPasswordDb(gui, dbPins, nonInteractive); } GWEN_Gui_SetGui(gui); ab=AB_Banking_new("aqpaypal-tool", GWEN_DB_GetCharValue(db, "cfgfile", 0, 0), 0); AB_Gui_Extend(gui, ab); if (strcasecmp(cmd, "listusers")==0) { rv=listUsers(ab, db, argc, argv); } else if (strcasecmp(cmd, "listaccounts")==0) { rv=listAccounts(ab, db, argc, argv); } else if (strcasecmp(cmd, "adduser")==0) { rv=addUser(ab, db, argc, argv); } else if (strcasecmp(cmd, "addaccount")==0) { rv=addAccount(ab, db, argc, argv); } else if (strcasecmp(cmd, "setsecrets")==0) { rv=setSecrets(ab, db, argc, argv); } else { fprintf(stderr, "ERROR: Unknown command \"%s\".\n", cmd); rv=1; } return rv; }
////////////////////////////////////////////////// //聊天 int saySth() { char buf[100]; //临时输入区 char gMsg[MSGLEN]; command com; user *cur=NULL; int remainder; //扩展区剩余空间 int pos; //扩展区的当前输入位置 int who, temp, count; int sended; //标记此包是否已经发过 user *pusers[50]; printf("\n*Talking mode\n" "*Continuous Enter to send msg,\n" "*Ctrl+D to quit conversation.\n"); pthread_mutex_lock(&usrMutex); count = listUsers(pusers, &userList, sizeof(pusers)/sizeof(pusers[0]), 0); // 每个用户都打上use标记? pthread_mutex_unlock(&usrMutex); who = inputNo(1, count, 1, "Please input user No.[1]:"); // 怎么参数里面还有需要打印的字符串 if (who>0) { cur = pusers[who-1]; initCommand(&com, IPMSG_SENDMSG|IPMSG_SENDCHECKOPT); memcpy(&com.peer, &cur->peer, sizeof(com.peer)); remainder = MSGLEN; pos = 0; sended = 1; while(1) { printf("(talking with %s):", cur->name); if (fgets(buf, sizeof(buf), stdin)==NULL) break; if (buf[0] != '\n') { strncpy(com.additional+pos, buf, remainder); temp = strlen(com.additional+pos); pos += temp; remainder -= temp; sended = 0; } if ((buf[0]=='\n') || (remainder<=1)) { if (!sended) { com.packetNo = (unsigned int)time(NULL); sendMsg(&com); sended = 1; printf("Message sent.\n"); } remainder = sizeof(com.additional); // 这里为什么要重新获取缓冲区大小 pos = 0; } } } puts("\nEnd conversation.\n"); pthread_mutex_lock(&usrMutex); unListUsers(pusers, count); // 清楚use标记 pthread_mutex_unlock(&usrMutex); }
int selectFiles() { command com; user *cur=NULL; int who, count, flag, fileType; unsigned int fileNo; char fileName[FILENAME]; struct stat fileAttr; char *strtmp; filenode *fntmp, *head, *tail; gsNode *newTask; user *pusers[50]; printf("\n*Sending mode\n" "*Continuous Enter to send file,\n" "*Ctrl+D to quit.\n"); pthread_mutex_lock(&usrMutex); count = listUsers(pusers, &userList, sizeof(pusers)/sizeof(pusers[0]), 0); pthread_mutex_unlock(&usrMutex); who = inputNo(1, count, 1, "Please input user No.[1]:"); if (who>0) { cur = pusers[who-1]; initCommand(&com, IPMSG_SENDMSG|IPMSG_FILEATTACHOPT); memcpy(&com.peer, &cur->peer, sizeof(com.peer)); printf("To send file to %s(%s).\nPlease select file to send:\n", cur->name, cur->host); newTask = (gsNode*)malloc(sizeof(gsNode)); initGsNode(newTask); newTask->packetNo = com.packetNo; fileNo = 0; head = com.fileList; tail = com.fileList; while (1) { if (fgets(fileName, FILENAME, stdin) == NULL) //取消传输 { free(newTask); newTask = NULL; while (head!=NULL) { tail = head; head = head->next; free(tail); } break; } transfStr(fileName, 0); //去除前后空白字符 if (fileName[0]=='\0') break; if (lstat(fileName, &fileAttr)<0) { printf("Get file attributes error.\n"); continue; } if (S_ISREG(fileAttr.st_mode)) fileType = 1; else if (S_ISDIR(fileAttr.st_mode)) fileType = 2; else { fileType = -1; printf("Unsupported file type.\n"); continue; } if (tail == NULL) head = tail = (filenode*)malloc(sizeof(filenode)); else { tail->next = (filenode*)malloc(sizeof(filenode)); tail = tail->next; } tail->next = NULL; tail->fileNo = fileNo; strncpy(tail->fileName, fileName, sizeof(tail->fileName)); snprintf(tail->fileSize, sizeof(tail->fileSize), "%x", fileAttr.st_size); snprintf(tail->mtime, sizeof(tail->mtime), "%x", fileAttr.st_mtime); tail->fileType = fileType; fileNo++; } // 可以连续发送多个文件? if (head==NULL) { if (newTask!=NULL) free(newTask); } else { newTask->fileList.next = com.fileList = head; pthread_mutex_lock(&sendFMutex); //lock newTask->next = sendFHead.next; sendFHead.next = newTask; pthread_mutex_unlock(&sendFMutex); //unlock if (newTask->fileList.next!=NULL) { sendMsg(&com); //可以放lock外面 printf("\nWaiting to transfer.\n"); } } } pthread_mutex_lock(&usrMutex); unListUsers(pusers, count); pthread_mutex_unlock(&usrMutex); }
int main(void) { fd_set master; // master file descriptor list fd_set read_fds; // temp file descriptor list for select() int fdmax; // maximum file descriptor number int listener; // listening socket descriptor int newfd; // newly accept()ed socket descriptor struct sockaddr_storage remoteaddr; // client address socklen_t addrlen; char buf[256]; // buffer for client data uint32_t recvId; int nbytes; int talking[100]= {0}; //talk to who pairs char remoteIP[INET6_ADDRSTRLEN]; int yes=1; // for setsockopt() SO_REUSEADDR, below int i, j, rv; struct addrinfo hints, *ai, *p; FD_ZERO(&master); // clear the master and temp sets FD_ZERO(&read_fds); // get us a socket and bind it memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) { fprintf(stderr, "selectserver: %s\n", gai_strerror(rv)); exit(1); } for(p = ai; p != NULL; p = p->ai_next) { listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if (listener < 0) { continue; } // lose the pesky "address already in use" error message setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) { close(listener); continue; } break; } // if we got here, it means we didn't get bound if (p == NULL) { fprintf(stderr, "selectserver: failed to bind\n"); exit(2); } freeaddrinfo(ai); // all done with this // listen if (listen(listener, 10) == -1) { perror("listen"); exit(3); } printf("server: waiting for connections...\n"); // add the listener to the master set FD_SET(listener, &master); // keep track of the biggest file descriptor fdmax = listener; // so far, it's this one // main loop for(;;) { read_fds = master; // copy it if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) { perror("select"); exit(4); } // run through the existing connections looking for data to read for(i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &read_fds)) { // we got one!! if (i == listener) { // handle new connections addrlen = sizeof remoteaddr; newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen); if (newfd == -1) { perror("accept"); } else { FD_SET(newfd, &master); // add to master set if (newfd > fdmax) { // keep track of the max fdmax = newfd; } printf("selectserver: new connection from %s on " "socket %d\n", inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd); //ask who to talk to char *str="you're online now\n"; send(newfd, str, strlen(str), 0); //show list //send mesage to someone } } else { // handle data from a client //recv //switch cmd //show -> list all //talk -> get id, and pair with each other if ((nbytes = recv(i, &buf, sizeof buf, 0)) <= 0) { // got error or connection closed by client if (nbytes == 0) { // connection closed printf("selectserver: socket %d hung up\n", i); } else { perror("recv"); } close(i); // bye! FD_CLR(i, &master); // remove from master set } else { // split cmd char *cmd; cmd= strtok(buf, " "); if (strncmp(cmd, "show",4) == 0) { listUsers(i,j, fdmax, master, listener); } else if (strncmp(cmd, "talk",4) == 0) { cmd= strtok(NULL, " "); //get the userId int userId= atoi(cmd); //get the msg char *msg= strtok(NULL, " "); //send the msg if (FD_ISSET(userId, &master)) { // except the listener and ourselves if (send(userId, msg, strlen(msg), 0) == -1) { perror("send"); } } } else { } } } // END handle data from client } // END got new incoming connection } // END looping through file descriptors } // END for(;;)--and you thought it would never end! return 0; }