You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.3 KiB
96 lines
2.3 KiB
/**
|
|
*
|
|
*/
|
|
package com.jotuntech.sketcher.client.command;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import com.jotuntech.sketcher.client.Client;
|
|
import com.jotuntech.sketcher.client.Command;
|
|
import com.jotuntech.sketcher.client.Connection;
|
|
import com.jotuntech.sketcher.client.UserInterface;
|
|
import com.jotuntech.sketcher.common.User;
|
|
|
|
/**
|
|
* @author Thor Harald Johansen
|
|
*
|
|
*/
|
|
public class SignInCommand implements Command {
|
|
Integer peerKey;
|
|
String login;
|
|
String password;
|
|
boolean viewer;
|
|
|
|
public SignInCommand() {
|
|
|
|
}
|
|
|
|
public SignInCommand(Integer peerKey, String login, String password, boolean viewer) {
|
|
this.peerKey = peerKey;
|
|
this.login = login;
|
|
this.password = password;
|
|
this.viewer = viewer;
|
|
}
|
|
|
|
public void decode(ByteBuffer in) {
|
|
peerKey = in.getInt();
|
|
viewer = in.get() != 0;
|
|
int loginLength = (in.get() & 0xFF) + 1;
|
|
StringBuffer loginBuffer = new StringBuffer();
|
|
for(int i = 0; i < loginLength; i++) {
|
|
loginBuffer.append(in.getChar());
|
|
}
|
|
login = loginBuffer.toString();
|
|
|
|
StringBuffer passwordBuffer = new StringBuffer();
|
|
while(in.remaining() >= 2) {
|
|
passwordBuffer.append(in.getChar());
|
|
}
|
|
password = passwordBuffer.toString();
|
|
}
|
|
|
|
public void encode(ByteBuffer out) {
|
|
out.putInt(peerKey);
|
|
out.put((byte) (viewer ? 0xFF : 0x00));
|
|
out.put((byte) (login.length() - 1));
|
|
for(int i = 0; i < login.length(); i++) {
|
|
out.putChar(login.charAt(i));
|
|
}
|
|
for(int i = 0; i < password.length(); i++) {
|
|
out.putChar(password.charAt(i));
|
|
}
|
|
}
|
|
|
|
public int perform(Client client, User user) {
|
|
/** Create user */
|
|
user = new User(login);
|
|
|
|
/** Set viewer flag */
|
|
user.setViewer(viewer);
|
|
|
|
/** Put user in client user map */
|
|
client.getUserMap().put(peerKey, user);
|
|
|
|
if(client.getConnection().getUser() == null) {
|
|
/** Assign user to client */
|
|
client.getConnection().setUser(user);
|
|
|
|
/** Select a default brush */
|
|
client.getUserInterface().getBrushButtons()[0].doClick();
|
|
}
|
|
|
|
/** Add canvas as phantom layer listener */
|
|
user.getPhantomLayer().addImageObserver(client.getCanvas());
|
|
|
|
/** Print message */
|
|
client.getUserInterface().println(user.getName() + " has signed in");
|
|
|
|
/** Play audio */
|
|
if(client.isSoundEnabled()) {
|
|
UserInterface.AUDIO_SIGN_IN.play();
|
|
}
|
|
|
|
/** Return silently */
|
|
return Connection.SEND_NONE;
|
|
}
|
|
}
|
|
|