OCRepresentation parseGetSetCallback(OCClientResponse* clientResponse)
    {
        if(clientResponse->payload == nullptr ||
                (
                    clientResponse->payload->type != PAYLOAD_TYPE_DEVICE &&
                    clientResponse->payload->type != PAYLOAD_TYPE_PLATFORM &&
                    clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION
                )
          )
        {
            //OCPayloadDestroy(clientResponse->payload);
            return OCRepresentation();
        }

        MessageContainer oc;
        oc.setPayload(clientResponse->payload);
        //OCPayloadDestroy(clientResponse->payload);

        std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
        if(it == oc.representations().end())
        {
            return OCRepresentation();
        }

        // first one is considered the root, everything else is considered a child of this one.
        OCRepresentation root = *it;
        ++it;

        std::for_each(it, oc.representations().end(),
                [&root](const OCRepresentation& repItr)
                {root.addChild(repItr);});
        return root;

    }
    OCRepresentation parseRDResponseCallback(OCClientResponse* clientResponse)
    {
        if (nullptr == clientResponse || nullptr == clientResponse->payload ||
                    PAYLOAD_TYPE_RD != clientResponse->payload->type)
        {
            return OCRepresentation();
        }

        MessageContainer oc;
        oc.setPayload(clientResponse->payload);

        std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
        if (it == oc.representations().end())
        {
            return OCRepresentation();
        }

        // first one is considered the root, everything else is considered a child of this one.
        OCRepresentation root = *it;
        root.setDevAddr(clientResponse->devAddr);
        root.setUri(clientResponse->resourceUri);
        ++it;

        std::for_each(it, oc.representations().end(),
                [&root](const OCRepresentation& repItr)
                {root.addChild(repItr);});
        return root;

    }
void TextBox::Add(std::string message)
{
	_messages.push_back(message);
	if (_messages.size() >= 5) {
		_messages.erase(_messages.begin());
	}
}
OCRepresentation parseGetSetCallback(OCClientResponse* clientResponse)
{
    if(clientResponse->resJSONPayload == nullptr || clientResponse->resJSONPayload[0] == '\0')
    {
        return OCRepresentation();
    }

    MessageContainer oc;
    oc.setJSONRepresentation(clientResponse->resJSONPayload);

    std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
    if(it == oc.representations().end())
    {
        return OCRepresentation();
    }

    // first one is considered the root, everything else is considered a child of this one.
    OCRepresentation root = *it;
    ++it;

    std::for_each(it, oc.representations().end(),
                  [&root](const OCRepresentation& repItr)
    {
        root.addChild(repItr);
    });
    return root;

}
Beispiel #5
0
bool Client::ProcessPendingMessage()
{
    MessageContainer messages;
    if (!zeromq::ReceiveMessage(*this->client_sock, messages, ZMQ_DONTWAIT)) {
        return false;
    }

    return this->ProcessResponseMessage(messages.GetMessages());
}
    OCPayload* InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
    {
        MessageContainer ocInfo;
        ocInfo.addRepresentation(rep);
        for(const OCRepresentation& r : rep.getChildren())
        {
            ocInfo.addRepresentation(r);
        }

        return reinterpret_cast<OCPayload*>(ocInfo.getPayload());
    }
    /// Validates that a protocol request message has been sent
    ///
    /// Expects the request message to have a single protocol envelope with a
    /// single embedded message of the enumeration specified and serialized to
    /// exactly what we specify
    void ExpectRequestMessage(uint32 type, ::google::protobuf::Message &expected)
    {
        ASSERT_TRUE(this->socket != NULL);

        ::zmq::pollitem_t pollitem;
        pollitem.events = ZMQ_POLLIN;
        pollitem.socket = *this->socket;
        pollitem.fd = 0;
        pollitem.revents = 0;

        ASSERT_EQ(1, ::zmq::poll(&pollitem, 1, 1000))
            << "Request message sent in orderly manner";

        MessageContainer messages;
        ASSERT_TRUE(zeromq::ReceiveMessage(*this->socket, messages));

        ASSERT_EQ(1, messages.IdentitiesSize()) << "client sent identities as part of request";
        ASSERT_EQ(1, messages.MessagesSize()) << "client sent 1 message with content";

        message_t msg;
        msg.copy(messages[0]);

        ASSERT_GT(msg.size(), 1) << "sent message has content";

        char msg_version = 0;
        memcpy(&msg_version, msg.data(), 1);
        EXPECT_EQ(0x01, msg_version) << "sent message has protocol header";

        Envelope e;

        EXPECT_NO_THROW(e = Envelope(msg, 1)) << "envelope could be deserialized";

        EXPECT_EQ(1, e.MessageCount()) << "envelope contains one message";
        EXPECT_EQ(::zippylog::message_namespace, e.MessageNamespace(0)) << "message of proper namespace";
        EXPECT_EQ(type, e.MessageType(0)) << "message of proper type";

        EXPECT_EQ(1, e.TagSize()) << "envelope has tag";

        ::google::protobuf::Message *sent = (::google::protobuf::Message *)e.GetMessage(0);
        ASSERT_TRUE(sent != NULL) << "able to deserialize embedded message";

        string serialized_expected, serialized_actual;
        EXPECT_TRUE(expected.SerializeToString(&serialized_expected));
        EXPECT_TRUE(sent->SerializeToString(&serialized_actual));

        EXPECT_EQ(serialized_expected, serialized_actual) << "sent request message matches expected";
    }
Beispiel #8
0
    Message::type ParseContext::handleError(
            const std::string& errorKey,
            MessageContainer& msgContainer,
            const std::string& msg ) const {

        InputError::Action action = get( errorKey );

        if (action == InputError::WARN) {
            msgContainer.warning(msg);
            return Message::Warning;
        }

        else if (action == InputError::THROW_EXCEPTION) {
            msgContainer.error(msg);
            throw std::invalid_argument(errorKey + ": " + msg);
        }

        return Message::Debug;

    }
    /// Helper to respond to a subscription request message
    void RespondToSubscriptionRequest(string const &)
    {
        MessageContainer messages;
        ASSERT_TRUE(zeromq::ReceiveMessage(*this->socket, messages));

        message_t msg;
        msg.copy(messages[0]);

        Envelope request(msg, 1);

        Envelope response;
        for (int i = 0; i < request.TagSize(); i++) {
            response.AddTag(request.GetTag(i));
        }

        protocol::response::SubscriptionAcceptAckV1 ack;
        ack.set_id("foobar");
        ack.set_ttl(10000);

        ack.add_to_envelope(response);

        zeromq::SendEnvelope(*this->socket, messages.GetIdentities(), response, true, 0);
    }
Beispiel #10
0
bool ReceiveMessage(::zmq::socket_t &socket, MessageContainer &container, int flags)
{
    container.Clear();

    bool in_identity = true;

    while (true) {
        message_t *msg = new message_t();
        if (!socket.recv(msg, flags)) {
            delete msg;
            return false;
        }

        if (msg->size() == 0) {
            in_identity = false;
            delete msg;
            continue;
        }

        if (in_identity) {
            container.AddIdentity(msg);
        }
        else {
            container.AddMessage(msg);
        }

        int32 more;
        size_t moresz;
        moresz = sizeof(more);
        socket.getsockopt(ZMQ_RCVMORE, &more, &moresz);

        if (!more) break;
    }

    return true;
}
    void ClientTransmissionManager::ReallySendMessage(MessageContainer& message)
	{
		std::stringstream s;

        if(this->mIsConnected && this->mClient!=NULL)
        {
		    ENetPacket* packet=NULL;
			bool reliable=message.GetReliable();
			bool sent=false;
			message.UpdateMessageData(); //Only needed for some messages but it is safe to call because it is not purely virtual in base class.
            if(message.GetMessageUnion().unsigned8BitIntegerPointer!=NULL)
            {
				try
				{
					size_t messageType = message.GetType();
					size_t messageChannel = message.GetChannel();
					size_t messageLength = message.GetLength();

					//for debugging mostly.
					if(messageChannel!=CHANNEL_VOICE_CHAT)
					{
						s << "Sending message of type " << messageType << " across channel " << messageChannel << " that was " << messageLength << " long.";
						this->GetClientSessionManager().Log(s);
					}

					packet = enet_packet_create(
						message.GetMessageUnion().unsigned8BitIntegerPointer,
						messageLength,
						reliable?ENET_PACKET_FLAG_RELIABLE:ENET_PACKET_FLAG_UNSEQUENCED);

					if(packet!=NULL)
					{
						this->ReallySendMessage(this->mPeer,messageChannel,packet);
						sent=true;
						enet_host_flush(this->mClient);
					} 
				}
				catch(...)
				{
					if(!sent && packet!=NULL)
					{
						enet_packet_destroy(packet);
					}
					throw;
				}
            }
        }
	}
std::string InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
{
    MessageContainer ocInfo;
    ocInfo.addRepresentation(rep);
    return ocInfo.getJSONRepresentation(OCInfoFormat::IncludeOC);
}