/// <summary> /// Initializes a new instance of the ChatSessionObject class. /// </summary> /// <param name="bus">The BusAttachment to be associated with.</param> /// <param name="path">The path for the BusObject.</param> /// <param name="host">The instance of the MainPage which handles the UI for this /// application.</param> public ChatSessionObject(BusAttachment bus, string path, MainPage host) { try { this.hostPage = host; this.busObject = new BusObject(bus, path, false); /* Add the interface to this object */ InterfaceDescription[] ifaceArr = new InterfaceDescription[1]; bus.CreateInterface(ChatServiceInterfaceName, ifaceArr, false); ifaceArr[0].AddSignal("Chat", "s", "str", 0, string.Empty); ifaceArr[0].Activate(); InterfaceDescription chatIfc = bus.GetInterface(ChatServiceInterfaceName); this.busObject.AddInterface(chatIfc); this.chatSignalReceiver = new MessageReceiver(bus); this.chatSignalReceiver.SignalHandler += new MessageReceiverSignalHandler(this.ChatSignalHandler); this.chatSignalMember = chatIfc.GetMember("Chat"); bus.RegisterSignalHandler(this.chatSignalReceiver, this.chatSignalMember, path); } catch (System.Exception ex) { QStatus errCode = AllJoyn.AllJoynException.GetErrorCode(ex.HResult); string errMsg = AllJoyn.AllJoynException.GetErrorMessage(ex.HResult); this.hostPage.DisplayStatus("Create ChatSessionObject Error : " + errMsg); } }
/// <summary> /// Connects to the bus, finds the service and calls the 'cat' with the two /// arguments "Hello " and "World!" /// </summary> /// <param name="sender">UI control which signaled the click event</param> /// <param name="e">arguments associated with the click event</param> private void Button_RunClick(object sender, RoutedEventArgs e) { if (!runningClient) { Task task = new Task(async() => { try { runningClient = true; busAtt = new BusAttachment("ClientApp", true, 4); this.OutputLine("BusAttachment Created."); BasicClientBusListener basicClientBusListener = new BasicClientBusListener(busAtt, foundNameEvent); busAtt.RegisterBusListener(basicClientBusListener); this.OutputLine("BusListener Registered."); /* Create and register the bundled daemon. The client process connects to daemon over tcp connection */ busAtt.Start(); await busAtt.ConnectAsync(BasicClientGlobals.ConnectSpec); this.OutputLine("Bundled Daemon Registered."); this.OutputLine("BusAttachment Connected to " + BasicClientGlobals.ConnectSpec + "."); busAtt.FindAdvertisedName(BasicClientGlobals.WellKnownServiceName); foundNameEvent.WaitOne(); /* Configure session properties and request a session with device with wellKnownName */ SessionOpts sessionOpts = new SessionOpts( BasicClientGlobals.SessionProps.TrType, BasicClientGlobals.SessionProps.IsMultiPoint, BasicClientGlobals.SessionProps.PrType, BasicClientGlobals.SessionProps.TmType); SessionOpts[] sOptsOut = new SessionOpts[1]; JoinSessionResult joinResults = await busAtt.JoinSessionAsync( BasicClientGlobals.WellKnownServiceName, BasicClientGlobals.SessionProps.SessionPort, basicClientBusListener, sessionOpts, sOptsOut, null); QStatus status = joinResults.Status; if (QStatus.ER_OK != status) { this.OutputLine("Joining a session with the Service was unsuccessful."); } else { this.OutputLine("Join Session was successful (sessionId=" + joinResults.SessionId + ")."); } // Create the proxy for the service interface by introspecting the service bus object ProxyBusObject proxyBusObject = new ProxyBusObject(busAtt, BasicClientGlobals.WellKnownServiceName, BasicClientGlobals.ServicePath, 0); if (QStatus.ER_OK == status) { IntrospectRemoteObjectResult introResult = await proxyBusObject.IntrospectRemoteObjectAsync(null); status = introResult.Status; if (QStatus.ER_OK != status) { this.OutputLine("Introspection of the service bus object failed."); } else { this.OutputLine("Introspection of the service bus object was successful."); } } if (QStatus.ER_OK == status) { // Call 'cat' method with the two string to be concatenated ("Hello" and " World!") MsgArg[] catMe = new MsgArg[2]; catMe[0] = new MsgArg("s", new object[] { "Hello" }); catMe[1] = new MsgArg("s", new object[] { " World!" }); InterfaceDescription interfaceDescription = proxyBusObject.GetInterface(BasicClientGlobals.InterfaceName); InterfaceMember interfaceMember = interfaceDescription.GetMember("cat"); this.OutputLine("Calling the 'cat' method of the service with args 'Hello' and ' World!'"); MethodCallResult callResults = await proxyBusObject.MethodCallAsync(interfaceMember, catMe, null, 100000, 0); Message msg = callResults.Message; if (msg.Type == AllJoynMessageType.MESSAGE_METHOD_RET) { string strRet = msg.GetArg(0).Value as string; this.OutputLine("Sender '" + msg.Sender + "' returned the value '" + strRet + "'"); } else { this.OutputLine("The 'cat' method call produced errors of type: " + msg.Type.ToString()); } } TearDown(); } catch (Exception ex) { QStatus s = AllJoynException.GetErrorCode(ex.HResult); } }); task.Start(); } }