コード例 #1
0
void EditorBuffer::moveCursorCol(int direction, bool shift, bool cmd) {
  if (cmd) {
    // If command key is pressed move to next word
    bool notFound = true;
    while (notFound && cursorPosition > text.begin() && cursorPosition < text.end()) {
      char stop_chars[] = " {}[]()\n";
      cursorPosition += direction;
      updateSelect(shift);
      for (unsigned int i = 0; i < strlen(stop_chars); ++i) {
        if (*(cursorPosition + direction) == stop_chars[i]) {
          notFound = false;
          break;
        }
      }
    }
  }
  else {
    if (direction == -1 && cursorPosition != text.begin()) {
      cursorPosition--;
      updateSelect(shift);
    }
    if (direction == 1 && cursorPosition != text.end()) {
      cursorPosition++;
      updateSelect(shift);
    }
  }
}
コード例 #2
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int selector()
{
	//backup file descriptor set
	fd_set working_set; 
	int i = 0;

	//timeout values	
	struct timeval tv;
	tv.tv_sec = 2;
	tv.tv_usec = 500000;

	FD_ZERO(&master_set);
	FD_ZERO(&working_set);

	fdmax = serverSocket;
	
	updateSelect(ADD, serverSocket, &master_set, &fdmax);
	updateSelect(ADD, STDIN_FILENO, &master_set, &fdmax);

	while(true){
		//copy master to working set

		printf("\r%s:%s >> ", localInfo.hostname, localInfo.port); 
		fflush(stdout);
		
		working_set = master_set;
		//memcpy(&working_set, &master_set, sizeof(master_set));

		if(select(fdmax+1, &working_set, NULL, NULL, NULL) < 0){
			perror("select: ");
			return -1;
		}

		for(i = 0; i <= fdmax; i++){
			if(FD_ISSET(i, &working_set)){
				if(i == serverSocket){
					acceptConnection();
				}else if(i == STDIN_FILENO){
					char* command = NULL;
					size_t nbytes;
					int result;
					getline(&command, &nbytes, stdin);
					result = command_execute(command);
					if(result){
						printf("Menu command failed: %s\n", strerror(result));
					}
				}else{
					//pass socket requiring attention
					printf("Incoming Message..\n");
					handle_command(i);
				}
			}
		}
	}
	return -1;
}
コード例 #3
0
ファイル: menu.cpp プロジェクト: hjqqq/tetrimino
void Menu::update()
{
    updateSelect();
    for (int i = 0; i != labelVector.size(); ++i){
	labelVector[i]->update();
    }
}
コード例 #4
0
void EditorBuffer::insert(int key) {
  if (selectStart != selectEnd) {
    cursorPosition = text.erase(selectStart, selectEnd);
  }
  cursorPosition = text.insert(cursorPosition, key);
  cursorPosition++;
  updateSelect(false);
}
コード例 #5
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
void cmd_CutAll()
{
	int i = 1;
	int socket;
	while(i <= connectionsCount){
		socket = getSocketFromList(i);
		removeFromConnections(socket);
		close(socket);
		updateSelect(DELETE, socket, &master_set, &fdmax);
		i += 1;
	}
	printf("Connection Lost to Server. Terminated all connections..\n");
}
コード例 #6
0
void EditorBuffer::backspace() {
  if (selectStart == selectEnd) {
    if (cursorPosition != text.begin()) {
      cursorPosition--;
      if (cursorPosition != text.end()) {
        cursorPosition = text.erase(cursorPosition);
      }
    }
  }
  else {
    cursorPosition = text.erase(selectStart, selectEnd);
  }
  updateSelect(false);
}
コード例 #7
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int cmd_terminate(int argc, char* argv[])
{
	if(argc != 2){
		printf("Please provide Connection Socket\n");
		return EINVAL;
	}

	int socket = getSocketFromList(atoi(argv[1]));
	if (removeFromConnections(socket) != 0){
		return -1;
	}

	close(socket);
	updateSelect(DELETE, socket, &master_set, &fdmax);

	if(localInfo.isServer){
		sendIPLIST();
	}

	return 0;
}
コード例 #8
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int cmd_register(int argc, char* argv[])
{

	if(localInfo.isServer){
		printf("Only Clients Can Register!\n");
		return EOPNOTSUPP;
	}

	if(argc != 3){
		printf("Please Provide IP Address and Port Of Server\n");
		return EINVAL;
	}

	if(localInfo.isRegistered){
		printf("Already Registered!\n");
		return EACCES;
	}

	struct addrinfo hints, *servinfo;
	int result;
	int serverConnection;
	char s[INET6_ADDRSTRLEN];

	//extract arguments
	char* server = argv[1];
    //safe PORT copying
    int pp = atoi(argv[2]);
    char serverPort[50];
    sprintf(serverPort, "%d", pp);

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;

	if((result = getaddrinfo(server, serverPort, &hints, &servinfo)) < 0){
		printf("getaddrinfo error:\n");
		return result;
	}

	if((serverConnection = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0){
		perror("register socket :");
		return serverConnection;
	}


	if(connect(serverConnection, servinfo->ai_addr, servinfo->ai_addrlen) < 0){
		perror("register connect :");
		return -1;
	}

	inet_ntop(servinfo->ai_family, get_in_addr((struct sockaddr *)servinfo->ai_addr), s, sizeof(s));
	printf("\nCLIENT CONNECTING TO %s\n", s);

	//prepare message containing listening port number
	char sendBuff[200];
	sprintf(sendBuff, "%s\n", localInfo.port);

	if(sendall(serverConnection, sendBuff, sizeof(sendBuff)) == -1){
		perror("Register: send ");
		close(serverConnection);
		return -1;
	}
	
	memset(&sendBuff[0], 0, sizeof(sendBuff));
	if((result = recvall(serverConnection, sendBuff, sizeof(sendBuff))) == -1){
		printf("handle_command recvall failed\n");
		return -1;
	}

	localInfo.isRegistered = true;

	//print message from server
	printf("%s\n", sendBuff);

	//update connections list 
	addToConnections(server, serverPort, s, serverConnection);

	//update descriptor in FD_SET
	updateSelect(ADD, serverConnection, &master_set, &fdmax);

	printf("\n");

	freeaddrinfo(servinfo);

	return 0;
}
コード例 #9
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int cmd_connect(int argc, char* argv[]){
	
	//check the arguments number
	if(argc != 3){
		printf("Please Provide IP Address and Port Of Peer\n");
		return EINVAL;
	}
	
	struct addrinfo hints, *servinfo;
	int result;
	int peerConnection;
	char ipaddr[INET6_ADDRSTRLEN];

	char* peer = argv[1];

    //safe PORT copying
    int pp = atoi(argv[2]);
    char peerPort[50];
    sprintf(peerPort, "%d", pp);

    //validate if connection allowable
    result = validateConnection(peer);
    
    if(result == -1){
    	return EHOSTUNREACH;
    }

    memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;

	//Connection process initiated
	if((result = getaddrinfo(peer, peerPort, &hints, &servinfo)) < 0){
		printf("getaddrinfo error:\n");
		return result;
	}

	if((peerConnection = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0){
		perror("connect socket :");
		return peerConnection;
	}


	if(connect(peerConnection, servinfo->ai_addr, servinfo->ai_addrlen) < 0){
		perror("connect connect :");
		return -1;
	}

	//printout info message about remote host
	inet_ntop(servinfo->ai_family, get_in_addr((struct sockaddr *)servinfo->ai_addr), ipaddr, sizeof(ipaddr));
	printf("\nCLIENT CONNECTING TO %s\n", ipaddr);

	//prepare message containing listening port number and hostnamt
	char sendBuff[200];
	sprintf(sendBuff, "%s\n", localInfo.port);

	if(sendall(peerConnection, sendBuff, sizeof(sendBuff)) == -1){
		perror("Register: send ");
		close(peerConnection);
		return -1;
	}

	//update connections list 
	addToConnections(peer, peerPort, ipaddr, peerConnection);

	//update descriptor in FD_SET
	updateSelect(ADD, peerConnection, &master_set, &fdmax);

	printf("\n");

	return 0;
}
コード例 #10
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int handle_command(int socket)
{
	char message[MESSAGE];
	int result;
	int code;

	//receive message
	if((result = recvall(socket, message, sizeof(message))) == -1){
		printf("handle_command recvall failed\n");
		return -1;
	}

	//if result 0, close connection
	if(result == CLOSED){
		struct peer *closedSocket = findConnection(socket);
		printf("%s closed the connection\n", closedSocket->hostname);
		updateSelect(DELETE, socket, &master_set, &fdmax);
		removeFromConnections(socket);
		if(localInfo.isServer){
		//broadcast new IP list
			sendIPLIST();
		}
		return 0;
	}

	//printf("NEW MESSAGE: %s\n", message);
	
	//EXTRACTING MESSAGE 
	char* newptr = message;
	int nbytes;

	char hostName[200];
	char port[50];
	char ipaddr[INET6_ADDRSTRLEN];

	//extract header
	sscanf(newptr, "%d %n", &code, &nbytes);
	newptr += nbytes;

	//SERVER IP UPDATE
	switch(code) {

		case ADD_IP_LIST :
		{
			DESTROY_IPLIST();
			printf("\n%s UPDATED SERVER IP LIST:\n", getTime());
			while(true){
				//parse out the IP LIST
				sscanf(newptr, "%s %s %s %n", hostName, ipaddr, port, &nbytes);
				if(strcmp(hostName, "EOF") == 0) {break;}
				//printf("%s %s %s\n",hostName, ipaddr, port);
				SAVE_IPLIST(hostName, port, ipaddr);
				newptr += nbytes;
			}
			PRINT_IPLIST();
			printf("\n");
			break;
		}
		case SEND_FILE :
		{
			char filename[50];
			char file_len[50];
			char chunks_num[50];
			sscanf(newptr, "%s %s %s", filename, file_len, chunks_num);
			struct peer *sender = findConnection(socket);
			printf("DOWNLOADING FILE: %s FROM: %s\n", filename, sender->hostname);
			if ((result = receiveFile(socket, filename, file_len, chunks_num)) != 0){
				return result;
			}
			break;
		}
		case REQUEST_FILE : 
		{
			char filename[50];
			int connectionID;
			sscanf(newptr, "%s", filename);

			if((connectionID = findID(socket)) == -1){
				printf("REQUEST_FILE: connection does not exist\n");
				return EHOSTUNREACH;
			}

			char ID[20];
			sprintf(ID, "%d", connectionID);
			int argCount = 3;
			char *arguments[] = {"UPLOAD", ID, filename};

			if((result = cmd_upload(argCount, arguments)) != 0){
				printf("ERROR: Could not upload file.\n");
				char message[MESSAGE];
				sprintf(message, "%d %s\n",NOTIFICATION, "REQUEST FAILED: COULD NOT UPLOAD FILE!");
				sendall(socket, message, sizeof(message));
				return EHOSTUNREACH;
			}
			break;
		}
		case NOTIFICATION :
		{	
			struct peer *sender = findConnection(socket);
			printf("\n**** NOTIFICATION FROM: %s ****\n", sender->hostname);
			printf(">> %s\n", newptr);
			break;
		}
		default :
			printf("Invalid Message Code, cannot understand\n");
			return -1;
	}

	return 0;	
}
コード例 #11
0
ファイル: proj1.c プロジェクト: itsbart/FileSharing
int acceptConnection()
{
	printf("\n");
	int newConnection;
	struct sockaddr_storage remoteaddr; //client address
	socklen_t addrlen;
	int port;
	int result;

	addrlen = sizeof(remoteaddr);
	//accept connection
	newConnection = accept(serverSocket, (struct sockaddr *)&remoteaddr, &addrlen);
	
	if(newConnection == -1){
		perror("accept");
		return -1;
	}

	char remoteIP[INET6_ADDRSTRLEN];
	char remoteName[250];
	char remotePort[50];

	//printout remote host IP
	inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr *)&remoteaddr), remoteIP, INET6_ADDRSTRLEN);
	printf("\nNEW Connection Request From: %s\n", remoteIP);

	//extract message from host with its listening port
	char message[200];
	if((result = recvall(newConnection, message, sizeof(message))) == -1){
		printf("acceptConnection: recvall failed \n");
		return -1;
	}

	//resolve IP to a name
	char remoteHost[300];
	result = getnameinfo((struct sockaddr*)&remoteaddr, sizeof(remoteaddr), remoteHost, sizeof(remoteHost), NULL, 0, 0);
	if(result != 0){
		printf("acceptConnection: getnameinfo error\n");
		return -1;
	}

	sscanf(message, "%s", remotePort);
	printf("Connection from %s accepted\n", remoteHost);
	addToConnections(remoteHost, remotePort, remoteIP, newConnection);

	//update FD_SET with new Socket
	if(updateSelect(ADD, newConnection, &master_set, &fdmax) != 0){
		printf("acceptConnection: updateSelect failed\n");
		return -1;
	}

	//reuse message buffer and send confirmation
	if(localInfo.isServer){
		memset(&message[0], 0, sizeof(message));
		sprintf(message, "Registration Successful.");
		if(sendall(newConnection, message, sizeof(message)) == -1){
			perror("acceptConnection: send");
			return -1;
		}
	}
	//broadcast IP list
	if(localInfo.isServer){
		sendIPLIST();
	}

	printf("\n");

	return 0;
}
コード例 #12
0
void EditorBuffer::insert(const string s) {
  int loc = cursorPosition - text.begin();
  text.insert(cursorPosition, s.begin(), s.end());
  cursorPosition = text.begin() + loc + s.size();
  updateSelect(false);
}
コード例 #13
0
void EditorBuffer::removeSelection() {
  cursorPosition = text.erase(selectStart, selectEnd);
  updateSelect(false);
}
コード例 #14
0
void EditorBuffer::moveCursorRow(int direction, bool shift, bool cmd) {
  setCursorPosition(getCurrentCol(), getCurrentRow() + direction);
  updateSelect(shift);
}