-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated endpoint class into web socket and user
This allows for login/logout and other neat things.
- Loading branch information
Showing
11 changed files
with
168 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
server/src/main/java/de/ovgu/spldev/varied/EndpointManager.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.ovgu.spldev.varied; | ||
|
||
import de.ovgu.spldev.varied.messaging.Message; | ||
import me.atrox.haikunator.Haikunator; | ||
import me.atrox.haikunator.HaikunatorBuilder; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class User { | ||
private String name; | ||
private WebSocket webSocket; | ||
private static Haikunator haikunator = new HaikunatorBuilder().setDelimiter(" ").setTokenLength(0).build(); | ||
|
||
private static String generateName() { | ||
Supplier<String> generator = () -> haikunator.haikunate() + " (anonymous)"; | ||
UserManager userManager = UserManager.getInstance(); | ||
String name = generator.get(); | ||
while (!userManager.isNameAvailable(name)) | ||
name = generator.get(); | ||
return name; | ||
} | ||
|
||
public User(WebSocket webSocket) { | ||
this(generateName(), webSocket); | ||
} | ||
|
||
public User(String name, WebSocket webSocket) { | ||
this.name = name; | ||
this.webSocket = webSocket; | ||
} | ||
|
||
public void send(Message.IEncodable message) { | ||
webSocket.send(message); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public WebSocket getWebSocket() { | ||
return webSocket; | ||
} | ||
|
||
public String toString() { | ||
return getName(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
server/src/main/java/de/ovgu/spldev/varied/UserManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package de.ovgu.spldev.varied; | ||
|
||
import de.ovgu.spldev.varied.messaging.Message; | ||
import de.ovgu.spldev.varied.util.StringUtils; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* Holds a mapping from web sockets to users and manages user registration | ||
*/ | ||
public class UserManager { | ||
private static UserManager instance; | ||
private HashMap<WebSocket, User> users = new HashMap<>(); | ||
|
||
private UserManager() { | ||
} | ||
|
||
public static UserManager getInstance() { | ||
return instance == null ? instance = new UserManager() : instance; | ||
} | ||
|
||
boolean isNameAvailable(String name) { | ||
return users.values().stream() | ||
.map(User::getName) | ||
.noneMatch(name::equals); | ||
} | ||
|
||
public void register(User newUser) { | ||
String name = newUser.getName(); | ||
if (!StringUtils.isPresent(name)) | ||
throw new RuntimeException("no name supplied on registration"); | ||
if (!isNameAvailable(name)) | ||
throw new RuntimeException("name already registered, choose another name"); | ||
if (users.containsValue(newUser)) | ||
throw new RuntimeException("user already registered"); | ||
if (users.containsKey(newUser.getWebSocket())) | ||
throw new RuntimeException("web socket is already logged in as another user"); | ||
users.put(newUser.getWebSocket(), newUser); | ||
|
||
// TODO: let the user decide which collaboration session to join | ||
CollaborationSession.getInstance().join(newUser); | ||
} | ||
|
||
public void register(WebSocket webSocket) { | ||
register(new User(webSocket)); | ||
} | ||
|
||
public void unregister(User oldUser) { | ||
// TODO: see above | ||
CollaborationSession.getInstance().leave(oldUser); | ||
|
||
users.remove(oldUser.getWebSocket()); | ||
} | ||
|
||
public void unregister(WebSocket webSocket) { | ||
User user = users.get(webSocket); | ||
if (user != null) | ||
unregister(user); | ||
} | ||
|
||
public void onMessage(WebSocket webSocket, Message message) { | ||
// TODO: dispatch to currect session (by asking the user object) | ||
// also handle login/logout here | ||
CollaborationSession.getInstance().onMessage(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.