Exemple #1
0
int selection(void)
{
	int no = 0;
	do{
		no = inputNo();
		switch(no){
		  case 1:
			enter();
			break;
		
		  case 2:
			list();
			break;
		
		  case 3:
			serch();
			break;
		
		  case 4:
			printf("【終了】\n");
			break;

		  default:
			printf("1~4までの半角数字でご入力頂きますよう、お願いします。\n");
			break;
		}
		
	}while(4 != no);
	
	return 0;
}
//取消文件传输
int ceaseSend()
{
  gsNode *gsList[CAPACITY], *cur;
  int tmp, index, count;
  char buf[FILENAME];
  filenode *curFile;

  while(1)
  {
    pthread_mutex_lock(&sendFMutex);
    count = listSFiles(gsList, sendFHead.next, sizeof(gsList)/sizeof(gsList[0])-1);
    pthread_mutex_unlock(&sendFMutex);

    if (count == 0)
      return 0;
    else
    {
      printf("\n++++++++++++++++++++\n");
      printf("Files to send:\n");
      for(tmp=1;tmp<=count;tmp++)
      {
        printf("%d. ", tmp);
        curFile = (*(gsList+tmp-1))->fileList.next;
        while (curFile!=NULL)
        {
          printf("%s ", curFile->fileName);
          curFile = curFile->next;
        }
        printf("\n");
      }
      printf("++++++++++++++++++++\n");
    }

    index = inputNo(1, count, 1,
                    "Which file(s) to cancel?(Ctrl+D to quit)\nNumber[1]:");

    if (index<0)
      return -1;

    pthread_mutex_lock(&sendFMutex);
    (*(gsList+index-1))->cancelled = 1;
    if ((*(gsList+index-1))->tcpSock>=0)
      shutdown((*(gsList+index-1))->tcpSock, SHUT_RDWR);
    pthread_mutex_unlock(&sendFMutex);
  }
}
////////////////////////////////////////////////////
//文件接收
//选择要接收的文件
int recvFiles()
{
  gsNode *gsList[CAPACITY], *cur;
  int tmp, index, count;
  char buf[FILENAME];
  struct stat dirAttr;
  pthread_t gFile;
  filenode *curFile;
  pthread_attr_t attr;

  while(1)
  {
    pthread_mutex_lock(&getFMutex);
    count = listGFiles(gsList, getFHead.next, sizeof(gsList)/sizeof(gsList[0])-1);
    pthread_mutex_unlock(&getFMutex);

    if (count == 0)
      return -1;
    else
    {
      printf("\n++++++++++++++++++++\n");
      printf("Files to get:\n");
      for(tmp=1;tmp<=count;tmp++)
      {
        printf("%d. ", tmp);
        curFile = (*(gsList+tmp-1))->fileList.next;
        while (curFile!=NULL)
        {
          printf("%s ", curFile->fileName);
          curFile = curFile->next;
        }
        printf("\n");
      }
      printf("++++++++++++++++++++\n");
    }

    index = inputNo(1, count, 1,
                    "Which file(s) to get?(Ctrl+D to quit)\nNumber[1]:");

    if (index<0)
      return -1;

    while(1)
    {
      printf("Where do you want to save?(Ctrl+D to quit)\nTarget dir[.]:");
      if (fgets(buf, sizeof(buf), stdin)==NULL)
        return -1;

      transfStr(buf, 0); //去除前后空白字符

      if (buf[0]=='\0')
      {
        buf[0]='.';
        buf[1]='\0';
      }

      if ((stat(buf, &dirAttr)<0) ||
          !S_ISDIR(dirAttr.st_mode) ||
          (access(buf, W_OK)<0))
        printf("Invalid directory. Please input again.\n");
      else break;
    }

    cur = *(gsList+index-1);
    tmp = strlen(buf);
    pthread_mutex_lock(&getFMutex);
    if (cur->cancelled == 0)
    {
      cur->targetDir = (char*)malloc(tmp+1);
      strncpy(cur->targetDir, buf, tmp+1);
      cur->transferring = 1;
    }
    pthread_mutex_unlock(&getFMutex);
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_create(&gFile, &attr, &getData, cur);
  }

}
//////////////////////////////////////////////////
//聊天
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);

}