Skip to content

Commit

Permalink
fix multi renderer creation on webguiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfaceS committed Sep 8, 2023
1 parent 887246a commit bc86bd2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/pms/library/RootFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public synchronized void discoverChildren() {
if (isDiscovered()) {
return;
}
LOGGER.debug("Discovering the root folder for " + renderer.getRendererName());

//clear childrens but keep copy until discovered
backupChildren.clear();
Expand Down Expand Up @@ -515,6 +516,7 @@ public synchronized List<LibraryResource> getLibraryResources(String objectId, b

if (count > 0) {
String systemName = libraryContainer.getSystemName();
LOGGER.trace("Start of analysis for " + systemName);
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(count);

int nParallelThreads = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}
}

private static WebGuiRenderer getRenderer(HttpServletRequest req, String uuid) {
private static synchronized WebGuiRenderer getRenderer(HttpServletRequest req, String uuid) {
if (ConnectedRenderers.hasWebPlayerRenderer(uuid)) {
return ConnectedRenderers.getWebPlayerRenderer(uuid);
}
Expand All @@ -295,15 +295,17 @@ private static void createRenderer(HttpServletRequest req, String uuid) {
return;
}
try {
LOGGER.info("Founded new web gui renderer with uuid: {}", uuid);
String userAgent = req.getHeader("User-agent");
String langs = WebGuiServletHelper.getLangs(req);
WebGuiRenderer renderer = new WebGuiRenderer(uuid, account.getUser().getId(), userAgent, langs);
renderer.associateIP(WebGuiServletHelper.getInetAddress(req.getRemoteAddr()));
renderer.setActive(true);
renderer.getRootFolder();
//renderer.getRootFolder();
ConnectedRenderers.addWebPlayerRenderer(renderer);
LOGGER.debug("Created web gui renderer for " + renderer.getRendererName());
} catch (ConfigurationException ex) {
LOGGER.info("Error in loading configuration of WebPlayerRenderer");
LOGGER.warn("Error in loading configuration of WebPlayerRenderer");
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/net/pms/renderers/ConnectedRenderers.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* This class handle all renderers and devices found.
*/
public class ConnectedRenderers {

private static final Logger LOGGER = LoggerFactory.getLogger(ConnectedRenderers.class);
private static final Map<InetAddress, Renderer> ADDRESS_RENDERER_ASSOCIATION = Collections.synchronizedMap(new HashMap<>());
private static final Map<InetAddress, String> ADDRESS_UUID_ASSOCIATION = Collections.synchronizedMap(new HashMap<>());
Expand Down Expand Up @@ -70,8 +71,8 @@ public static Collection<Renderer> getConnectedRenderers() {
}

/**
* Tries to find a matching renderer configuration based on the given collection of
* request headers
* Tries to find a matching renderer configuration based on the given
* collection of request headers
*
* @param headers The headers.
* @param ia The request's origin address.
Expand All @@ -89,10 +90,10 @@ public static Renderer getRendererConfigurationByHeaders(SortedHeaderMap sortedH
r = ConnectedRenderers.resolve(ia, ref);
if (r != null) {
LOGGER.trace(
"Matched {}media renderer \"{}\" based on headers {}",
isNew ? "new " : "",
r.getRendererName(),
sortedHeaders
"Matched {}media renderer \"{}\" based on headers {}",
isNew ? "new " : "",
r.getRendererName(),
sortedHeaders
);
}
}
Expand Down Expand Up @@ -192,11 +193,7 @@ public static Renderer resolve(InetAddress ia, RendererConfiguration ref) {
}

public static void verify(Renderer r) {
// FIXME: this is a very fallible, incomplete validity test for use only until
// we find something better. The assumption is that renderers unable determine
// their own address (i.e. non-UPnP/web renderers that have lost their spot in the
// address association to a newer renderer at the same ip) are "invalid".
if (r.getUpnpMode() != Renderer.UPNP_BLOCK && r.getAddress() == null) {
if (!r.verify()) {
LOGGER.debug("Purging renderer {} as invalid", r);
r.delete(0);
}
Expand Down Expand Up @@ -358,15 +355,19 @@ public static String getRandomUUID() {

/**
* RendererMap was marking renderer via uuid.
*
* @param uuid
* @return Renderer
*/
public static void markRenderer(String uuid, int property, Object value) {
Renderer renderer = UUID_RENDERER_ASSOCIATION.get(uuid);
switch (property) {
case JUPnPDeviceHelper.ACTIVE -> renderer.setActive((boolean) value);
case JUPnPDeviceHelper.RENEW -> renderer.setRenew((boolean) value);
case JUPnPDeviceHelper.CONTROLS -> renderer.setControls((int) value);
case JUPnPDeviceHelper.ACTIVE ->
renderer.setActive((boolean) value);
case JUPnPDeviceHelper.RENEW ->
renderer.setRenew((boolean) value);
case JUPnPDeviceHelper.CONTROLS ->
renderer.setControls((int) value);
default -> {
//not handled
}
Expand All @@ -387,6 +388,7 @@ public static Renderer getUpNPRenderer(String uuid) {

/**
* RendererMap was creating renderer on the fly if not found.
*
* @param uuid
* @return Renderer
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/pms/renderers/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,14 @@ public boolean isUpnpAllowed() {
return getUpnpMode() > UPNP_NONE;
}

public boolean verify() {
// FIXME: this is a very fallible, incomplete validity test for use only until
// we find something better. The assumption is that renderers unable determine
// their own address (i.e. non-UPnP/web renderers that have lost their spot in the
// address association to a newer renderer at the same ip) are "invalid".
return getUpnpMode() == Renderer.UPNP_BLOCK || getAddress() != null;
}

public static int getUpnpMode(String mode) {
if (mode != null) {
return switch (mode.trim().toLowerCase()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/pms/renderers/devices/WebGuiRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public String getSubLanguage() {
return subLang;
}

@Override
public boolean isAllowed() {
return true;
}

@Override
public void setAllowed(boolean b) {
//nothing to change
}

public void sendMessage(String... args) {
JsonObject jObject = new JsonObject();
jObject.addProperty("action", "player");
Expand Down

0 comments on commit bc86bd2

Please sign in to comment.