public void AddCallbackToCallbacksDictionary(IUserCallback callback, IUserName request, Dictionary <IUserCallback, string> callbacksByUserName) { if (!callbacksByUserName.ContainsKey(callback)) {//from now on, this user will know each time other user is loged in callbacksByUserName.Add(callback, request.UserName); } }
public User Login(string username, string password) { lock (dummyObj) //jel neophodan lock ovde? Radim nad istim podacima, ali svaki put sa novom instancom DataContext-a i ne modifikujem podatke ovde, samo citam. { using (var context = new DataContext()) { var user = context.Users.FirstOrDefault(u => u.Username == username && u.Password == password); //returns null if no match if (user != null) { try { callback = OperationContext.Current.GetCallbackChannel <IUserCallback>(); CallbackData.Users.Add(user.Username, callback); //CallbackData.Callbacks.Add(callback); } catch (Exception) { Program.Log.Warn($"Already logged in. Username=('{username}')"); return(null); } } Program.Log.Info($"Successful Login. Username=('{username}')"); return(user); } } }
//online callbacks public void OnUserCame(int userId) { try { using (UserContext userContext = new UserContext()) { User dbUser = userContext.Users.Where(u => u.Id == userId).FirstOrDefault(); dbUser.Status = "online"; userContext.SaveChanges(); if (dbUser != null) { CallbackData callbackData = new CallbackData(); callbackData.User = dbUser; IUserCallback callback = OperationContext.Current.GetCallbackChannel <IUserCallback>(); callbackData.UserCallback = callback; usersOnline.Add(callbackData); Thread t = new Thread(new ParameterizedThreadStart(userCameCallback)); t.IsBackground = true; t.Start(callbackData); } } } catch (Exception) { } }
public void HandleUnexceptedDisconnection(IUserCallback unexpectedDisconnectedUser, Dictionary <IUserCallback, string> callbacksByUserName) { //removing from onllineUsers : string userName = ReturnUserNameByCallback(unexpectedDisconnectedUser, callbacksByUserName); //remove from callbacks: callbacksByUserName.Remove(unexpectedDisconnectedUser); }
public LoginResponse Login(LoginRequest loginRequest) { //service logic should return OnlineUser Guid For User and update dicitonary (in case login=Succes) string responseMessage = string.Empty; bool loginSuccess = serviceLogic.TryLogin(loginRequest, out responseMessage); if (loginSuccess == true) { Guid authentication = serviceLogic.CreateAuthentication(loginRequest.UserName); for (int i = 0; i < callbacksByUserName.Count; i++) { var user = callbacksByUserName.ElementAt(i).Key; try { user.OnLogin(new OnLoginRequest { UserName = loginRequest.UserName }); } catch { string disconnectedUserName = callbacksHandler.ReturnUserNameByCallback(user, callbacksByUserName); serviceLogic.TryLogout(disconnectedUserName); callbacksHandler.HandleUnexceptedDisconnection(user, callbacksByUserName); i--; } } List <UserDTO> allOtherUsers = serviceLogic.GetOnlineUsersExcept(loginRequest.UserName); //getting all users already logged in, in order to tell the user about them //making a callback to tell the other users the user is loged in (if succeded) IUserCallback callback = OperationContext.Current.GetCallbackChannel <IUserCallback>(); callbacksHandler.AddCallbackToCallbacksDictionary(callback, loginRequest, callbacksByUserName); return(new LoginResponse { IsSuccess = true, Message = responseMessage, AllOtherUsers = allOtherUsers, Authentication = authentication }); } else //login didnt succes: { return(new LoginResponse { IsSuccess = false, Message = responseMessage, AllOtherUsers = new List <UserDTO> { }, Authentication = Guid.Empty }); } }
public LogoutResponse Logout(LogoutRequest logoutRequest) { Guid authentication = serviceLogic.GetHeaderAuthentication(); UserDTO LogedoutUser = serviceLogic.TryLogout(logoutRequest, authentication); if (LogedoutUser == null) { return(new LogoutResponse { IsSuccess = false, Message = "request not authentiacated" }); } else { //removing user from callbacks: IUserCallback callback = OperationContext.Current.GetCallbackChannel <IUserCallback>(); callbacksByUserName.Remove(callback); for (int i = 0; i < callbacksByUserName.Count; i++) { var user = callbacksByUserName.ElementAt(i).Key; try { user.OnLogout(new OnlogoutRequest { UserName = logoutRequest.UserName }); } catch { string disconnectedUserName = callbacksHandler.ReturnUserNameByCallback(user, callbacksByUserName); serviceLogic.TryLogout(disconnectedUserName); callbacksHandler.HandleUnexceptedDisconnection(user, callbacksByUserName); i--; } } return(new LogoutResponse { IsSuccess = true, Message = "loged out succefully" }); } }
public void Disconnect(IUserCallback unexpectedDisconnectedUser) { throw new NotImplementedException(); }
public User() { callback = OperationContext.Current.GetCallbackChannel <IUserCallback>(); }
public string ReturnUserNameByCallback(IUserCallback callback, Dictionary <IUserCallback, string> callbacksByUserName) { return(callbacksByUserName.ContainsKey(callback) ? callbacksByUserName[callback] : string.Empty); }