コード例 #1
0
ファイル: ClientCollection.cpp プロジェクト: dmstkd7/sogong
void ClientCollection::signUp_deleteClient() {
	int command = 0;

	cout << "1. 회원 가입" << endl;
	cout << "2. 회원 탈퇴" << endl;
	cout << "0. 메인 메뉴로 돌아가기" << endl;

	cout << "* 입력 선택 : ";
	cin >> command;
	cin.clear();
	cin.ignore(256, '\n');

	switch (command) {
	case 1:
		cout << "************** 회원가입 하러 오셨군요 반갑습니다 **************" << endl;
		if (CurrentUser.getId() != "!!!"){ cout << "로그아웃 후 가입이 가능합니다." << endl; }
		else {
			ClientCollect.signUp();
			cout << "회원 가입이 완료되었습니다." << endl;
		}
		break;

	case 2:
		cout << "************** 회원탈퇴 하러 오셨군요 반갑습니다 **************" << endl;
		// CurrentUser만이 탈퇴가 가능하다
		if (CurrentUser.getId() == "!!!") {
			cout << "로그인 후 탈퇴가 가능합니다." << endl;
		}
		else{
			if (ClientCollect.deleteClient(CurrentUser) == true) { cout << "탈퇴가 완료되었습니다." << endl; }

			else { cout << "탈퇴가 취소되었습니다." << endl; }
		}

		break;

	case 0:
		cout << "메인 메뉴로 돌아갑니다." << endl;
		return;
		break;

	default:
		cout << "잘못된 값을 입력하셨습니다. 다시 입력해주세요";
		break;
	}

}
コード例 #2
0
void *commandThread( void *sID ){
  int sockID = *( reinterpret_cast<int *>( sID ) );
  pthread_mutex_unlock( &accept_mutex );

  int name_ok, n, cmdsize = COMMAND_LEN, cmdstatus, wbsize;
  bool valid, write_back;
  string clientName, comstr, comhead = "", comarg = "", wbmessage;
  char username[ CLIENT_NAME_LEN + 1 ];
  char buff[ MAXLINE ];
  Client *client;

  cout << "cThread(" << pthread_self() << "): Reading username...\n";
  if( ( readN( sockID, username, CLIENT_NAME_LEN ) ) < 0 ){
    cout << "cThread(" << pthread_self() << "): Read error.\n";
    exit( 6 );
  }

  clientName = username;
  if( allClients.exists( clientName ) ){
    name_ok = NAME_NOT_AVAILABLE;
    name_ok = htons( name_ok );
    if( ( writeN( sockID, ( char * ) &name_ok, sizeof( name_ok ) ) ) < 0 ){
      cout << "cThread(" << pthread_self() << "): Write error.\n";
      exit( 7 );
    }
    pthread_exit( NULL );
  }
  
  name_ok = NAME_AVAILABLE;
  name_ok = htons( name_ok );
  if( ( writeN( sockID, ( char * ) &name_ok, sizeof( name_ok ) ) ) < 0 ){
    cout << "cThread(" << pthread_self() << "): Write error.\n";
    exit( 8 );
  }

  client = new Client( clientName, defaultChatRoom );
  defaultChatRoom->addClient( client );
  client->setCommandSockDesc( sockID );
  allClients.addClient( client );

  while( !client->isActive() );
 
  memset( buff, '\0', MAXLINE );

  // first, read the size of the command
  while( ( readN( sockID, ( char * ) &cmdsize, sizeof( cmdsize ) ) ) > 0 ){

    // then, read the command
    if( ( n = readN( sockID, buff, cmdsize ) ) < 0 ){
      cout << "cThread(" << pthread_self() << "): Read error.\n";
      exit( 9 );
    }
    comstr = buff;
    cout << "cThread(" << pthread_self() << "): " << comstr << "\n";
    
    // In order to keep the internals consistent across clients, we lock this region
    pthread_mutex_lock( &command_mutex );

    // first pass - see if the command has the right number of characters
    valid = true;
    write_back = false;
    if( comstr.length() > 3 ){
      for( int i = 0; i < 4; i++ )
	comhead += comstr[i];
    }
    else
      valid = false;
    if( comhead != "bye." && comhead != "shut" && comhead != "lscr" && comhead != "lssu" && comhead != "myde" )
      if( comstr.length() > 5 )
	for( int j = 5; j < comstr.length(); j++ )
	  comarg += comstr[j];
      else
	valid = false;
    
    // second pass - interpret the command
    if( valid ){
      
      // "bye." - client wishes to exit the server
      if( comhead == "bye." ){
	allChatRooms.wipeClient( client->getID() );
	allClients.deleteClient( client->getID() );
	client->closeReader();
	client->closeWriter();
      }
      // "crea" - CREAte a new chatroom
      else if( comhead == "crea" ){
	ChatRoom *newRoom = new ChatRoom( comarg );
	if( !allChatRooms.addChatRoom( newRoom ) )
	  valid = false;
	else
	  dispatch( newRoom );
      }
      // "subs" - SUBScribe to an existing chatroom
      else if( comhead == "subs" ){
	ChatRoom *newRoom;
	if( newRoom = allChatRooms.exists( comarg ) )
	  if( !newRoom->addClient( client ) )
	    valid = false;
	  else;
	else
	  valid = false;
      }
      // "unsu" - UNSUbscribe from a given chatroom
      else if( comhead == "unsu" ){
	ChatRoom *newRoom;
	if( newRoom = allChatRooms.exists( comarg ) )
	  if( newRoom->removeClient( client->getID() ) )
	    if( !newRoom->numClients() && newRoom->getRoomName() != "Default" ){
	      allChatRooms.deleteChatRoom( comarg );
	      // Below should trigger the clean-up of the ChatRoom's consumer threads
	      newRoom = NULL;
	    }
	    else;
	  else 
	    valid = false;
	else
	  valid = false;
      }
      // "shut" - SHUT the server down
      else if( comhead == "shut" ){

      }
      // "myde" - displays the client's default chatroom
      else if( comhead == "myde" ){
	wbmessage = "(None)\n";
	if( client->getDefaultChatRoom() ){
	  wbmessage = client->getDefaultChatRoom()->getRoomName();
	  wbmessage += '\n';
	}
	write_back = true;
      }
      // "defa" - change the client's DEFAult chatroom; this is where the client's messages are sent
      else if( comhead == "defa" ){
	ChatRoom *newRoom;
	if( newRoom = allChatRooms.exists( comarg ) ){
	  client->setDefaultChatRoom( newRoom );
	  newRoom->addClient( client );
	}
	else
	  valid = false;
      }
      // "lscr" - provide a LIst of all existing ChatRooms
      else if( comhead == "lscr" ){
	wbmessage = allChatRooms.lscr();
	write_back = true;
      }
      // "lssu" - provide a LIst of chatrooms to which this client has SUbscribed 
      else if( comhead == "lssu" ){
	wbmessage = allChatRooms.lssu( client->getID() );
	write_back = true;
      }
      else
	valid = false;
    }

    if( valid )
      if( write_back )
	cmdstatus = COMMAND_WRITE_BACK;
      else
	cmdstatus = COMMAND_ACCEPTED;
    else
      cmdstatus = COMMAND_NOT_ACCEPTED;

    if( ( writeN( sockID, ( char * ) &cmdstatus, sizeof( cmdstatus ) ) ) < 0 ){
      cout << "cThread(" << pthread_self() << "): Write error.\n";
      exit( 10 );
    }
    if( cmdstatus == COMMAND_WRITE_BACK ){
      wbsize = wbmessage.length();
      if( ( writeN( sockID, ( char * ) &wbsize, sizeof( wbsize ) ) ) < 0 ){
	cout << "cThread(" << pthread_self() << "): Write error.\n";
	exit( 11 );
      }
      if( ( writeN( sockID, ( char * ) wbmessage.c_str(), wbsize ) ) < 0 ){
	cout << "cThread(" << pthread_self() << "): Write error.\n";
	exit( 12 );
      }
    }
    pthread_mutex_unlock( &command_mutex );

    comhead = "";
    comarg = "";
    wbmessage = "";
    memset( buff, '\0', MAXLINE );    
    
  }
  // clean up!
  pthread_exit( NULL );
}