コード例 #1
0
ファイル: server.c プロジェクト: ruipfmendes/IRC
void process_client(int client_fd)
{
    int nread = 0;
    int i;
    int n_receivers;
    char times [SHORT_STRING];
    char buffer[BUF_SIZE];
    char user[SHORT_STRING];
    char sender[SHORT_STRING];
    char subject[NORMAL_SIZE];
    char body[BUF_SIZE];
    char index[SHORT_STRING];
    char receiver[SHORT_STRING];

    char waste[100];

    nread = read(client_fd, buffer, BUF_SIZE-1);
    fflush(stdout);
    buffer[nread] = '\0';

    if(strcmp(buffer, "inbox") == 0){
        nread = read(client_fd, user, SHORT_STRING-1);
        fflush(stdout);
        user[nread] = '\0';
        check_inbox(user, client_fd);

    } else if(strcmp(buffer, "cmail") == 0){
        printf("ENTROU NA FUNÇAO CMAIL\n");

        nread = read(client_fd, user, SHORT_STRING-1);
        fflush(stdout);
        user[nread] = '\0';
        printf("user-> %s\n", user);

        nread=read(client_fd, times,SHORT_STRING-1);
        fflush(stdout);
        times[nread-1]='\0';
        n_receivers = atoi(times);
        printf("n_receivers->%d\n", n_receivers);

        nread=read(client_fd,subject, NORMAL_SIZE-1);
        fflush(stdout);
        subject[nread]=0;
        printf("SUBJECT-> %s\n", subject);

        nread=read(client_fd,body, BUF_SIZE-1);
        fflush(stdout);
        body[nread]=0;
        printf("BODY-> %s\n", body);

        for(i = 0; i < n_receivers; i++){
            nread = read(client_fd, receiver, SHORT_STRING-1);
            fflush(stdout);
            receiver[nread] = '\0';
            printf("user->%s subject->%s body->%s receiver-> %s\n", user, subject, body, receiver);
            inserir_email(user, receiver, subject, body, client_fd);
        }
    } else if(strcmp(buffer, "omail") == 0){
        nread = read(client_fd, user, SHORT_STRING-1);
        fflush(stdout);
        user[nread] = '\0';

        nread = read(client_fd, index, SHORT_STRING-1);
        fflush(stdout);
        index[nread] = '\0';

        open_email(user, atoi(index), client_fd);
    } else if(strcmp(buffer, "dmail") == 0){
        nread = read(client_fd, user, SHORT_STRING-1);
        fflush(stdout);
        user[nread] = '\0';
        nread = read(client_fd, index, SHORT_STRING-1);
        fflush(stdout);
        index[nread] = '\0';

        delete_email(user, atoi(index), client_fd);
    } else {
        printf("Wrong key word\n");
    }

    /*write(client_fd,buffer,1+strlen(buffer));
    fflush(stdin);*/


    fflush(stdout);
    close(client_fd);
}
コード例 #2
0
void main()
{
  badge_setup();
  simpleterm_close();
  ir_start();
  ee_wipe();
  
  // Connection to host routine (FORCE CONNECTION TO HOST)
  port = fdserial_open(31, 30, 0, 115200);
  char_size(SMALL);
  cursor(2, 4);
  display("Connecting...");
  while(1)
  {
    dprint(port, "Propeller\n");
    pause(1000);  // We need this pause, since the host needs time to respond. 5 x 1 second = 10 second timeout
    if (fdserial_rxCount(port) == 0)
    {
      continue;
    }      
    else if (fdserial_rxCount(port) < 5)
    {
      fdserial_rxFlush(port);
      continue;
    }
    else dscan(port, "%s", handshake);
    // Attempt handshake and listen to response
    if (strcmp(handshake, "H0st") == 0)
    {
      break;
    }      
  }
  clear();
  
  while(1)
  {
    rgb(L, OFF);
    rgb(R, OFF);
        
    char_size(BIG);
    cursor(0, 0);
    display("HOTSPOT!");
    char_size(SMALL);
    cursor(4, 5);
    display("Start an");
    cursor(1, 6);
    display("interaction to");
    cursor(0, 7);
    display("upload your data");
    
    memset(&their, 0, sizeof(info));
    
    int t = CNT;
    int dt = CLKFREQ * 2;
    
    if (check_inbox() == 1)
    {
      clear();
      message_get(&their);
      ir_send(&my);
      
      char_size(SMALL);
      cursor(2, 2);
      display("Receiving...");
      
      while(1)
      {
        if (CNT - t > dt)
        {
          clear();
          char_size(SMALL);
          cursor(5, 2);
          display("ERROR!");
          cursor(0, 5);
          display("Please try again");
          dprint(port, "txBegin\n");  
          dprint(port, "Timeout\n");
          ee_wipe();
          clear_inbox();
          pause(2000);
          clear();
          break;
        }
        if (check_inbox() == 1)
        {
          t = CNT;
          message_get(&their);
          if(!strcmp(their.name, "txDone"))
          {
            char_size(SMALL);
            cursor(0, 5);
            display("Upload complete");
            dprint(port, "txBegin\n");
            ee_uploadContacts(port);
            ee_wipe();
            clear_inbox();
            pause(1000);
            clear();
            break;
          }
          ee_save(&their);
          dprint(port, "Name: %s\n", their.name);
          dprint(port, "Email: %s\n", their.email);       
        }
      }        
    }      
  }    
}  
コード例 #3
0
int quadbuilder_create(quadbuilder_t* qb) {
	int iA=0, iB, iC, iD, newpoint;
	int ninbox;
	int i, j;
	int iAalloc;
	quad q;
	pquad_t* qb_pquads;

	// ensure the arrays are large enough...
	if (qb->Nstars > qb->Ncq) {
		// (free and malloc rather than realloc because we don't care about
		//  the previous contents)
		free(qb->inbox);
		free(qb->pquads);
		qb->Ncq = qb->Nstars;
		qb->inbox =  calloc(qb->Nstars, sizeof(int));
		qb->pquads = calloc(qb->Nstars * qb->Nstars, sizeof(pquad_t));
		if (!qb->inbox || !qb->pquads) {
			ERROR("quad-builder: failed to malloc qb->inbox or qb->pquads.  Nstars=%i.\n", qb->Nstars);
			return -1;
		}
	}

	qb_pquads = qb->pquads;

	/*
	  Each time through the "for" loop below, we consider a new
	  star ("newpoint").  First, we try building all quads that
	  have the new star on the diagonal (star B).  Then, we try
	  building all quads that have the star not on the diagonal
	  (star D).

	  Note that we keep the invariants iA < iB and iC < iD.
	*/
	memset(&q, 0, sizeof(quad));
	for (newpoint=0; newpoint<qb->Nstars; newpoint++) {
		pquad_t* pq;
		// quads with the new star on the diagonal:
		iB = newpoint;
		for (iA = 0; iA < newpoint; iA++) {
			pq = qb_pquads + iA*qb->Nstars + iB;
			pq->inbox = NULL;
			pq->ninbox = 0;
			pq->iA = iA;
			pq->iB = iB;

			check_scale(qb, pq);
			if (!pq->scale_ok)
				continue;

			q.star[0] = pq->staridA;
			q.star[1] = pq->staridB;

			pq->check_ok = TRUE;
			if (qb->check_AB_stars)
				pq->check_ok = qb->check_AB_stars(qb, pq, qb->check_AB_stars_token);
			if (!pq->check_ok)
				continue;

			// list the possible internal stars...
			ninbox = 0;
			for (iC = 0; iC < newpoint; iC++) {
				if ((iC == iA) || (iC == iB))
					continue;
				qb->inbox[ninbox] = iC;
				ninbox++;
			}
			// check which ones are inside the box...
			ninbox = check_inbox(pq, qb->inbox, ninbox, qb->starxyz);
			//if (!ninbox)
			//continue;
			if (ninbox && qb->check_internal_stars)
				ninbox = qb->check_internal_stars(qb, q.star[0], q.star[1], qb->inbox, ninbox, qb->check_internal_stars_token);
			//if (!ninbox)
			//continue;

			add_interior_stars(qb, ninbox, qb->inbox, &q, 2, qb->dimquads, 0);
			if (qb->stop_creating)
				goto theend;

			pq->inbox = malloc(qb->Nstars * sizeof(int));
			if (!pq->inbox) {
				ERROR("hpquads: failed to malloc pq->inbox.\n");
				exit(-1);
			}
			pq->ninbox = ninbox;
			memcpy(pq->inbox, qb->inbox, ninbox * sizeof(int));
			debug("iA=%i, iB=%i: saved %i 'inbox' entries.\n", iA, iB, ninbox);
		}
		iAalloc = iA;

		// quads with the new star not on the diagonal:
		iD = newpoint;
		for (iA = 0; iA < newpoint; iA++) {
			for (iB = iA + 1; iB < newpoint; iB++) {
				pq = qb_pquads + iA*qb->Nstars + iB;
				if (!(pq->scale_ok && pq->check_ok))
					continue;
				// check if this new star is in the box.
				qb->inbox[0] = iD;
				ninbox = check_inbox(pq, qb->inbox, 1, qb->starxyz);
				if (!ninbox)
					continue;
				if (qb->check_internal_stars)
					ninbox = qb->check_internal_stars(qb, q.star[0], q.star[1], qb->inbox, ninbox, qb->check_internal_stars_token);
				if (!ninbox)
					continue;

				pq->inbox[pq->ninbox] = iD;
				pq->ninbox++;

				q.star[0] = pq->staridA;
				q.star[1] = pq->staridB;

				add_interior_stars(qb, pq->ninbox, pq->inbox, &q, 2, qb->dimquads, 0);
				if (qb->stop_creating) {
					iA = iAalloc;
					goto theend;
				}
			}
		}
	}
 theend:
	for (i=0; i<imin(qb->Nstars, newpoint+1); i++) {
		int lim = (i == newpoint) ? iA : i;
		for (j=0; j<lim; j++) {
			pquad_t* pq = qb_pquads + j*qb->Nstars + i;
			free(pq->inbox);
			pq->inbox = NULL;
		}
	}
	return 0;
}