Esempio n. 1
0
void internsymbol(node s, scope v){
     assertpos(issym(s),s);
     push(complete_symbol_list,s);
     if (s->body.symbol.flags & intern_F) {
	  errorpos(s,"symbol defined again ... ");
	  return;
	  }
     s->body.symbol.flags |= intern_F;
     if (v != NULL) reinternsymbol(s,v);
     /* if ( 0 == strcmp("x",tostring(s)) ) trap(); */
     if (s->body.symbol.type!=keyword_T) {
	  char *Cname;
	  assertpos(issym(s),s);
	  if (s->body.symbol.flags & literal_F) {
	    Cname = tostring(s); /* no totoken here? */
	    if (!(s->body.symbol.flags & nouniquify_F))
	      Cname = uniquify(Cname);
	  }
	  else {
	    Cname = totoken(tostring(s));
	    if (s->body.symbol.flags & (export_F | import_F))
	      Cname = prefixify(s->body.symbol.package,Cname);
	    if (!(s->body.symbol.flags & nouniquify_F))
	      Cname = uniquify(Cname);
	    else if (s->body.symbol.name->body.unique_string.seqno == 0) 
	      s->body.symbol.name->body.unique_string.seqno++;
	  }
	  s->body.symbol.Cname = Cname;
	  }
     if (s->body.symbol.flags & (export_F|import_F)) exportit(s,v);
     }
int main(){
  //if( argc != 2)
  //  { cout << "requires localport" << endl; return(-1); }
  //int localport = atoi(argv[1]);
  int localport = 1234; 
  cout << "Hello!" << endl;
  dataholder players("playerlist.txt");
  dataholder highs("highscores.txt");

  string gamemap[15];
  getmap(gamemap);
  cout << "Current map:\n";
  for(int i = 0; i < 15; i++)
    cout << gamemap[i] << endl;  
  cout << "\n\n";
  
  int mainsocket = socket(AF_INET, SOCK_DGRAM, 0);
  if (mainsocket < 0){
    perror("socket creation");
    exit(1);
  }
  sockaddr_in localinfo;
  //localinfo.sin_len = sizeof(localinfo);
  localinfo.sin_family = AF_INET;
  localinfo.sin_addr.s_addr = htonl(INADDR_ANY);
  localinfo.sin_port = htons(localport);

  int r = bind(mainsocket, (sockaddr *) &localinfo, sizeof(localinfo));
  if (r<0){
    perror("bind");
    exit(1);
  }
  printf("ready\n");
  cout << "running on port " << localport << endl;
  
  char buffer[20000];
  int tokcount;
  string tok1;
  string tok2;
  string tok3;
  string tok4;
  string tosend;
  string strbuff;
  int a;
  string findret;

  while(true){
    tosend.clear();
    tokcount = 1;
    //struct sockadder_in incoming_info;
    //unsigned int socklen = sizeof(incoming);
    struct sockaddr_in incoming;
    unsigned int socklen = sizeof(incoming);

    r = recvfrom( mainsocket, &buffer, sizeof(buffer)-1, 0, 
                  (sockaddr *) &incoming, &socklen);
    printf("from %s port %d, %d characters: %s\n",
            inet_ntoa(incoming.sin_addr),
            ntohs(incoming.sin_port),
            r, buffer);
    
    buffer[r] = 0;
    cout << "Received: " << buffer << endl;
    if( r<0 ){
      perror("recvfrom");
      exit(1); }
    buffer[r] = 0;
    strbuff = buffer;
    // change to loop when adding data structures
    a = totoken(strbuff,tok1);
    if (a >= 0){
      tokcount++;
      a = totoken(strbuff,tok2);
      if (a >= 0){
        tokcount++;
        a = totoken(strbuff,tok3);
        if( a >= 0){
          tokcount++;
          a = totoken(strbuff,tok4);
          if( a >=0)
            tokcount++; } } }
    cout << "tokcount-> " << tokcount << endl;
    if (tokcount == 2){ // FINP, DELP, FINM, FINH, WINNER
      cout << "tok1: " << tok1 << "  tok2: " << tok2 << endl;
      string comp = "WIN";
      if(tok1 == "WINNER" || tok1 == "WIN" || tok1 == comp)
       cout << "They are the same\n";
      if( tok1 == "FINP" ){
        a = players.find(tok2,findret);
        if( a == -1)
          tosend = "could not find";
        else{
          tosend = "PLR:";
          tosend += findret;
        }
      }
      else if(tok1 == "WINNER" || tok1 == "WIN" || tok1 == comp){
        r = recvfrom( mainsocket, &buffer, sizeof(buffer)-1, 0, 
                  (sockaddr *) &incoming, &socklen);
        buffer[r] = 0;
        cout << "ABOUT TO ADD: " << buffer << " TO HIGHSCORES\n";	
        highs.highadd(buffer);
        cout << "Added " << buffer << " to highscores\n";
        tosend = "good";
      }
      else if (tok1 == "DELP"){
        a = players.find(tok2,findret);
        if( a == -1)
          tosend = "ERR:invalid gamertag";
        else{
          players.take(a-1,a+3);
          tosend = "good\n";}
      }
      else if (tok1 == "FINM"){
        tosend = "good";
      }
      else if( tok1 == "FINH"){
        a = highs.find(tok2,findret);
        if( a == -1)
          tosend = "cound not find";
        else{
          tosend = "SCO:";
          tosend += findret;
        }
      }
      else { tosend = "ERR: invalid first token"; }
    }
    else if (tokcount == 3){// ADDM
      cout << "tok1: " << tok1 << "  tok2: " << tok2 << "  tok3: " << tok3<< endl;
      if( tok1 == "ADDM"){
        tosend = "good";
      }
      else 
        tosend = "ERR: invalid first token";
    }
    else if (tokcount == 4){// ADDH, ADDP, U
       cout << "tok1: " << tok1 << "  tok2: " << tok2 << "  tok3: " << tok3 << "  tok4: " << tok4 << endl;
      if(tok1 == "U"){
        players.find(tok2, findret);
        tosend += tok2;
        tosend += ':';
        tosend += tok3;
        tosend += ':';
        tosend += tok4;
        cout << "Comparing tosend and findret\n";
        cout << "Tosend: " << tosend << "   findret: " << findret << endl;
        if(tosend == findret)
         tosend = '1';
        else tosend = '0';
      }
      else if (tok1 == "ADDP"){
        a = players.find(tok3,findret);
        if(a == -1){
          players.add(4,tok2,tok3,tok4);
          tosend = "good";
        }
        else tosend = "ERR:gamertag taken";
      }
      else if( tok1 == "ADDH"){
        tosend = "good";
      }
    }
    else {
      if (tok1 == "off") tosend = "off";
      else if (tok1 == "save"){
        players.writeout(); //finish later
        highs.writeout();
      }
      else if (tok1 == "MAP"){
        for( int i = 0; i < 15; i++){
          tosend += gamemap[i];
          tosend += '!';
        }
      }
      else  tosend = "ERR: invalid message structure";}

    cout << "---REPONSE---\n" << tosend << endl << endl;

    r = sendto(mainsocket, tosend.c_str(), strlen(tosend.c_str()), 0,
               (sockaddr *) &incoming, sizeof(incoming));
    if (r<0){
      perror("sendto");
      exit(1); }
    if( tok1 == "off" ) break;
  }
 
  return 0;
}