/// <summary> /// Do the stuff required to join the session. /// </summary> /// <returns>True if successfully joined the session.</returns> private async Task <bool> JoinSessionAsync() { bool returnValue = true; SessionOpts opts = new SessionOpts( TrafficType.TRAFFIC_MESSAGES, false, ProximityType.PROXIMITY_ANY, TransportMaskType.TRANSPORT_ANY); SessionOpts[] opts_out = new SessionOpts[1]; JoinSessionResult result = await this.Bus.JoinSessionAsync( App.ServiceName, App.ServicePort, this.Listeners, opts, opts_out, null); if (result.Status == QStatus.ER_OK) { App.OutputLine("Successfully joined session."); } else { string errorMessage = string.Format("Join session failed with error {0:X}.", result.Status); App.OutputLine(errorMessage); returnValue = false; } return(returnValue); }
public async void OnFoundMediaServer() { if (_sessionId == 0) { /* * Join the session - if we need a specific qos we would check it here. */ SessionOpts opts = new SessionOpts(TrafficType.TRAFFIC_MESSAGES, true, ProximityType.PROXIMITY_ANY, TransportMaskType.TRANSPORT_ANY); SessionOpts[] opts_out = new SessionOpts[1]; JoinSessionResult result = await _bus.JoinSessionAsync(MEDIA_SERVER_NAME, SESSION_PORT, _listeners, opts, opts_out, null); _sessionId = result.SessionId; ConnectToServer(); } }
/// <summary> /// Joins a session with specified nameprefix using specs provided /// </summary> /// <param name="name">Name prefix or well-known name to join session with</param> /// <param name="sessionPort">Port for session</param> /// <param name="opts">Session Opts for session</param> public async void DoJoinAsync(string name, uint sessionPort, SessionOpts opts) { SessionOpts[] optsOut = new SessionOpts[1]; JoinSessionResult joinResult = await this.busAtt.JoinSessionAsync(name, (ushort)sessionPort, (SessionListener)this.busListener, opts, optsOut, null); QStatus status = joinResult.Status; if (QStatus.ER_OK == status) { this.mainPage.Output("BusAttachment.JoinSessionAsync(" + name + ", " + sessionPort + ", ...) succeeded with id=" + joinResult.SessionId); lock (this.sessionMap) { this.sessionMap.Add(joinResult.SessionId, new SessionInfo(joinResult.SessionId, new SessionPortInfo(sessionPort, name, opts))); } } else { this.mainPage.Output("BusAttachment.JoinSessionAsync(" + name + ", " + sessionPort + ", ...) failed: " + status.ToString()); } }
/// <summary> /// Do the stuff required to join the session. /// </summary> /// <returns>True if successfully joined the session.</returns> private async Task <bool> JoinSessionAsync() { bool returnValue = true; SessionOpts opts = new SessionOpts( ClientGlobals.SessionProps.TrType, ClientGlobals.SessionProps.IsMultiPoint, ClientGlobals.SessionProps.PrType, ClientGlobals.SessionProps.TmType); SessionOpts[] opts_out = new SessionOpts[1]; App app = Application.Current as App; JoinSessionResult result = await this.Bus.JoinSessionAsync( ClientGlobals.ServiceName, ClientGlobals.SessionProps.SessionPort, app.Listeners, opts, opts_out, null); string message = string.Format("Join Session status = {0}.", result.Status.ToString()); App.OutputLine(message); if (QStatus.ER_OK == result.Status) { this.SessionId = result.SessionId; message = string.Format("SessionId='{0}'.", result.SessionId); App.OutputLine(message); } else { string errorMessage = string.Format("Join session failed with error {0:X}.", result.Status); App.OutputLine(errorMessage); returnValue = false; } return(returnValue); }
/// <summary> /// Join an existing channel with the given name. /// </summary> /// <param name="name">The name of the channel to join.</param> private async void JoinChannel(string name) { try { this.DisplayStatus("Joining chat session: " + name); string wellKnownName = this.MakeWellKnownName(name); SessionOpts opts = new SessionOpts(TrafficType.TRAFFIC_MESSAGES, true, ProximityType.PROXIMITY_ANY, alljoynTransports); SessionOpts[] opts_out = new SessionOpts[1]; this.channelJoined = name; JoinSessionResult result = await this.bus.JoinSessionAsync( wellKnownName, ContactPort, this.busListeners, opts, opts_out, null); if (result.Status == QStatus.ER_OK) { this.SessionId = result.SessionId; this.DisplayStatus("Join chat session " + this.SessionId + " successfully"); await this.coreDispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { this.JoinChannelButton.Content = "Leave Channel"; }); } else { this.DisplayStatus("Join chat session " + result.SessionId + " failed. Error: " + result.Status); } } catch (Exception ex) { QStatus stat = AllJoynException.GetErrorCode(ex.HResult); string errMsg = AllJoynException.GetErrorMessage(ex.HResult); DisplayStatus("JoinChannel Error : " + errMsg); } }
/// <summary> /// Connects to the bus and registers a signal handler for when the 'name' property changes. /// </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 (busAtt == null) { Task task = new Task(async() => { try { busAtt = new BusAttachment("SignalConsumerApp", true, 4); // create and activate the interface InterfaceDescription[] interfaceDescription = new InterfaceDescription[1]; busAtt.CreateInterface(SignalConsumerGlobals.InterfaceName, interfaceDescription, false); interfaceDescription[0].AddSignal("nameChanged", "s", "newName", (byte)0, string.Empty); interfaceDescription[0].AddProperty("name", "s", (byte)PropAccessType.PROP_ACCESS_RW); interfaceDescription[0].Activate(); busListener = new SignalConsumerBusListener(busAtt, foundNameEvent); OutputLine("BusAttachment and BusListener Created."); busAtt.RegisterBusListener(busListener); OutputLine("BusListener Registered."); busAtt.Start(); busAtt.ConnectAsync(SignalConsumerGlobals.ConnectSpec).AsTask().Wait(); OutputLine("Bundled Daemon Registered."); OutputLine("BusAttachment Connected to " + SignalConsumerGlobals.ConnectSpec + "."); busAtt.FindAdvertisedName(SignalConsumerGlobals.WellKnownServiceName); foundNameEvent.WaitOne(); /* Configure session properties and request a session with device with wellKnownName */ SessionOpts sessionOpts = new SessionOpts( SignalConsumerGlobals.SessionProps.TrType, SignalConsumerGlobals.SessionProps.IsMultiPoint, SignalConsumerGlobals.SessionProps.PrType, SignalConsumerGlobals.SessionProps.TmType); SessionOpts[] sessionOptsOut = new SessionOpts[1]; OutputLine("Requesting a session with the well known service name."); JoinSessionResult joinResult = await busAtt.JoinSessionAsync( SignalConsumerGlobals.WellKnownServiceName, SignalConsumerGlobals.SessionProps.SessionPort, busListener, sessionOpts, sessionOptsOut, null); if (QStatus.ER_OK == joinResult.Status) { OutputLine("Join Session was successful (sessionId=" + joinResult.SessionId + ")."); busAtt.AddMatch("type='signal',interface='org.alljoyn.Bus.signal_sample',member='nameChanged'"); OutputLine("Subscribed to the 'nameChanged' signal."); } else { OutputLine("Join Session was unsuccessful."); } } catch (Exception ex) { OutputLine("Errors were produced while establishing the application."); QStatus status = AllJoynException.GetErrorCode(ex.HResult); busAtt = null; } }); task.Start(); } }
/// <summary> /// Connects to the bus, finds the service and sets the 'name' property to the value /// specified by the user. /// </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_RunNameChangeClient(object sender, RoutedEventArgs e) { if (this.TextBox_Input.Text == string.Empty) { this.OutputLine("You must provide an argument to run Name Change Client!"); } if (!runningClient && this.TextBox_Input.Text != string.Empty) { string newName = this.TextBox_Input.Text; Task task = new Task(async() => { try { runningClient = true; busAtt = new BusAttachment("NameChangeApp", true, 4); OutputLine("BusAttachment Created."); NameChangeBusListener busListener = new NameChangeBusListener(busAtt, foundNameEvent); busAtt.RegisterBusListener(busListener); OutputLine("BusListener Registered."); /* Create and register the bundled daemon. The client process connects to daemon over tcp connection */ busAtt.Start(); await busAtt.ConnectAsync(NameChangeGlobals.ConnectSpec); OutputLine("Bundled Daemon Registered."); OutputLine("BusAttachment Connected to " + NameChangeGlobals.ConnectSpec + "."); busAtt.FindAdvertisedName(NameChangeGlobals.WellKnownServiceName); foundNameEvent.WaitOne(); /* Configure session properties and request a session with device with wellKnownName */ SessionOpts sOpts = new SessionOpts( NameChangeGlobals.SessionProps.TrType, NameChangeGlobals.SessionProps.IsMultiPoint, NameChangeGlobals.SessionProps.PrType, NameChangeGlobals.SessionProps.TmType); SessionOpts[] sOptsOut = new SessionOpts[1]; JoinSessionResult joinResults = await busAtt.JoinSessionAsync( NameChangeGlobals.WellKnownServiceName, NameChangeGlobals.SessionProps.SessionPort, busListener, sOpts, sOptsOut, null); QStatus status = joinResults.Status; if (QStatus.ER_OK == status) { this.OutputLine("Join Session was successful (sessionId=" + joinResults.SessionId + ")."); } else { this.OutputLine("Join Session was unsuccessful."); } ProxyBusObject pbo = new ProxyBusObject(busAtt, NameChangeGlobals.WellKnownServiceName, NameChangeGlobals.ServicePath, sessionId); if (QStatus.ER_OK == status) { IntrospectRemoteObjectResult introResult = await pbo.IntrospectRemoteObjectAsync(null); status = introResult.Status; if (QStatus.ER_OK == status) { this.OutputLine("Introspection of the service object was successful."); } else { this.OutputLine("Introspection of the service object was unsuccessful."); } } if (QStatus.ER_OK == status) { object[] obj = new object[] { newName }; MsgArg msg = new MsgArg("s", obj); SetPropertyResult setResult = await pbo.SetPropertyAsync(NameChangeGlobals.InterfaceName, "name", msg, null, 2000); } TearDown(); } catch (Exception ex) { QStatus s = AllJoynException.GetErrorCode(ex.HResult); OutputLine("Error: " + ex.ToString()); runningClient = false; } }); task.Start(); } }
/// <summary> /// Run the client stress op which stresses the bus attachment in a client type /// configuration which find the well-known service name and calls the 'cat' /// method /// </summary> /// <param name="isMultipoint">True if operation uses multipoint sessions</param> private void RunClient(bool isMultipoint) { try { ClientBusListener clientBusListener = new ClientBusListener(this.busAtt, this, this.foundName); this.busAtt.FindAdvertisedName(ServiceName); this.DebugPrint("Looking for WKN : " + ServiceName); this.foundName.WaitOne(12000); SessionOpts optsIn = new SessionOpts( TrafficType.TRAFFIC_MESSAGES, isMultipoint, ProximityType.PROXIMITY_ANY, TransportMaskType.TRANSPORT_ANY); SessionOpts[] optsOut = new SessionOpts[1]; Task <JoinSessionResult> joinTask = this.busAtt.JoinSessionAsync( this.DiscoveredName, ServicePort, (SessionListener)clientBusListener, optsIn, optsOut, null).AsTask <JoinSessionResult>(); joinTask.Wait(); JoinSessionResult joinResult = joinTask.Result; QStatus status = joinResult.Status; ProxyBusObject proxyBusObj = null; if (QStatus.ER_OK == status) { this.DebugPrint("JoinSession with " + this.DiscoveredName + " was successful (sessionId=" + joinResult.SessionId + ")"); proxyBusObj = new ProxyBusObject(this.busAtt, this.DiscoveredName, ServicePath, joinResult.SessionId); Task <IntrospectRemoteObjectResult> introTask = proxyBusObj.IntrospectRemoteObjectAsync(null).AsTask <IntrospectRemoteObjectResult>(); introTask.Wait(); IntrospectRemoteObjectResult introResult = introTask.Result; status = introResult.Status; if (QStatus.ER_OK == status) { this.DebugPrint("Introspection of the service was successfull"); MsgArg hello = new MsgArg("s", new object[] { "Hello " }); MsgArg world = new MsgArg("s", new object[] { "World!" }); InterfaceMember catMethod = proxyBusObj.GetInterface(InterfaceName).GetMethod("cat"); byte flags = (byte)0; Task <MethodCallResult> catTask = proxyBusObj.MethodCallAsync(catMethod, new MsgArg[] { hello, world }, null, 5000, flags).AsTask <MethodCallResult>(); catTask.Wait(); MethodCallResult catResult = catTask.Result; if (catResult.Message.Type == AllJoynMessageType.MESSAGE_METHOD_RET) { this.DebugPrint(this.DiscoveredName + ".cat ( path=" + ServicePath + ") returned \"" + catResult.Message.GetArg(0).Value.ToString() + "\""); } else { this.DebugPrint("Method call on " + this.DiscoveredName + ".cat failed (ReturnType=" + catResult.Message.Type.ToString() + ")"); } } else { this.DebugPrint("Introspection was unsuccessful: " + status.ToString()); } } else { this.DebugPrint("Join Session was unsuccessful: " + status.ToString()); } this.busAtt.CancelFindAdvertisedName(ServiceName); this.busAtt.UnregisterBusListener(clientBusListener); this.DebugPrint("Successfully unraveled the client operation"); } catch (ArgumentNullException ex) { this.DebugPrint(">>>> TIMEOUT, Client could not find WKN >>>>"); } catch (Exception ex) { var errMsg = AllJoynException.GetErrorMessage(ex.HResult); this.DebugPrint(">>>> Client Execution 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(); } }