public static void Main() { AJ_Status status = AJ_Status.AJ_OK; UInt32 bus = 0; bool connected = false; bool done = false; UInt32 sessionId = 0; AJ myAlljoyn = new AJ(); myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object( ) { path = ServicePath, interfaces = SampleInterface }, false, false); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); int retry = 0; while (!done) { netmon.WaitForIpAddress(); AJ_Message msg = new AJ_Message(); if (!connected) { do { status = myAlljoyn.ClientConnectBus(bus, null, CONNECT_TIMEOUT); } while (status != AJ_Status.AJ_OK); do { status = myAlljoyn.ClientFindService(bus, ServiceName, null, CONNECT_TIMEOUT); } while (status != AJ_Status.AJ_OK); retry = 0; do { status = myAlljoyn.ClientConnectService(bus, CONNECT_TIMEOUT, ServiceName, ServicePort, ref sessionId, null, ref fullServiceName); if (retry++ > 10) { break; } } while (status != AJ_Status.AJ_OK); if (status == AJ_Status.AJ_OK) { connected = true; MakeMethodCall(bus, sessionId, myAlljoyn); } else { Debug.Print("Timed out"); myAlljoyn.Disconnect(bus); continue; } } status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (AJ_Status.AJ_ERR_TIMEOUT == status) { continue; } if (AJ_Status.AJ_OK == status) { if (msg.msgId == AJ_REPLY_ID(BASIC_CLIENT_CAT)) { UInt32 argPtr = myAlljoyn.GetArgPtr(4); status = myAlljoyn.UnmarshalArg(msg, argPtr); string value = myAlljoyn.GetArgString(4); if (AJ_Status.AJ_OK == status) { Debug.Print(ServiceName + "cat returned " + value); done = true; } else { MakeMethodCall(bus, sessionId, myAlljoyn); } } else if (msg.msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON) { { UInt32 id = 0, reason = 0; myAlljoyn.UnmarshalArgs(msg, "uu", ref id, ref reason); } status = AJ_Status.AJ_ERR_SESSION_LOST; } else { /* Pass to the built-in handlers. */ status = myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } /* Messages MUST be discarded to free resources. */ myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_SESSION_LOST) { myAlljoyn.Disconnect(bus); //exit(0); } } }
public static void Main() { // Define device interface. Interfaces beginning with // dollar sign ('$') are deemed secure string[] deviceInterface = { "$org.alljoyn.bus.samples.secure.SecureInterface", "?Ping inStr<s outStr>s", " " }; AJ_Status status = AJ_Status.AJ_OK; AJ_Status authStatus = AJ_Status.AJ_ERR_NULL; UInt32 bus = 0; bool connected = false; bool done = false; UInt32 sessionId = 0; AJ myAlljoyn = new AJ(); // Initialize Alljoyn object myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = deviceInterface }, false, false); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); while (!done) { netmon.WaitForIpAddress(); AJ_Message msg = new AJ_Message(); if (!connected) { // the original AJ_StartClientByName is splitted into 3 functions instead of one, as it will block the other CLR functions // and have a more efficient connection, especially when the service it is looking for is not existed yet. Instead of // keep connect and disconnect to router, send the findserive, we can just wait at the 3rd stage,ClientConnectservice. // // 1st stage - ClientconnectBus , found router and attached bus to it, wait until router found or timeout is set. // 2nd stage - ClientFindService, Adverstise the service it is looking for // 3rd stage - ClientConnectService, Connect to the services it is looking for, it will wait until the service pop up or timeout is set, do { status = myAlljoyn.ClientConnectBus(bus, null, CONNECT_TIMEOUT); }while (status != AJ_Status.AJ_OK); do { status = myAlljoyn.ClientFindService(bus, ServiceName, null, CONNECT_TIMEOUT); }while (status != AJ_Status.AJ_OK); int retry = 0; do { status = myAlljoyn.ClientConnectService(bus, CONNECT_TIMEOUT, ServiceName, ServicePort, ref sessionId, null, ref FullServiceName); // user can break from this call, instead of having a while loop retry++; if (retry > 10) { break; } } while (status != AJ_Status.AJ_OK); // if break from here, need to call myAlljoyn.Disconnect(bus); if (status != AJ_Status.AJ_OK) { myAlljoyn.Disconnect(bus); } if (status == AJ_Status.AJ_OK) { connected = true; authStatus = AJ_Status.AJ_ERR_NULL; // Provide credentials for all security // suites by setting write-only properties. // Only the security suites specified // in SecuritySuites will be used myAlljoyn.KeyExpiration = KeyExpiration; myAlljoyn.PskHint = PskHint; myAlljoyn.PskString = PskChar; myAlljoyn.PemPriv = PemPrv; myAlljoyn.PemX509 = PemX509; // Enable security suites myAlljoyn.EnableSecurity(bus, SecuritySuites); // Clear any stored credentials myAlljoyn.ClearCredentials(); // Begin authentication process with the service. This is // an asynchronous process so we will need to poll the // authStatus value during the main message processing loop status = myAlljoyn.AuthenticatePeer(bus, FullServiceName); if (status != AJ_Status.AJ_OK) { Debug.Print("AJ_BusAuthenticatePeer returned " + status.ToString()); break; } } else { continue; } } // Poll for authStatus to determine when // authentication handshaking is complete. authStatus = myAlljoyn.GetAuthStatus(); if (authStatus != AJ_Status.AJ_ERR_NULL) { if (authStatus != AJ_Status.AJ_OK) { myAlljoyn.Disconnect(bus); break; } // We set the authStatus to NULL when the // handshaking is finished to indicate that // we should proceed with normal alljoyn // message processing myAlljoyn.SetAuthStatus(AJ_Status.AJ_ERR_NULL); // Make ping method call to service status = SendPing(bus, sessionId, myAlljoyn); if (status != AJ_Status.AJ_OK) { Debug.Print("SendPing returned " + status.ToString()); continue; } } // process messages from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (AJ_Status.AJ_ERR_TIMEOUT == status) { continue; } if (AJ_Status.AJ_OK == status) { // Handle Ping response if (msg.msgId == AJ_REPLY_ID(PING_METHOD)) { UInt32 argPtr = myAlljoyn.GetArgPtr(4); if (AJ_Status.AJ_OK == myAlljoyn.UnmarshalArg(msg, argPtr)) { string value = myAlljoyn.GetArgString(4); if (value == pingString) { Debug.Print("Ping was successful"); } else { Debug.Print("Ping returned different string"); } } else { } done = true; } else if (msg.msgId == AJ_SIGNAL_SESSION_LOST_WITH_REASON) { // Just eating the reason for this demo, in production // reason should be inspected and acted upon { UInt32 id = 0, reason = 0; myAlljoyn.UnmarshalArgs(msg, "uu", ref id, ref reason); } status = AJ_Status.AJ_ERR_SESSION_LOST; } else { // Pass to the built-in handlers status = myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } // Clean up myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_SESSION_LOST) { myAlljoyn.Disconnect(bus); } } }