0

AJTCL:Message from server to client

asked 2014-11-11 05:59:40 -0700

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi Geeks, I am dealing with ajtcl source code. In that basic_client is sending message to basic_service. I am modifying the code as my server has to send message to client. I am getting different different error messages as i do modify everytime.Can anyone help me the flow of functions that i have to use to overcome this problem. Thanks in advance.

Regards, shashi.

Client code :

#define AJ_MODULE BASIC_CLIENT

#include <stdio.h>
#include <stdlib.h>
#include <aj_debug.h>
#include <alljoyn.h>

static const char ServiceName[] = "org.alljoyn.Bus.sample";
static const char ServicePath[] = "/sample";
static const uint16_t ServicePort = 25;

uint8_t dbgBASIC_CLIENT = 0;
/**
 * The interface name followed by the method signatures.
 *
 * See also .\inc\aj_introspect.h
 */
static const char* const sampleInterface[] = {
    "org.alljoyn.Bus.sample",   /* The first entry is the interface name. */
    "?Dummy foo<i",             /* This is just a dummy entry at index 0 for illustration purposes. */
    "?Dummy2 fee<i",            /* This is just a dummy entry at index 1 for illustration purposes. */
    "?cat inStr1<s inStr2<s outStr>s", /* Method at index 2. */
    NULL
};

/**
 * A NULL terminated collection of all interfaces.
 */
static const AJ_InterfaceDescription sampleInterfaces[] = {
    sampleInterface,
    NULL
};

/**
 * Objects implemented by the application. The first member in the AJ_Object structure is the path.
 * The second is the collection of all interfaces at that path.
 */
static const AJ_Object AppObjects[] = {
    { ServicePath, sampleInterfaces },
    { NULL }
};



#define BASIC_CLIENT_CAT AJ_PRX_MESSAGE_ID(0, 0, 2)

#define CONNECT_TIMEOUT    (1000 * 60)
#define UNMARSHAL_TIMEOUT  (1000 * 5)
#define METHOD_TIMEOUT     (100 * 10)
/*
void MakeMethodCall(AJ_BusAttachment* bus, uint32_t sessionId)
{
    AJ_Status status;
    AJ_Message msg;
    printf("\nIn client received session id %d\n",sessionId );
    printf("\nbefore marshalmethod call session id %d  msg.msgId %d  %s\n\n",sessionId,msg.msgId,msg.sender );
    status = AJ_MarshalMethodCall(bus, &msg, BASIC_CLIENT_CAT, ServiceName, sessionId, 0, METHOD_TIMEOUT);

    printf("\nafter marshalmethod call session id %d  msg.msgId %d  %s\n\n",sessionId,msg.msgId,msg.sender );
    if (status == AJ_OK) {
        status = AJ_MarshalArgs(&msg, "ss", "Hello ", "World!");
    }

    if (status == AJ_OK) {
        status = AJ_DeliverMsg(&msg);
    }

    AJ_InfoPrintf(("MakeMethodCall() resulted in a status of 0x%04x.\n", status));
}
*/
void MakeMethodCall(AJ_BusAttachment* bus, uint32_t sessionId)
{
    AJ_Status status;
label:
        AJ_Message msg;
    char buff1[50], buff2[50];
    printf("\nIn client received session id %u\n",sessionId );
    status = AJ_MarshalMethodCall(bus, &msg, BASIC_CLIENT_CAT, ServiceName, sessionId, 0, METHOD_TIMEOUT);

    printf("\nafter marshalmethod call session id %u  msg.msgId %d  %s\n\n",sessionId,msg.msgId,msg.sender );
     if (status == AJ_OK) {
       status = AJ_UnmarshalArgs(&msg, "ss", buff1, buff2 );
     printf("\nstring receied %s  %s from %u \n",buff1,buff2,msg.sessionId );
     }
goto label;
     AJ_InfoPrintf(("MakeMethodCall() resulted in a status of 0x%04x.\n", status));
                                    }


int AJ_Main(void)
{
    AJ_Status status = AJ_OK;
    AJ_BusAttachment bus;
    uint8_t connected = FALSE;
    uint8_t done = FALSE;
    uint32_t sessionId = 0;

    /*
     * One time initialization before calling any other AllJoyn APIs
     */
    AJ_Initialize();
//    AJ_PrintXML(AppObjects);
    AJ_RegisterObjects(NULL, AppObjects);

    while (!done) {
        AJ_Message msg;

        if (!connected) {
            status = AJ_StartClient(&bus,
                                    NULL,
                                    CONNECT_TIMEOUT,
                                    FALSE,
                                    ServiceName,
                                    ServicePort,
                                    &sessionId,
                                    NULL);

            if (status == AJ_OK) {
                AJ_InfoPrintf(("StartClient returned %d, sessionId=%u.\n", status, sessionId));
        printf("client started and connected\n");       
     connected = TRUE;
        printf("session ID %u sender  %s", msg.sessionId,msg.sender );

                MakeMethodCall(&bus, sessionId);
            } else {
    printf("start client failed ...
(more)
edit retag flag offensive close merge delete

Comments

Can you please post some errors that you encounter? Any source code you wish to share as well will help to point out the issues.

bspencer ( 2014-11-11 13:06:13 -0700 )edit

Hi Bspencer, I have edited my post with the code.Sorry i couldn't able to attach the files.I tried but it couldn't happen.Please guide me where i am going wrong. Thanks in advance. Regards, shashi.

shashidhar ( 2014-11-11 22:53:31 -0700 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-12 12:55:31 -0700

bspencer gravatar image

There are a few things wrong with the code changes you have made. First it appears that you are trying to flip the logic where the client side contains the method that you want the service side to invoke. This is fine to have a Client contain the object and a service side interact with this. Conceptually it is backwards in thought because the Service is advertising that it supports connections, but not offering an object to be interacted with.

The first thing that you need to understand is how BusObjects and ProxyObjects work. The call AJ_RegisterObjects has the first argument the local BusObjects and the second argument the ProxyBusObjects. IE what this software offers locally and what it objects it expects to interact with on the other side (ProxyBusObject). In your software the client is not receiving the BusMethod call because you have registered the BusObject as a ProxyBusObject AJ_RegisterObjects(NULL, AppObjects); This should be AJ_RegisterObjects(AppObjects, NULL);. Then on the service side you have it registered as a BusObject, when you need it to be a ProxyBusObject, IE switch the values in AJ_RegisterObjects.

Once you do this the #defines you have to lookup the AllJoyn Message ID that comes in is pointing to the wrong table. So you will need to change the #defines for the CAT method: #define BASIC_CLIENT_CAT AJ_PRX_MESSAGE_ID(0, 0, 2) in the Client code needs to be #define BASIC_CLIENT_CAT AJ_APP_MESSAGE_ID(0, 0, 1) and #define BASIC_SERVICE_CAT AJ_APP_MESSAGE_ID(0, 0, 1) in the Service code needs to be #define BASIC_SERVICE_CAT AJ_PRX_MESSAGE_ID(0, 0, 1)

The points above are the obvious things that I noticed. Not certain if there is anything I missed.

edit flag offensive delete publish link more

Comments

Hi Bspencer, Thank you for the quick response. I will modify the things you have mentioned and if i get any doubts i will get back to you.

shashidhar ( 2014-11-12 20:32:04 -0700 )edit

Hi Bspencer, I am facing these errors..... 1) aj_msg.c:899 AJ_UnmarshalMsg(): AJ_ERR_READ 2)aj_msg.c:433 LoadBytes(): AJ_ERR_READ 3)aj_msg.c:1794 AJ_MarshalArg(): AJ_ERR_END_OF_DATA.. Please clearify.Do you any sample codes so dat we can get the flow? Thanks

shashidhar ( 2014-11-13 02:28:54 -0700 )edit

have you looked at [basic_client.c](https://git.allseenalliance.org/cgit/core/ajtcl.git/tree/samples/basic/basic_client.c?id=v14.06a) and [basic_service.c](https://git.allseenalliance.org/cgit/core/ajtcl.git/tree/samples/basic/basic_service.c?id=v14.06a)

waynelee ( 2014-11-13 10:53:50 -0700 )edit

At this stage instead of going backwards and hacking about the code and trying to figure it out errors my recommendation would be to write the application you want and learn about each API as you go. What you are trying to do is reverse the BusObjects in the basic client/service example. In doing this though you have made some mistakes due to misunderstanding the concepts of AllJoyn. I don't have the code to run, but the new errors you receive indicate that something else needs to change that I missed in reviewing the above code.

bspencer ( 2014-11-13 11:01:21 -0700 )edit

I see the other issue, you are trying to Marshal a method call in your new client code. Really what you want to do is just switch the logic of what side advertises and what side joins. IE just flip the StartService and StartClient calls and the callbacks to form a session instead of switching the BusObject and call flows. This would be quicker and less painful than swapping all of the BusObjects/ProxybusObjects and method calls flow logic.

bspencer ( 2014-11-13 11:03:28 -0700 )edit
Login/Signup to Answer

Question Tools

Follow
1 follower

Stats

Asked: 2014-11-11 05:59:40 -0700

Seen: 134 times

Last updated: Nov 12 '14