Skip to content

Commit

Permalink
Did some basic work on voice connections
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Jul 16, 2017
1 parent 8530f5f commit 8f8447f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/Lavalink.iml
/logs/
/application.yml
19 changes: 19 additions & 0 deletions src/main/java/lavalink/server/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lavalink.server;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "lavalink.server")
@Component
public class Config {

private String userId;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}
}
4 changes: 3 additions & 1 deletion src/main/java/lavalink/server/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
@Controller
public class Launcher {

public static Config config;
public final SocketServer socketServer;

@Autowired
public Launcher(SocketServer socketServer) {
public Launcher(Config config, SocketServer socketServer) {
Launcher.config = config;
this.socketServer = socketServer;
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/lavalink/server/io/CoreClientImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lavalink.server.io;

import org.java_websocket.WebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CoreClientImpl implements net.dv8tion.jda.CoreClient {

private static final Logger log = LoggerFactory.getLogger(CoreClientImpl.class);

private final WebSocket socket;

public CoreClientImpl(WebSocket socket) {
this.socket = socket;
}

@Override
public void sendWS(String s) {
log.info("SEND_WS " + s);
}

@Override
public boolean isConnected() {
return false;
}

@Override
public boolean inGuild(String s) {
return true;
}

@Override
public boolean voiceChannelExists(String s) {
return true;
}

@Override
public boolean hasPermissionInChannel(String s, long l) {
return true;
}

}
31 changes: 31 additions & 0 deletions src/main/java/lavalink/server/io/SocketContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lavalink.server.io;

import lavalink.server.Launcher;
import net.dv8tion.jda.Core;
import org.java_websocket.WebSocket;

public class SocketContext {

private final WebSocket socket;
private final CoreClientImpl coreClient;
private final Core core;

SocketContext(WebSocket socket) {
this.socket = socket;
this.coreClient = new CoreClientImpl(socket);
this.core = new Core(Launcher.config.getUserId(), coreClient);
}

public WebSocket getSocket() {
return socket;
}

public CoreClientImpl getCoreClient() {
return coreClient;
}

public Core getCore() {
return core;
}

}
22 changes: 20 additions & 2 deletions src/main/java/lavalink/server/io/SocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static lavalink.server.io.WSCodes.*;

public class SocketServer extends WebSocketServer {

private static final Logger log = LoggerFactory.getLogger(SocketServer.class);
private static final Map<WebSocket, SocketContext> contextMap = new HashMap<>();
private final String password;

public SocketServer(String password) {
Expand All @@ -22,6 +26,7 @@ public SocketServer(String password) {
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
if (clientHandshake.getFieldValue("Authorization").equals(password)) {
log.info("Connection opened from " + webSocket.getRemoteSocketAddress() + " with protocol " + webSocket.getDraft());
contextMap.put(webSocket, new SocketContext(webSocket));
} else {
log.error("Authentication failed from " + webSocket.getRemoteSocketAddress() + " with protocol " + webSocket.getDraft());
webSocket.close(AUTHORIZATION_REJECTED, "Authorization rejected");
Expand All @@ -31,6 +36,7 @@ public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
@Override
public void onClose(WebSocket webSocket, int i, String s, boolean b) {
log.info("Connection closed from " + webSocket.getRemoteSocketAddress().toString() + " with protocol " + webSocket.getDraft());
contextMap.remove(webSocket);
}

@Override
Expand All @@ -42,9 +48,21 @@ public void onMessage(WebSocket webSocket, String s) {
}

switch (json.getString("op")) {
case "create":
case "connect":
contextMap.get(webSocket).getCore().getConnectionManager().queueAudioConnect(
json.getString("guildId"),
json.getString("channelId")
);
break;
case "voiceUpdate":
contextMap.get(webSocket).getCore().provideVoiceServerUpdate(
json.getString("sessionId"),
json.getJSONObject("event")
);
break;
case "destroy":
case "disconnect":
contextMap.get(webSocket).getCore().getAudioManager(json.getString("guildId"))
.closeAudioConnection();
break;
}
}
Expand Down

0 comments on commit 8f8447f

Please sign in to comment.