public static void Main() { #region Initialize AlljoynSecurityService // Define device interface. Interfaces beginning with // dollar sign ('$') are deemed secure string[] deviceInterface = { "$org.alljoyn.bus.samples.secure.SecureInterface", "?Ping inStr<s outStr>s", " " }; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 BASIC_SERVICE_PING = AJ.AppMessageId(0, 0, 0); bool connected = false; const ushort port = 0; UInt32 bus = 0; var msg = new AJ_Message(); var myAlljoyn = new AJ(); // Initialize Alljoyn object myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize(); myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = deviceInterface }, false, true); #endregion // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor(); // Start the message processing loop while (true) { netmon.WaitForIpAddress(); AJ_Status status; if (!connected) { status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { Debug.Print(status.ToString()); continue; } Debug.Print("StartService returned AJ_OK; running \n"); connected = true; // Provide credentials for all security // suites by setting write-only properties myAlljoyn.KeyExpiration = KeyExpiration; myAlljoyn.PskHint = PskHint; myAlljoyn.PskString = PskChar; myAlljoyn.PemPriv = PemPrv; myAlljoyn.PemX509 = PemX509; // Enable security suites myAlljoyn.EnableSecurity(bus, SecuritySuites); // once connected to the Alljoyn router do the "About" annoucement if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } // wait for a message from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status == AJ_Status.AJ_ERR_TIMEOUT) { Debug.Print("do work\n"); continue; } // if the message is ok, check for message types this app wants to handle if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; Debug.Print(str); // Session connection from a client? if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { Debug.Print("Accepted session session_id \n"); } else { Debug.Print("AJ_BusReplyAcceptSession: error \n"); } } // Session connected else if (msg.msgId == BASIC_SERVICE_PING) { status = AppHandlePing(msg, myAlljoyn); } else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } // Request to read an interface property else // default handling (pass it on to the bus) { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { Debug.Print("AllJoyn disconnect\n"); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect again myAlljoyn.Sleep(10 * 1000); } } }
public static void Main( ) { #region Initialize ThinClient Service string[] weatherInterface = { "com.microsoft.NetMicroFrameWork.WeatherStation", "!WeatherEvent Arg1>s", "@String1=s", "@String2=s", "@String3=s", " " }; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 WEATHER_EVENT_SIGNAL = AJ.AppMessageId(0, 1, 0); const byte AJ_FLAG_GLOBAL_BROADCAST = 0x20; bool connected = false; const ushort port = 0; UInt32 bus = 0; var msg = new AJ_Message( ); // Initialize Alljoyn object var myAlljoyn = new AJ( ); var ajServices = new AJ_Services(myAlljoyn); // Create and set the property store var ps = new PropertyStore( ); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetSupportedLanguages(supportedLanguages); ps.SetDefaultLanguage(defaultLanguage); ps.SetDefaultSupportUrls(defaultSupportUrls); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetDefaultDescriptions(defaultDescriptions); ps.SetDefaultManufacturers(defaultManufacturers); ps.SetDefaultModelNumber(defaultModelNumber); ps.SetDefaultSoftwareVersion(defaultSoftwareVersion); ps.InitMandatoryFields( ); ajServices.SetPropertyStore(ps); // Set About attributes myAlljoyn.SetAboutIconContent(aboutIconContent); myAlljoyn.SetAboutIconURL(aboutIconUrl); myAlljoyn.CreateBus(ref bus); myAlljoyn.Initialize( ); myAlljoyn.RegisterObjectsInterface(new AJ_Object( ) { path = ServicePath, interfaces = weatherInterface }, true, true); #endregion ajServices.Initialize_NotificationService("DEFAULT"); // Attach to Netowrk Link state change notifications var netmon = new NetworkStateMonitor( ); // Start the message processing loop while (true) { netmon.WaitForIpAddress( ); AJ_Status status; if (!connected) { status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { continue; } Debug.Print("StartService returned AJ_OK; running \n"); connected = true; // once connected to the Alljoyn router do the "About" annoucement if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } // wait for a message from the router status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status == AJ_Status.AJ_ERR_TIMEOUT) { Debug.Print("do work\n"); continue; } // if the message is ok, check for message types this app wants to handle if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; Debug.Print(str); // Session connection from a client? if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { Debug.Print("Accepted session session_id \n"); } else { Debug.Print("AJ_BusReplyAcceptSession: error \n"); } } // Session connected else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } // Request to read an interface property else if (msg.msgId == GET_PROPERTY) { myAlljoyn.BusGetProp(msg, GetPropertyHandler); } // Set a property on the advertised interface else if (msg.msgId == SET_PROPERTY) { myAlljoyn.BusSetProp(msg, SetPropertyHandler); // This is how to send a signal. You can put this code in // the handler for a weather event, etc. uint sn = 4; ajServices.SendNotification(bus, "Sample notify", 2, 500, ref sn); var sig = new AJ_Message(); AJ_Status s = myAlljoyn.MarshalSignal(bus, sig, WEATHER_EVENT_SIGNAL, 0, 0, AJ_FLAG_GLOBAL_BROADCAST, 0); s = myAlljoyn.MarshalArg(sig, "s", "22222 This is a bogus weather event"); myAlljoyn.DeliverMsg(sig); } else // default handling (pass it on to the bus) { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { Debug.Print("AllJoyn disconnect\n"); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect again myAlljoyn.Sleep(10 * 1000); } } }
// program entry point public static void Main() { // Initialize Alljoyn class instance AJ myAlljoyn = new AJ(); var ajServices = new AJ_Services(myAlljoyn); // some state variables bool connected = false; AJ_Status status = AJ_Status.AJ_OK; // constants used in the message processing loop UInt32 CONNECT_TIMEOUT = (1000 * 1000); UInt32 UNMARSHAL_TIMEOUT = 5000; UInt32 AJ_METHOD_ACCEPT_SESSION = AJ.BusMessageId(2, 0, 0); UInt32 AJ_SIGNAL_SESSION_JOINED = AJ.BusMessageId(2, 0, 1); UInt32 START_TOASTING = AJ.AppMessageId(0, 1, 0); UInt32 STOP_TOASTING = AJ.AppMessageId(0, 1, 1); UInt32 TOAST_DONE_SIGNAL = AJ.AppMessageId(0, 1, 2); UInt32 DARKNESS = AJ.AppPropertyId(0, 1, 3); byte AJ_FLAG_GLOBAL_BROADCAST = 0x20; UInt16 port = 0; UInt32 bus = 0; AJ_Message msg = new AJ_Message(); // Define the application that the outside world // will see string ServiceName = "com.microsoft.sample.toaster"; string ServicePath = "/toaster"; UInt16 ServicePort = 42; string DaemonServiceName = "org.alljoyn.BusNode.Led"; // Define the Alljoyn interface that will be // published to the outside world string[] testInterface = { "com.microsoft.sample.toaster", "?startToasting", "?stopToasting", "!toastDone status>i", "@Darkness=u", " " }; // Create and set the property store PropertyStore ps = new PropertyStore(); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetSupportedLanguages(supportedLanguages); ps.SetDefaultLanguage(defaultLanguage); ps.SetDefaultSupportUrls(defaultSupportUrls); ps.SetDefaultDeviceNames(defaultDeviceNames); ps.SetDefaultDescriptions(defaultDescriptions); ps.SetDefaultManufacturers(defaultManufacturers); ps.SetDefaultModelNumber(defaultModelNumber); ps.SetDefaultSoftwareVersion(defaultSoftwareVersion); ps.InitMandatoryFields(); ajServices.SetPropertyStore(ps); // Set About attributes myAlljoyn.SetAboutIconContent(aboutIconContent); myAlljoyn.SetAboutIconURL(aboutIconUrl); // Create the Alljoyn bus instance myAlljoyn.CreateBus(ref bus); // Initialize Alljoyn instance myAlljoyn.Initialize(); // Register alljoyn interface. We set the 2nd parameter to true to indicate that // properties are going to be used myAlljoyn.RegisterObjectsInterface(new AJ_Object() { path = ServicePath, interfaces = testInterface }, true, true); // Start the message processing loop while (true) { if (!connected) { IntPtr arg = new IntPtr(); status = myAlljoyn.StartService(bus, DaemonServiceName, CONNECT_TIMEOUT, AJ.AJ_FALSE, ServicePort, ServiceName, AJ.AJ_NAME_REQ_DO_NOT_QUEUE); if (status != AJ_Status.AJ_OK) { goto exit; } myAlljoyn.AlwaysPrintf(("StartService returned AJ_OK; running \n")); connected = true; if (status == AJ_Status.AJ_OK) { myAlljoyn.doAnnounce = true; myAlljoyn.AboutAnnounce(bus, ServicePort); } } status = myAlljoyn.UnmarshalMsg(bus, msg, UNMARSHAL_TIMEOUT); if (status != AJ_Status.AJ_OK) { if (status == AJ_Status.AJ_ERR_TIMEOUT) { myAlljoyn.AlwaysPrintf(("do work\n")); goto exit; } } if (status == AJ_Status.AJ_OK) { string str = "Received message + msgId=" + msg.msgId.ToString("X") + "\n"; myAlljoyn.AlwaysPrintf((str)); if (msg.msgId == AJ_METHOD_ACCEPT_SESSION) { string joiner = ""; UInt32 sessionId = 0; myAlljoyn.UnmarshalArgs(msg, "qus", port, sessionId, joiner); status = myAlljoyn.BusReplyAcceptSession(msg, 1); if (status == AJ_Status.AJ_OK) { myAlljoyn.AlwaysPrintf(("Accepted session session_id \n")); } else { myAlljoyn.AlwaysPrintf(("AJ_BusReplyAcceptSession: error \n")); } } else if (msg.msgId == AJ_SIGNAL_SESSION_JOINED) { // do nothing... } else if (msg.msgId == START_TOASTING) { AJ_Message reply = new AJ_Message(); status = myAlljoyn.MarshalReplyMsg(msg, reply); if (status == AJ_Status.AJ_OK) { status = myAlljoyn.DeliverMsg(reply); } } else if (msg.msgId == STOP_TOASTING) { AJ_Status s = 0; AJ_Message reply = new AJ_Message(); s = myAlljoyn.MarshalReplyMsg(msg, reply); if (s == AJ_Status.AJ_OK) { s = myAlljoyn.DeliverMsg(reply); } // send a signal here AJ_Message sig = new AJ_Message(); s = myAlljoyn.MarshalSignal(bus, sig, TOAST_DONE_SIGNAL, 0, 0, AJ_FLAG_GLOBAL_BROADCAST, 0); s = myAlljoyn.MarshalArg(sig, "i", (uint)s); myAlljoyn.DeliverMsg(sig); } else if (msg.msgId == GET_DARKNESS) { myAlljoyn.BusGetProp(msg, GetPropertyHandler); } else if (msg.msgId == SET_DARKNESS) { myAlljoyn.BusSetProp(msg, SetPropertyHandler); } else { myAlljoyn.BusHandleBusMessage(msg, bus, ServicePort); } } myAlljoyn.CloseMsg(msg); if (status == AJ_Status.AJ_ERR_READ) { myAlljoyn.AlwaysPrintf(("AllJoyn disconnect\n")); myAlljoyn.Disconnect(bus); connected = false; // sleep a little while before trying to connect myAlljoyn.Sleep(10 * 1000); } exit: myAlljoyn.AlwaysPrintf((" Exit \n")); } }