コード例 #1
0
ファイル: inbandstream.cpp プロジェクト: ChALkeR/vacuum-im
bool InBandStream::sendNextPaket(bool AFlush)
{
	bool sent = false;
	if (isOpen() && FDataIqRequestId.isEmpty() && (bytesToWrite()>=FBlockSize || AFlush))
	{
		FThreadLock.lockForWrite();
		QByteArray data = FWriteBuffer.read(FBlockSize);
		FThreadLock.unlock();

		if (!data.isEmpty())
		{
			if (FStanzaProcessor)
			{
				Stanza paket(FStanzaType==StanzaMessage ? STANZA_KIND_MESSAGE : STANZA_KIND_IQ);
				paket.setTo(FContactJid.full()).setUniqueId();

				QDomElement dataElem = paket.addElement("data",NS_INBAND_BYTESTREAMS);
				dataElem.setAttribute("sid",FStreamId);
				dataElem.setAttribute("seq",FSeqOut);
				dataElem.appendChild(paket.createTextNode(QString::fromUtf8(data.toBase64())));

				if (FStanzaType == StanzaMessage)
				{
					QDomElement ampElem = paket.addElement("amp","http://jabber.org/protocol/amp");
					QDomElement ruleElem = ampElem.appendChild(paket.createElement("rule")).toElement();
					ruleElem.setAttribute("condition","deliver");
					ruleElem.setAttribute("value","stored");
					ruleElem.setAttribute("action","error");
					ruleElem = ampElem.appendChild(paket.createElement("rule")).toElement();
					ruleElem.setAttribute("condition","match-resource");
					ruleElem.setAttribute("value","exact");
					ruleElem.setAttribute("action","error");

					DataEvent *dataEvent = new DataEvent(AFlush);
					QCoreApplication::postEvent(this, dataEvent);

					sent = FStanzaProcessor->sendStanzaOut(FStreamJid,paket);
				}
				else
				{
					paket.setType(STANZA_TYPE_SET);
					FDataIqRequestId = paket.id();
					sent = FStanzaProcessor->sendStanzaRequest(this,FStreamJid,paket,DATA_TIMEOUT);
				}
			}

			if (sent)
			{
				FSeqOut = FSeqOut<USHRT_MAX ? FSeqOut+1 : 0;
				emit bytesWritten(data.size());
				FBytesWrittenCondition.wakeAll();
			}
			else
			{
				abort(XmppError(IERR_INBAND_STREAM_DATA_NOT_SENT));
			}
		}
	}
	return sent;
}
コード例 #2
0
bool gateway::process(paket &pkg)
{
  bool processOk = false;

  if(pkg.opcode != K_OP_LEER)
  {

    switch(state)
    {
      case Zustaende::IDLE:
        // sollte nicht passieren
        throw "gateway::process(): case Zustaende::IDLE";
        break;

      case Zustaende::WAIT_RAM_RRF:
        if((pkg.opcode == K_OP_RFF) && (pkg.sender == ramId))
        {
          pkg = paket(K_OP_NXT, id, ramId, 0, 0, 0);
          state = Zustaende::PROCESSING;
          processOk = true;
          PRINT_DEBUG("gateway - RFF erhalten");
        }
        else
        {
          // sollte nicht passieren
          throw "gateway::process(): case Zustaende::WAIT_RAM_RRF";
        }
        break;

      case Zustaende::PROCESSING:
        if((pkg.opcode == K_OP_NXA) && (pkg.sender == ramId))
        {
          PRINT_DEBUG("gateway - NXA erhalten");
          if(!pixelBuffer->full())
          {
            pixelBuffer->push(pkg);
            // kein setzen von processOk, sendeBuffer wird intern verwaltet
          }
          else
          {
            // todo errorhandling
            throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_NXA";
          }

          // sende weitere pixelanfragen an den ram (sofern noch platz im buffer ist)
          if(!pixelBuffer->full())
          {
            pkg = paket(K_OP_NXT, id, ramId, 0, 0, 0);
            if(!sendeBuffer->push(pkg))
            {
              // todo error
              throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_NXA, sendeBuffer voll 1";
            }
          }

          // sende alle verfuegbaren pixel an idle compute-module
          while((!pixelBuffer->empty()) && (!computeListAllBusy()))
          {
            pixelBuffer->pop(pkg);
            pkg = paket(K_OP_EXE, id, getAndSetNextIdleCompute(), pkg.xpos,
                pkg.ypos, pkg.color); // neues paket, damit id automatisch angelegt wird
            if(!sendeBuffer->push(pkg))
            {
              // todo error (z.B. throw, oder pkg zurueckschreiben und break
              throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_NXA, sendeBuffer voll 2";
            }
          }
        }
        else if(pkg.opcode == K_OP_FIN)
        {
          PRINT_DEBUG("gateway - FIN erhalten");
          setComputeIdle(pkg.sender);

          // sende weitere pixelanfragen an den ram (sofern noch platz im buffer ist)
          if(!pixelBuffer->full())
          {
            pkg = paket(K_OP_NXT, id, ramId, 0, 0, 0);
            if(!sendeBuffer->push(pkg))
            {
              // todo error
              throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_FIN, sendeBuffer voll 1";
            }
          }

          // sende alle verfuegbaren pixel an idle compute-module
          while((!pixelBuffer->empty()) && (!computeListAllBusy()))
          {
            pixelBuffer->pop(pkg);
            pkg = paket(K_OP_EXE, id, getAndSetNextIdleCompute(), pkg.xpos,
                pkg.ypos, pkg.color); // neues paket, damit id automatisch angelegt wird
            if(!sendeBuffer->push(pkg))
            {
              // todo error (z.B. throw, oder pkg zurueckschreiben und break
              throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_FIN, sendeBuffer voll 2";
            }
          }
        }
        else if((pkg.opcode == K_OP_END) && (pkg.sender == ramId))
        {
          PRINT_DEBUG("gateway - END erhalten");
          //ggf. entfernen, da ram die info zuerst bekommen haben kann
          if(pixelBuffer->empty() && computeListAllIdle())
          {
            pkg = paket(K_OP_WFI, id, ramId, 0, 0, 0);
            state = Zustaende::WAIT_RAM_WFF;
            processOk = true;
          }
          else
          {
            // todo error handling
            throw "gateway::process(): case Zustaende::PROCESSING, opcode == K_OP_END";
          }
        }
        else
        {
          // sollte nicht passieren
          throw "gateway::process(): case Zustaende::PROCESSING, opcode == else";
        }
        break;

      case Zustaende::WAIT_RAM_WFF:
        if((pkg.opcode == K_OP_WFF) && (pkg.sender == ramId))
        {
          PRINT_DEBUG("gateway - Beende Process");
          state = Zustaende::IDLE;
          sc_stop();
          // kein setzen von processOk, da kein paketsenden noetig
        }
        else
        {
          // sollte nicht passieren
          throw "gateway::process(): case Zustaende::WAIT_RAM_WFF";
        }
        break;

      default:
        // sollte nicht passieren
        throw "gateway::process(): case default";
        break;
    }
  }

  return processOk;
}