int main(void) { struct sockaddr_in serverend; socklen_t len; int sd, ret, shmid, epfd; struct epoll_event revents; len = sizeof(serverend); pid_t pid; serverend.sin_family = AF_INET; serverend.sin_port = htons(SERVERPORT); inet_pton(AF_INET, SERVERADDR, &serverend.sin_addr); sd = socket(AF_INET, SOCK_STREAM, 0); if (-1 == sd) { perror("socket"); exit(1); } ret = bind(sd, (struct sockaddr *)&serverend, len); if (-1 == ret) { perror("bind"); exit(1); } ret = listen(sd, 30); ret = fcntl(sd, F_GETFL); ret = fcntl(sd, F_SETFL, ret | O_NONBLOCK); if (-1 == ret) printf("listen\n"); desory_zmobe(); Prolib_t *lib = create_dynamic_process_libs(sd, 3); epfd = InitEpoll(sd); shmid = init_shmid(sd); while (1) { ret = epoll_wait(epfd, &revents, 1, -1); if (ret >= 0) { if (revents.events & EPOLLIN) { printf("new connect is coming!, ret: %d\n", ret); ret = assign_job(lib, 3, task); if (-1 == ret) new_worker(lib, 3, shmid); } } } }
static void sent_build_stats(void *arg) { struct build_peer *peer = arg; free(peer->buf); peer->buf = NULL; assign_job(peer); }
int main(void) { int i, ret, serverport; int pid_num = PORT_NUM; pid_t pid; int tmp = 0; int IsFirst = 1; double curtimeval, nexttimeval; Prolib_t *lib = create_dynamic_process_libs(pid_num); struct timeval CurPval, NextPval; Init_TimerValue(&curtimeval, &nexttimeval); while (1) { serverport = generate_dynamic_RandPort(); for (i = 0; i < PORT_NUM; i++) { tmp = serverport + i; assign_job(lib, i, tmp); } #if DEBUG printf("task asign over!\n"); #endif if (IsFirst == 1) { #if DEBUG CurPval.tv_sec = 30; #else CurPval.tv_sec = curtimeval; #endif CurPval.tv_usec = 0; select(0, NULL, NULL, NULL, &CurPval); IsFirst = 0; } else if (IsFirst == 0) { #if DEBUG NextPval.tv_sec = 60; #else NextPval.tv_sec = nexttimeval; #endif NextPval.tv_usec = 0; select(0, NULL, NULL, NULL, &NextPval); } } return 0; }
static void finish_job(void *arg) { struct scan_peer *peer = arg; if (strlen(peer->job->scan_output) != peer->output_len) { warnx("Invalid output len received from peer"); kill_peer(peer); return; } LIST_REMOVE(peer, peer_link); process_job(peer->job, JOB_DONE); assign_job(peer); }
static void kill_peer(void *arg) { struct build_peer *peer = arg; (void)close(peer->fd); LIST_REMOVE(peer, peer_link); if (peer->job != NULL) process_job(peer->job, JOB_OPEN, 1); free(peer->buf); free(peer); peer = LIST_FIRST(&unassigned_peers); if (peer != NULL) assign_job(peer); }
static void kill_peer(void *arg) { struct scan_peer *peer = arg; (void)close(peer->fd); LIST_REMOVE(peer, peer_link); free(peer->job->scan_output); peer->job->scan_output = NULL; process_job(peer->job, JOB_OPEN); free(peer); peer = LIST_FIRST(&inactive_peers); if (peer == NULL) return; LIST_REMOVE(peer, peer_link); assign_job(peer); }
static void finish_job(void *arg) { struct build_peer *peer = arg; LIST_REMOVE(peer, peer_link); if (peer->tmp_buf[0] == 'D') process_job(peer->job, JOB_DONE, 1); else if (peer->tmp_buf[0] == 'F') process_job(peer->job, JOB_FAILED, 1); else kill_peer(peer); peer->job = NULL; recv_command(peer); peer = LIST_FIRST(&unassigned_peers); if (peer != NULL) assign_job(peer); }
static void assign_job(void *arg) { struct build_peer *peer = arg; size_t build_info_len; uint32_t net_build_info_len; if (peer->tmp_buf[0] == 'S') { send_build_stats(peer); return; } if (peer->tmp_buf[0] != 'G') { kill_peer(peer); return; } LIST_REMOVE(peer, peer_link); peer->job = get_job(); if (peer->job == NULL) { LIST_INSERT_HEAD(&unassigned_peers, peer, peer_link); if (LIST_EMPTY(&active_peers)) event_loopexit(NULL); return; } LIST_INSERT_HEAD(&active_peers, peer, peer_link); build_info_len = peer->job->end - peer->job->begin; if (build_info_len > 0xffffff) errx(1, "Build info too long"); net_build_info_len = htonl(build_info_len); (void)memcpy(peer->tmp_buf, &net_build_info_len, 4); deferred_write(peer->fd, peer->tmp_buf, 4, peer, send_build_info, kill_peer); peer = LIST_FIRST(&unassigned_peers); if (peer != NULL) assign_job(peer); }
static void listen_handler(int sock, void *arg) { struct scan_peer *peer; struct sockaddr_in src; socklen_t src_len; int fd; src_len = sizeof(src); if ((fd = accept(sock, (struct sockaddr *)&src, &src_len)) == -1) { warn("Could not accept connection"); return; } if (set_nonblocking(fd) == -1) { (void)close(fd); warn("Could not set non-blocking IO"); return; } peer = xmalloc(sizeof(*peer)); peer->fd = fd; assign_job(peer); }
static void child_handler(struct signal_event *ev) { struct scan_peer *peer; int status; if (waitpid(child_pid, &status, WNOHANG) == -1) { if (errno == ECHILD) return; err(1, "Could not wait for child"); } if (status != 0) err(1, "Start script failed"); clients_started = 1; signal_del(ev); while ((peer = LIST_FIRST(&inactive_peers)) != NULL) { LIST_REMOVE(peer, peer_link); assign_job(peer); if (peer-> job == NULL) break; } }
static void recv_output(void *arg) { struct scan_peer *peer = arg; uint32_t output_len; (void)memcpy(&output_len, peer->tmp_buf, 4); output_len = ntohl(output_len); if (output_len == 0) { LIST_REMOVE(peer, peer_link); process_job(peer->job, JOB_DONE); assign_job(peer); return; } if (output_len == 0xffffff) { warnx("Invalid output len received from peer"); kill_peer(peer); return; } peer->job->scan_output = xmalloc(output_len + 1); peer->job->scan_output[output_len] = '\0'; peer->output_len = output_len; deferred_read(peer->fd, peer->job->scan_output, output_len, peer, finish_job, kill_peer); }
int _tmain (int argc, TCHAR* argv[]) { if (argc < 2) { _tprintf(_T("Usage:\n")); _tprintf(_T("%s max_filename [manager [port]]\n"),argv[0]); return 1; } //-- Come up with a temporary path for creating archives if (!ExpandEnvironmentStrings(_T("%TEMP%"),work_path,MAX_PATH)) { if (!ExpandEnvironmentStrings(_T("%TMP%"),work_path,MAX_PATH)) { if (!ExpandEnvironmentStrings(_T("%HOMEPATH%"),work_path,MAX_PATH)) _tcscpy(work_path,_T("c:\\")); } } if (work_path[_tcslen(work_path) - 1] != _T('\\')) _tcscat(work_path,_T("\\")); //-- Creates new manager access module instance MaxNetManager* manager = CreateManager(); if (manager) { try { //-- Setup default manager port short port = DF_MGRPORT; //-- Placeholder for manager name TCHAR managername[MAX_PATH]; //-- If we have a port defined, use it if (argc > 3) port = _ttoi(argv[3]); //-- If we have a host name (or ip address) for the manager, use it if (argc > 2) _tcscpy(managername,argv[2]); else { //-- Otherwise, find out who is the manager (if any) // The API will broadcast a message looking for a // manager using the given port. If a manager is // found, its name/ip address will be placed in // "managername" below. if (!manager->FindManager(port,managername)) { _tprintf(_T("Could not find a Manager\n")); DestroyManager(manager); return 1; } } //-- Connect to the manager manager->Connect(port,managername); //-- We're connected. Sumbmit Job(s)... WIN32_FIND_DATA find; HANDLE findhandle = FindFirstFile(argv[1],&find); if (findhandle != INVALID_HANDLE_VALUE) { TCHAR opath[MAX_PATH],npath[MAX_PATH]; _tsplitpath(argv[1],opath,npath,0,0); _tcscat(opath,npath); if (opath[_tcslen(opath) - 1] != _T('\\')) _tcscat(opath,_T("\\")); do { _tcscpy(npath,opath); _tcscat(npath,find.cFileName); _tprintf(_T("Submitting %s..."),find.cFileName); if (!assign_job(manager,npath)) break; _tprintf(_T("\r%s Submitted successfully...\n"),find.cFileName); } while (FindNextFile(findhandle,&find)); FindClose(findhandle); } manager->Disconnect(); //-- Exception Handler } catch (MaxNet* maxerr) { _tprintf(_T("\n%s (0x%08X)\n"),maxerr->GetErrorText(),maxerr->GetError()); } //-- Clean up manager instance DestroyManager(manager); } return 0; }