string RadomFormula::GetFormula()
{
	p1 = rand() % 30;
	p2 = rand() % 30;
	op1 = rand() % OpArr.size();

	int pi = Operate(op1, p1, p2);
	int s1 = -pi;
	int s2 = 30 - pi;
	if (s1 < -30)
	{
		s1 = -30;
	}
	if (s2 > 30)
	{
		s2 = 30;
	}
	p3 = s1 + rand() % (s2 - s1);
	if (p3 >= 0)
	{
		op2 = 0;
	}
	else
	{
		op2 = 1; p3 = -p3;
	}
	return Transport(p1) + OpArr[op1] + Transport(p2) + OpArr[op2] + Transport(p3) + "=";
}
status_t BnRTCNode::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
     switch(code) {
        case kIDTransport: {
            rtc_binder_data_t out;
            out.data = &data;
            out.reply = reply;
            // Reserved 4 bytes;
            reply->writeInt32(0);
            int ret = Transport(&out);
            int pos = reply->dataPosition();
            reply->setDataPosition(0);
            reply->writeInt32(ret);
            reply->setDataPosition(pos);
            return NO_ERROR;
        } break;
        case kIDResigerObs: {
            CHECK_INTERFACE(IRTCNode, data, reply);
            sp<IBinder> handle = data.readStrongBinder();
            sp<IRTCNode> node = interface_cast<IRTCNode>(handle);
            int ret = RegisterObserver(node);
            reply->writeInt32(ret);
            return NO_ERROR;
        }break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
status_t BnRTCBinder::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
     switch(code) {
        case kDefault_CALL_ID: {
            rtc_binder_data_t out;
            out.data = &data;
            out.reply = reply;
            // Reserved 4 bytes
            reply->writeInt32(0);
            int ret = Transport(&out);
            int pos = reply->dataPosition();
            reply->setDataPosition(0);
            // Rewrite 4 bytes
            reply->writeInt32(ret);
            reply->setDataPosition(pos);
            return NO_ERROR;
        } break;
       case kDefault_CreateNodeID: {
            CHECK_INTERFACE(IRTCBinder, data, reply);
            int type = 0;
            type = data.readInt32();
            sp<IRTCNode> node = NewNode(type);
#if ANDROID_SDK_VERSION >=23
            reply->writeStrongBinder(node->asBinder(node.get()));
#else
            reply->writeStrongBinder(node->asBinder());
#endif
            return NO_ERROR;
       } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
Exemple #4
0
    vector<Input::MetaCode> EventUserInput::AddCodesFromRawEvent(const RawEvent& RawEvent_)
    {
        vector<Input::MetaCode> Results;
        switch(RawEvent_.type)
        {
            case SDL_KEYUP:         case SDL_KEYDOWN:
            case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN:
            case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP:
                Results.push_back(this->AddCode(RawEvent_));
                break;

            case SDL_MOUSEMOTION:{       //Can contain Multiple Metacodes
                std::vector<Input::MetaCode> Transport(this->AddCodesFromSDLMouseMotion(RawEvent_));
                Results.insert(Results.end(), Transport.begin(),Transport.end());
                break;}

            case SDL_JOYAXISMOTION: {       //Can contain Multiple Metacodes
                std::vector<Input::MetaCode> Transport(this->AddCodesFromSDLJoyStickMotion(RawEvent_));
                Results.insert(Results.end(), Transport.begin(),Transport.end());
                break;}

            case SDL_JOYBALLMOTION:{
                std::vector<Input::MetaCode> Transport(this->AddCodeFromSDLJoyStickBall(RawEvent_));
                Results.insert(Results.end(), Transport.begin(),Transport.end());
                break;}

            case SDL_JOYHATMOTION:{
                std::vector<Input::MetaCode> Transport(this->AddCodeFromSDLJoyStickHat(RawEvent_));
                Results.insert(Results.end(), Transport.begin(),Transport.end());
                break;}

            default:
                MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Unknown SDL Event Inserted");
                break;
        }

        return Results;
    }
nsresult
nsHttpPipeline::FillSendBuf()
{
    // reads from request queue, moving transactions to response queue
    // when they have been completely read.

    nsresult rv;
    
    if (!mSendBufIn) {
        // allocate a single-segment pipe
        rv = NS_NewPipe(getter_AddRefs(mSendBufIn),
                        getter_AddRefs(mSendBufOut),
                        nsIOService::gDefaultSegmentSize,  /* segment size */
                        nsIOService::gDefaultSegmentSize,  /* max size */
                        true, true);
        if (NS_FAILED(rv)) return rv;
    }

    PRUint32 n, avail;
    nsAHttpTransaction *trans;
    nsITransport *transport = Transport();

    while ((trans = Request(0)) != nsnull) {
        avail = trans->Available();
        if (avail) {
            // if there is already a response in the responseq then this
            // new data comprises a pipeline. Update the transaction in the
            // response queue to reflect that if necessary. We are now sending
            // out a request while we haven't received all responses.
            nsAHttpTransaction *response = Response(0);
            if (response && !response->PipelinePosition())
                response->SetPipelinePosition(1);
            rv = trans->ReadSegments(this, avail, &n);
            if (NS_FAILED(rv)) return rv;
            
            if (n == 0) {
                LOG(("send pipe is full"));
                break;
            }

            mSendingToProgress += n;
            if (!mSuppressSendEvents && transport) {
                // Simulate a SENDING_TO event
                trans->OnTransportStatus(transport,
                                         NS_NET_STATUS_SENDING_TO,
                                         mSendingToProgress);
            }
        }

        avail = trans->Available();
        if (avail == 0) {
            // move transaction from request queue to response queue
            mRequestQ.RemoveElementAt(0);
            mResponseQ.AppendElement(trans);
            mRequestIsPartial = false;

            if (!mSuppressSendEvents && transport) {
                // Simulate a WAITING_FOR event
                trans->OnTransportStatus(transport,
                                         NS_NET_STATUS_WAITING_FOR,
                                         mSendingToProgress);
            }

            // It would be good to re-enable data read handlers via ResumeRecv()
            // except the read handler code can be synchronously dispatched on
            // the stack.
        }
        else
            mRequestIsPartial = true;
    }
    return NS_OK;
}
nsresult
nsHttpPipeline::WriteSegments(nsAHttpSegmentWriter *writer,
                              PRUint32 count,
                              PRUint32 *countWritten)
{
    LOG(("nsHttpPipeline::WriteSegments [this=%x count=%u]\n", this, count));

    NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");

    if (mClosed)
        return NS_SUCCEEDED(mStatus) ? NS_BASE_STREAM_CLOSED : mStatus;

    nsAHttpTransaction *trans; 
    nsresult rv;

    trans = Response(0);
    // This code deals with the establishment of a CONNECT tunnel through
    // an HTTP proxy. It allows the connection to do the CONNECT/200
    // HTTP transaction to establish an SSL tunnel as a precursor to the
    // actual pipeline of regular HTTP transactions.
    if (!trans && mRequestQ.Length() &&
        mConnection->IsProxyConnectInProgress()) {
        LOG(("nsHttpPipeline::WriteSegments [this=%p] Forced Delegation\n",
             this));
        trans = Request(0);
    }

    if (!trans) {
        if (mRequestQ.Length() > 0)
            rv = NS_BASE_STREAM_WOULD_BLOCK;
        else
            rv = NS_BASE_STREAM_CLOSED;
    }
    else {
        // 
        // ask the transaction to consume data from the connection.
        // PushBack may be called recursively.
        //
        rv = trans->WriteSegments(writer, count, countWritten);

        if (rv == NS_BASE_STREAM_CLOSED || trans->IsDone()) {
            trans->Close(NS_OK);

            // Release the transaction if it is not IsProxyConnectInProgress()
            if (trans == Response(0)) {
                NS_RELEASE(trans);
                mResponseQ.RemoveElementAt(0);
                mResponseIsPartial = false;
                ++mHttp1xTransactionCount;
            }

            // ask the connection manager to add additional transactions
            // to our pipeline.
            nsRefPtr<nsHttpConnectionInfo> ci;
            GetConnectionInfo(getter_AddRefs(ci));
            if (ci)
                gHttpHandler->ConnMgr()->ProcessPendingQForEntry(ci);
        }
        else
            mResponseIsPartial = true;
    }

    if (mPushBackLen) {
        nsHttpPushBackWriter writer(mPushBackBuf, mPushBackLen);
        PRUint32 len = mPushBackLen, n;
        mPushBackLen = 0;

        // This progress notification has previously been sent from
        // the socket transport code, but it was delivered to the
        // previous transaction on the pipeline.
        nsITransport *transport = Transport();
        if (transport)
            OnTransportStatus(transport,
                              nsISocketTransport::STATUS_RECEIVING_FROM,
                              mReceivingFromProgress);

        // the push back buffer is never larger than NS_HTTP_SEGMENT_SIZE,
        // so we are guaranteed that the next response will eat the entire
        // push back buffer (even though it might again call PushBack).
        rv = WriteSegments(&writer, len, &n);
    }

    return rv;
}
Exemple #7
0
void Army::Update( Logic& l, float delta_time, int i )
{
	if( !used )
		return;

	int farm = l.GetFarmIndex( from );
	int city = l.GetCityIndex( from );
	int structure = l.GetStructureIndex( from );

	// Feed
	if( state == Stationary && city != -1 )
	{
		City& c( l.GetCityByIndex( city ) );
		if( hunger > 0 )
		{
			c.food_contained -= hunger;
			hunger = 0;
		}
		c.food_contained -= food_consumed * delta_time;
	}
	else if( state == Stationary && farm != -1 )
	{
		Farm& f( l.GetFarmByIndex( farm ) );
		if( hunger > 0 )
		{
			f.food -= hunger;
			hunger = 0;
		}
		f.food -= food_consumed * delta_time;
	}
	else
		if( food_stored <= 0 )
		{
			hunger += food_consumed * delta_time;
			float limit = soldiers + carts;
			if( hunger > limit )
			{
				// Start the starving... muahahahaha.
				if( soldiers )
					soldiers -= soldiers * 0.1 <= 1 ? 1: soldiers * 0.1;
				if( carts )
					carts -= carts * 0.2 <= 1 ? 1: carts * 0.1;
				limit = soldiers + carts;
				hunger = limit - limit / 10;
				Calculate();

				if( soldiers + carts < 1 )
				{
					rectangles[ (int)Type::Army ].erase( l.GetArmies()[i].rectangle );
					l.GetArmies().erase( i );
					return;
				}
			}
		}
		else
		{
			if( hunger > 0 )
			{
				food_stored -= hunger;
				hunger = 0;
			}
			food_stored -= food_consumed * delta_time;
		}


	if( state == Moving )
	{
		Point& p = l.GetPoint( to );
		float distance = speed * delta_time;
		float d_x = p.x - x;
		float d_y = p.y - y;
		if( d_y == 0 )
			d_y = 0.0001;
		float t = Logic::L(d_x) / ( Logic::L(d_x) + Logic::L(d_y) );
		float x_ = (p.x > x ? distance : -distance) * t;
		float y_ = (p.y > y ? distance : -distance) * (1-t);
		if( Logic::L(p.x - x) <= Logic::L(x_) )
			x = p.x;
		if( Logic::L(p.y - y) <= Logic::L(y_) )
			y = p.y;

		if( x == p.x && y == p.y )
		{
			if( transporting != Resource::Nothing )
				Transport( l, delta_time );
			else
				Move( l, delta_time, i );
		}
		else
		{
			x += x_;
			y += y_;
		}
		Calculate();
	}
	else if( state == CollectFood && farm != -1 )
	{
		Farm& f( l.GetFarmByIndex( farm ) );
		float collected = soldiers * delta_time;
		f.food -= collected;
		food_stored += collected;
		if( food_stored > storage_capacity ) // TODO: Change later when more resources can be transported
			state = Stationary;
	}
	else if( state == CollectPeople )
	{
		if( farm != -1 )
		{
			Farm& f( l.GetFarmByIndex( farm ) );
			float collected = soldiers * 10 * delta_time;
			people += collected;
			f.population -= collected;
			if( people > people_max )
			{
				people = people_max;
				state = Stationary;
			}
			else if( f.population < 0 )
			{
				f.population = 0;
				state = Stationary;
			}
		}
		else if( city != -1 )
		{
			City& c( l.GetCityByIndex( city ) );
			float collected = soldiers * 10 * delta_time;
			people += collected;
			c.population -= collected;
			if( people > people_max )
			{
				people = people_max;
				state = Stationary;
			}
			else if( c.population < 0 )
			{
				c.population = 0;
				state = Stationary;
			}
		}
		else if( structure != -1 )
		{
			Structure& s( l.GetStructureByIndex( structure ) );
			float collected = soldiers * 10 * delta_time;
			people += collected;
			s.population -= collected;
			if( people > people_max )
			{
				people = people_max;
				state = Stationary;
			}
			else if( s.population < 0 )
			{
				s.population = 0;
				state = Stationary;
			}
		}
	}
	else if( state == KillPeople )
	{
		if( structure != -1 )
		{
			Structure& s( l.GetStructureByIndex( structure ) );
			s.population -= soldiers * 100 * delta_time;
			if( s.population < 0 )
			{
				s.population = 0;
				state = Stationary;
			}
		}
		else if( city != -1 )
		{
			City& c( l.GetCityByIndex( city ) );
			c.population -= soldiers * 100 * delta_time;
			if( c.population < 0 )
			{
				c.population = 0;
				state = Stationary;
			}
		}
		else if( farm != -1 )
		{
			Farm& f( l.GetFarmByIndex( farm ) );
			f.population -= soldiers * 100 * delta_time;
			if( f.population < 0 )
			{
				f.population = 0;
				state = Stationary;
			}
		}
	}
	else if( state == DestroyFarm && farm != -1 )
	{
		Farm& f( l.GetFarmByIndex( farm ) );
		Rectangle& r( rectangles[ (int)Type::Farm ][f.rectangle] );

		float size = r.scale * r.scale;
		size -= soldiers * 0.1 * delta_time;
		if( size <= 0.1 )
		{
			l.RemoveFarm( from );
			state = Stationary;
		}
		else
		{
			r.scale = size / r.scale;
			f.Calculate();
		}
	}
	else if( state == DestroyCity && city != -1 )
	{
		City& c( l.GetCityByIndex( city ) );
		Rectangle& r( rectangles[ (int)Type::City ][c.rectangle] );

		float size = r.scale * r.scale;
		size -= soldiers * 0.1 * delta_time;
		if( size <= 0.1 )
		{
			l.RemoveCity( from );
			state = Stationary;
		}
		else
		{
			r.scale = size / r.scale;
			c.Calculate();
		}
	}
	else if( state == DestroyStructure && structure != -1 )
	{
		Structure& s( l.GetStructureByIndex( structure ) );
		Rectangle& r( rectangles[ (int)Type::Structure ][s.rectangle] );

		if( s.build_progress <= 0.1 )
		{
			l.RemoveStructure( from );
			state = Stationary;
		}
		else
			s.build_progress -= soldiers * delta_time;
	}
	else if( state == Fighting )
	{
		// TODO: Add later
	}
}
Exemple #8
0
nsresult
nsHttpPipeline::FillSendBuf()
{
    // reads from request queue, moving transactions to response queue
    // when they have been completely read.

    nsresult rv;

    if (!mSendBufIn) {
        // allocate a single-segment pipe
        rv = NS_NewPipe(getter_AddRefs(mSendBufIn),
                        getter_AddRefs(mSendBufOut),
                        nsIOService::gDefaultSegmentSize,  /* segment size */
                        nsIOService::gDefaultSegmentSize,  /* max size */
                        true, true);
        if (NS_FAILED(rv)) return rv;
    }

    PRUint32 n, avail;
    nsAHttpTransaction *trans;
    nsITransport *transport = Transport();

    while ((trans = Request(0)) != nsnull) {
        avail = trans->Available();
        if (avail) {
            rv = trans->ReadSegments(this, avail, &n);
            if (NS_FAILED(rv)) return rv;

            if (n == 0) {
                LOG(("send pipe is full"));
                break;
            }

            mSendingToProgress += n;
            if (!mSuppressSendEvents && transport) {
                // Simulate a SENDING_TO event
                trans->OnTransportStatus(transport,
                                         NS_NET_STATUS_SENDING_TO,
                                         mSendingToProgress);
            }
        }

        avail = trans->Available();
        if (avail == 0) {
            // move transaction from request queue to response queue
            mRequestQ.RemoveElementAt(0);
            mResponseQ.AppendElement(trans);
            mRequestIsPartial = false;

            if (!mSuppressSendEvents && transport) {
                // Simulate a WAITING_FOR event
                trans->OnTransportStatus(transport,
                                         NS_NET_STATUS_WAITING_FOR,
                                         mSendingToProgress);
            }
        }
        else
            mRequestIsPartial = true;
    }
    return NS_OK;
}
Exemple #9
0
nsresult
nsHttpPipeline::WriteSegments(nsAHttpSegmentWriter *writer,
                              PRUint32 count,
                              PRUint32 *countWritten)
{
    LOG(("nsHttpPipeline::WriteSegments [this=%x count=%u]\n", this, count));

    NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");

    if (mClosed)
        return NS_SUCCEEDED(mStatus) ? NS_BASE_STREAM_CLOSED : mStatus;

    nsAHttpTransaction *trans;
    nsresult rv;

    trans = Response(0);
    if (!trans) {
        if (mRequestQ.Length() > 0)
            rv = NS_BASE_STREAM_WOULD_BLOCK;
        else
            rv = NS_BASE_STREAM_CLOSED;
    }
    else {
        //
        // ask the transaction to consume data from the connection.
        // PushBack may be called recursively.
        //
        rv = trans->WriteSegments(writer, count, countWritten);

        if (rv == NS_BASE_STREAM_CLOSED || trans->IsDone()) {
            trans->Close(NS_OK);
            NS_RELEASE(trans);
            mResponseQ.RemoveElementAt(0);
            mResponseIsPartial = false;
            ++mHttp1xTransactionCount;

            // ask the connection manager to add additional transactions
            // to our pipeline.
            gHttpHandler->ConnMgr()->AddTransactionToPipeline(this);
        }
        else
            mResponseIsPartial = true;
    }

    if (mPushBackLen) {
        nsHttpPushBackWriter writer(mPushBackBuf, mPushBackLen);
        PRUint32 len = mPushBackLen, n;
        mPushBackLen = 0;

        // This progress notification has previously been sent from
        // the socket transport code, but it was delivered to the
        // previous transaction on the pipeline.
        nsITransport *transport = Transport();
        if (transport)
            OnTransportStatus(transport,
                              nsISocketTransport::STATUS_RECEIVING_FROM,
                              mReceivingFromProgress);

        // the push back buffer is never larger than NS_HTTP_SEGMENT_SIZE,
        // so we are guaranteed that the next response will eat the entire
        // push back buffer (even though it might again call PushBack).
        rv = WriteSegments(&writer, len, &n);
    }

    return rv;
}