Beispiel #1
0
enum protoCmd rx_request(struct protocol *req, int fd)
{
    struct item  *item;

    // Upgrade version 1 and 2 to  version 3
    if (req->ver == 1) {
        strcpy(req->xmlname, "default");
    } 
    if (req->ver < 3) {
        strcpy(req->mimetype,"image/png"); 
        strcpy(req->options,"");
    } else if (req->ver != 3) {
        syslog(LOG_ERR, "Bad protocol version %d", req->ver);
        return cmdNotDone;
    }

    syslog(LOG_DEBUG, "DEBUG: Got command %s fd(%d) xml(%s), z(%d), x(%d), y(%d), mime(%s), options(%s)",
           cmdStr(req->cmd), fd, req->xmlname, req->z, req->x, req->y, req->mimetype, req->options);

    if ((req->cmd != cmdRender) && (req->cmd != cmdRenderPrio) && (req->cmd != cmdRenderLow) && (req->cmd != cmdDirty) && (req->cmd != cmdRenderBulk)) {
        syslog(LOG_WARNING, "WARNING: Ignoring unknown command %s fd(%d) xml(%s), z(%d), x(%d), y(%d)",
                    cmdStr(req->cmd), fd, req->xmlname, req->z, req->x, req->y);
        return cmdNotDone;
    }

    item = (struct item *)malloc(sizeof(*item));
    if (!item) {
            syslog(LOG_ERR, "malloc failed");
            return cmdNotDone;
    }

    item->req = *req;
    item->duplicates = NULL;
    item->fd = (req->cmd == cmdDirty) ? FD_INVALID : fd;

#ifdef METATILE
    /* Round down request co-ordinates to the neareast N (should be a power of 2)
     * Note: request path is no longer consistent but this will be recalculated
     * when the metatile is being rendered.
     */
    item->mx = item->req.x & ~(METATILE-1);
    item->my = item->req.y & ~(METATILE-1);
#else
    item->mx = item->req.x;
    item->my = item->req.y;
#endif

    return request_queue_add_request(render_request_queue, item);
}
void FileTransferWidget::ReadyRead()
{
    switch (m_transferMode)
    {
    case TM_RECIEVE_CLIENT:
    {
        if (m_currentStatus == FT_WAIT_FOR_HELLO)
        {
            QString cmd(m_socket->readAll());
            qDebug()<<"File transfer cmd recieved: "<<cmd;
            if (cmd.contains("MRA_FT_HELLO") && cmd.contains(m_req.From))
            {
                GetNextFile();
            }
        }
        else
        {
            m_currentFileSize += m_socket->bytesAvailable();
            m_speedBytes += m_socket->bytesAvailable();
            m_ui->doneLabel->setText(MRIMCommonUtils::GetFileSize(m_currentFileSize));
            m_ui->progressBar->setValue(m_currentFileSize);
            m_currentFile.write(m_socket->readAll());

            if (m_currentFileSize >= m_filesHashIter->value())
            {
                //done with current file
                m_currentFile.close();
                m_currentStatus = FT_TRANSFER_FILE_COMPLETED;
                GetNextFile();
            }
        }
    }
    break;
    case TM_SEND_SERVER:
    {
        if (m_currentStatus == FT_WAIT_FOR_HELLO)
        {
            QString cmd(m_socket->readAll());
            qDebug()<<"File transfer cmd recieved: "<<cmd;
            if (cmd.contains("MRA_FT_HELLO") && cmd.contains(m_req.To))
            {
                SendCmd("MRA_FT_HELLO "+m_client->GetAccountInfo().account_name);
                m_currentStatus = FT_WAIT_FOR_TRANSFER;
            }
        }
        else if (m_currentStatus == FT_WAIT_FOR_TRANSFER && m_sentFilesCount < m_req.FilesInfo.count())
        {
            QString cmdStr(m_socket->readAll());
            qDebug()<<"File transfer cmd recieved: "<<cmdStr;
            QStringList cmd = cmdStr.split(' ');
            if (cmd.contains("MRA_FT_GET_FILE"))
            {
                m_currentStatus = FT_TRANSFER;
                SendFile(cmd[1]);
            }
        }
    }
    break;
    }
}
Beispiel #3
0
enum protoCmd rx_request(const struct protocol *req, int fd)
{
    struct protocol *reqnew;
    struct item *list = NULL, *item;
    enum protoCmd pend;

    // Upgrade version 1 to version 2
    if (req->ver == 1) {
        reqnew = (struct protocol *)malloc(sizeof(struct protocol));
        memcpy(reqnew, req, sizeof(struct protocol_v1));
        reqnew->xmlname[0] = 0;
        req = reqnew;
    }
    else if (req->ver != 2) {
        syslog(LOG_ERR, "Bad protocol version %d", req->ver);
        return cmdIgnore;
    }

    syslog(LOG_DEBUG, "DEBUG: Got command %s fd(%d) xml(%s), z(%d), x(%d), y(%d)",
            cmdStr(req->cmd), fd, req->xmlname, req->z, req->x, req->y);

    if ((req->cmd != cmdRender) && (req->cmd != cmdRenderPrio) && (req->cmd != cmdDirty) && (req->cmd != cmdRenderBulk))
        return cmdIgnore;

    item = (struct item *)malloc(sizeof(*item));
    if (!item) {
            syslog(LOG_ERR, "malloc failed");
            return cmdNotDone;
    }

    item->req = *req;
    item->duplicates = NULL;
    item->fd = (req->cmd == cmdDirty) ? FD_INVALID : fd;

    item_load(item, req);

#ifdef METATILE
    /* Round down request co-ordinates to the neareast N (should be a power of 2)
     * Note: request path is no longer consistent but this will be recalculated
     * when the metatile is being rendered.
     */
    item->mx = item->req.x & ~(METATILE-1);
    item->my = item->req.y & ~(METATILE-1);
#else
    item->mx = item->req.x;
    item->my = item->req.y;
#endif

    pthread_mutex_lock(&qLock);

    // Check for a matching request in the current rendering or dirty queues
    pend = pending(item);
    if (pend == cmdNotDone) {
        // We found a match in the dirty queue, can not wait for it
        pthread_mutex_unlock(&qLock);
        free(item);
        return cmdNotDone;
    }
    if (pend == cmdIgnore) {
        // Found a match in render queue, item added as duplicate
        pthread_mutex_unlock(&qLock);
        return cmdIgnore;
    }

    // New request, add it to render or dirty queue
    if ((req->cmd == cmdRender) && (reqNum < REQ_LIMIT)) {
        list = &reqHead;
        item->inQueue = queueRequest;
        reqNum++;
    } else if ((req->cmd == cmdRenderPrio) && (reqPrioNum < REQ_LIMIT)) {
        list = &reqPrioHead;
        item->inQueue = queueRequestPrio;
        reqPrioNum++;
    } else if ((req->cmd == cmdRenderBulk) && (reqBulkNum < REQ_LIMIT)) {
        list = &reqBulkHead;
        item->inQueue = queueRequestBulk;
        reqBulkNum++;
    } else if (dirtyNum < DIRTY_LIMIT) {
        list = &dirtyHead;
        item->inQueue = queueDirty;
        dirtyNum++;
        item->fd = FD_INVALID; // No response after render
    } else {
        // The queue is severely backlogged. Drop request
        stats.noReqDroped++;
        pthread_mutex_unlock(&qLock);
        free(item);
        return cmdNotDone;
    }

    if (list) {
        item->next = list;
        item->prev = list->prev;
        item->prev->next = item;
        list->prev = item;
        /* In addition to the linked list, add item to a hash table index
         * for faster lookup of pending requests.
         */
        insert_item_idx(item);

        pthread_cond_signal(&qCond);
    } else
        free(item);

    pthread_mutex_unlock(&qLock);

    return (list == &reqHead)?cmdIgnore:cmdNotDone;
}
Beispiel #4
0
int cmdParseScript(char *script, cmdScript_t *s, int diags)
{
   int idx, len, b, i, j, tags, resolved;
   int status;
   uint32_t p[10];
   cmdInstr_t instr;
   cmdCtlParse_t ctl;
   char v[CMD_MAX_EXTENSION];

   ctl.eaten = 0;

   status = 0;

   cmdTagStep_t tag_step[PI_MAX_SCRIPT_TAGS];

   len = strlen(script);

   /* calloc space for PARAMS, VARS, CMDS, and STRINGS */

   b = (sizeof(int) * (PI_MAX_SCRIPT_PARAMS + PI_MAX_SCRIPT_VARS)) +
       (sizeof(cmdInstr_t) * (len + 2) / 2) + len;

   s->par = calloc(1, b);

   if (s->par == NULL) return -1;

   s->var = s->par + PI_MAX_SCRIPT_PARAMS;

   s->instr = (cmdInstr_t *)(s->var + PI_MAX_SCRIPT_VARS);

   s->str_area = (char *)(s->instr + ((len + 2) / 2));

   s->str_area_len = len;
   s->str_area_pos = 0;

   s->instrs = 0;

   tags = 0;

   idx = 0;

   while (ctl.eaten<len)
   {
      idx = cmdParse(script, p, CMD_MAX_EXTENSION, v, &ctl);

      if (idx >= 0)
      {
         if (p[3])
         {
            memcpy(s->str_area + s->str_area_pos, v, p[3]);
            s->str_area[s->str_area_pos + p[3]] = 0;
            p[4] = (intptr_t) s->str_area + s->str_area_pos;
            s->str_area_pos += (p[3] + 1);
         }

         memcpy(&instr.p, p, sizeof(instr.p));

         if (instr.p[0] == PI_CMD_TAG)
         {
            if (tags < PI_MAX_SCRIPT_TAGS)
            {
               /* check tag not already used */
               for (j=0; j<tags; j++)
               {
                  if (tag_step[j].tag == instr.p[1])
                  {
                     if (diags)
                     {
                        fprintf(stderr, "Duplicate tag: %d\n", instr.p[1]);
                     }

                     if (!status) status = PI_DUP_TAG;
                     idx = -1;
                  }
               }

               tag_step[tags].tag = instr.p[1];
               tag_step[tags].step = s->instrs;
               tags++;
            }
            else
            {
               if (diags)
               {
                  fprintf(stderr, "Too many tags: %d\n", instr.p[1]);
               }
               if (!status) status = PI_TOO_MANY_TAGS;
               idx = -1;
            }
         }
      }
      else
      {
         if (diags)
         {
            if (idx == CMD_UNKNOWN_CMD)
               fprintf(stderr, "Unknown command: %s\n", cmdStr());
            else
               fprintf(stderr, "Bad parameter to %s\n", cmdStr());
         }
         if (!status) status = PI_BAD_SCRIPT_CMD;
      }

      if (idx >= 0)
      {
         if (instr.p[0] != PI_CMD_TAG)
         {
            memcpy(instr.opt, &ctl.opt, sizeof(instr.opt));
            s->instr[s->instrs++] = instr;
         }
      }
   }

   for (i=0; i<s->instrs; i++)
   {
      instr = s->instr[i];

      /* resolve jumps */

      if ((instr.p[0] == PI_CMD_JMP) || (instr.p[0] == PI_CMD_CALL) ||
          (instr.p[0] == PI_CMD_JZ)  || (instr.p[0] == PI_CMD_JNZ)  ||
          (instr.p[0] == PI_CMD_JM)  || (instr.p[0] == PI_CMD_JP))
      {
         resolved = 0;

         for (j=0; j<tags; j++)
         {
            if (instr.p[1] == tag_step[j].tag)
            {
               s->instr[i].p[1] = tag_step[j].step;
               resolved = 1;
               break;
            }
         }

         if (!resolved)
         {
            if (diags)
            {
               fprintf(stderr, "Can't resolve tag %d\n", instr.p[1]);
            }
            if (!status) status = PI_BAD_TAG;
         }
      }
   }
   return status;
}
  void
  onData(const ndn::Interest& interest, ndn::Data& data, std::string linkPrefix)
  {
    std::cout << "Data received for: " << interest.getName() << std::endl;
    CollectorData reply;

    reply.wireDecode(data.getContent().blockFromValue());

    if(reply.m_statusList.empty())
    {
      std::cerr << "received data is empty!!" << std::endl;
    }

    // get the list of the remote links requested for this prefix
    std::unordered_map<std::string,std::list<ndn::NdnMapServer::linkPair>>::const_iterator got = m_linksList.find(linkPrefix);

    if(got == m_linksList.end())
    {
      std::cerr << "failed to recognize the prefix: " << linkPrefix << std::endl;
    }
    else
    {
      std::list<ndn::NdnMapServer::linkPair> prefixList = got->second;

      for (unsigned i=0; i< reply.m_statusList.size(); i++)
      {
        int LinkId = 0;
        std::cout << "Reply: " << reply.m_statusList.at(i);

        // get the link id of the current IP
        for (auto pair = prefixList.cbegin(); pair != prefixList.cend(); ++pair)
        {
          if((*pair).linkIp == reply.m_statusList[i].getLinkIp())
          {
            if (DEBUG)
              std::cout << " Link ID for " << linkPrefix << " and " << reply.m_statusList[i].getLinkIp() << " is " << (*pair).linkId << std::endl;

            LinkId = (*pair).linkId;
          }
        }

        std::string cmdStr("http://");
        cmdStr += m_mapServerAddr;
        cmdStr += "/bw/";
        cmdStr += std::to_string(LinkId);
        cmdStr += "/" + reply.m_statusList[i].getTimestamp() + "/";
        cmdStr += std::to_string(reply.m_statusList[i].getTx() * 8) + "/";
        cmdStr += std::to_string(reply.m_statusList[i].getRx() * 8);

        if (DEBUG)
          std::cout << "cmd to pass to curl: " << cmdStr << std::endl;

        int status;
        // check for zombies
        waitpid(-1, &status, WNOHANG);
        int pid;
        if ((pid = fork()) < 0)
          printf("for failed for curl %s\n", cmdStr.c_str());
        else
        {
          if (pid == 0)
            execl("/usr/bin/curl","curl", "-s", "-L", cmdStr.c_str(), NULL);
        }
        // check for zombies again
        waitpid(-1, &status, WNOHANG);

      }
      reply.m_statusList.clear();
    }
  }
Beispiel #6
0
int main(int argc , char *argv[])
{
   int sock, command;
   int idx, i, pp, l, len;
   cmdCmd_t cmd;
   uint32_t p[CMD_P_ARR];
   cmdCtlParse_t ctl;
   cmdScript_t s;
   char v[CMD_MAX_EXTENSION];

   sock = openSocket();

   command_buf[0] = 0;
   l = 0;
   pp = 0;

   for (i=1; i<argc; i++)
   {
      l += (strlen(argv[i]) + 1);
      if (l < sizeof(command_buf))
         {sprintf(command_buf+pp, "%s ", argv[i]); pp=l;}
   }

   if (pp) {command_buf[--pp] = 0;}

   ctl.eaten = 0;

   len = strlen(command_buf);
   idx = 0;

   while ((idx >= 0) && (ctl.eaten < len))
   {
      if ((idx=cmdParse(command_buf, p, CMD_MAX_EXTENSION, v, &ctl)) >= 0)
      {
         command = p[0];

         if (command < PI_CMD_SCRIPT)
         {
            if (command == PI_CMD_HELP)
            {
               printf(cmdUsage);
            }
            else if (command == PI_CMD_PARSE)
            {
               cmdParseScript(v, &s, 1);
               if (s.par) free (s.par);
            }
            else
            {
               cmd.cmd = command;
               cmd.p1 = p[1];
               cmd.p2 = p[2];
               cmd.p3 = p[3];

               if (sock != SOCKET_OPEN_FAILED)
               {
                  if (send(sock, &cmd, sizeof(cmdCmd_t), 0) ==
                     sizeof(cmdCmd_t))
                  {
                     if (p[3]) send(sock, v, p[3], 0); /* send extensions */

                     if (recv(sock, &cmd, sizeof(cmdCmd_t), MSG_WAITALL) ==
                        sizeof(cmdCmd_t))
                     {
                        get_extensions(sock, command, cmd.res);

                        print_result(sock, cmdInfo[idx].rv, cmd);
                     }
                     else fatal("socket receive failed");
                  }
                  else fatal("socket send failed");
               }
               else fatal("socket connect failed");
            }
         }
         else fatal("%s only allowed within a script", cmdInfo[idx].name);
      }
      else
      {
         if (idx == CMD_UNKNOWN_CMD)
            fatal("%s? unknown command, pigs h for help", cmdStr());
         else
            fatal("%s: bad parameter, pigs h for help", cmdStr());
      }
   }

   if (sock >= 0) close(sock);

   return 0;
}