/// <summary> /// Do the work of joining a session. /// </summary> /// <param name="folder">The folder to put the received file in.</param> /// <returns>true if all the input variables look good.</returns> public bool StartJoinSessionTask(StorageFolder folder) { bool returnValue = false; this.Folder = folder; if (null != this.Bus && null != this.Folder) { Task task = new Task(async() => { try { if (0 != this.SessionId) { this.Bus.LeaveSession(this.SessionId); } this.lastIndex = 0; this.CreateInterfaceAndBusObject(); this.AddSignalHandler(); // We found a remote bus that is advertising the well-known name so connect to it. bool result = await this.JoinSessionAsync(); if (result) { App.OutputLine("Successfully joined session."); } } catch (Exception ex) { QStatus status = AllJoynException.GetErrorCode(ex.HResult); string message = string.Format( "Errors were produced while establishing the application '{0}' (0x{0:X}).", status.ToString(), status); App.OutputLine(message); } }); task.Start(); returnValue = true; } return(returnValue); }
/// <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> /// 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> /// This method is called when the advertised name is found. /// </summary> /// <param name="wellKnownName">A well known name that the remote bus is advertising.</param> /// <param name="transportMask">Transport that received the advertisement.</param> /// <param name="namePrefix">The well-known name prefix used in call to FindAdvertisedName that triggered this callback.</param> private async void FoundAdvertisedName(string wellKnownName, TransportMaskType transportMask, string namePrefix) { if (!string.IsNullOrEmpty(wellKnownName) && wellKnownName.CompareTo(App.ServiceName) == 0) { string foundIt = string.Format( "Client found advertised name '{0}' on {1}.", wellKnownName, transportMask.ToString()); App.OutputLine(foundIt); // We found a remote bus that is advertising the well-known name so connect to it. bool result = await this.JoinSessionAsync(); if (result) { InterfaceDescription secureInterface = this.Bus.GetInterface(App.InterfaceName); this.ProxyObject = new ProxyBusObject(this.Bus, App.ServiceName, App.ServicePath, 0); this.ProxyObject.AddInterface(secureInterface); MsgArg[] inputs = new MsgArg[1]; inputs[0] = new MsgArg("s", new object[] { "Client says Hello AllJoyn!" }); MethodCallResult callResults = null; try { callResults = await this.ProxyObject.MethodCallAsync(App.InterfaceName, "Ping", inputs, null, 5000, 0); } catch (Exception ex) { const string ErrorFormat = "ProxyObject.MethodCallAsync(\"Ping\") call produced error(s). QStatus = 0x{0:X} ('{1}')."; QStatus status = AllJoynException.GetErrorCode(ex.HResult); string error = string.Format(ErrorFormat, status, status.ToString()); System.Diagnostics.Debug.WriteLine(error); App.OutputLine(error); } if (callResults != null) { Message mess = callResults.Message; AllJoynMessageType messType = mess.Type; if (messType == AllJoynMessageType.MESSAGE_METHOD_RET) { string strRet = mess.GetArg(0).Value as string; if (!string.IsNullOrEmpty(strRet)) { string output = string.Format( "{0}.Ping (path = {1}) returned \"{2}\"", App.InterfaceName, App.ServicePath, strRet); App.OutputLine(output); } else { const string Error = "Error: Server returned null or empty string in response to ping."; App.OutputLine(Error); System.Diagnostics.Debug.WriteLine(Error); } } else { string error = string.Format("Server returned message of type '{0}'.", messType.ToString()); App.OutputLine(error); System.Diagnostics.Debug.WriteLine(error); } } } } }