Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket & WebNotifications: Stability and Logging Improvements #591

Merged
merged 11 commits into from
Jul 25, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,34 @@
import com.genexus.websocket.Session;

@ServerEndpoint(value = "/gxwebsocket")
public class GXWebSocket extends GXWebSocketCommon implements IGXWebSocketAsync {

private static GXWebSocket instance = null;

public GXWebSocket(){
instance = this;
}

public static IGXWebSocketAsync getInstance() {
return instance;
public class GXWebSocket {

private GXWebSocketService wsService;

public GXWebSocket() {
wsService = GXWebSocketService.getService();
}

@OnOpen
public void OnOpen (javax.websocket.Session session) {
OnOpenCommon(new Session(session));
public void onOpen(javax.websocket.Session session) {
wsService.onOpen(new Session(session));
}

@OnMessage
public void OnMessage (String txt, javax.websocket.Session session) {
OnMessageCommon(txt, new Session(session));
public void onMessage(String txt, javax.websocket.Session session) {
wsService.onMessage(txt, new Session(session));
}

@OnClose
public void myOnClose (javax.websocket.Session session, CloseReason reason) {
myOnCloseCommon(new Session(session));
public void onClose(javax.websocket.Session session, CloseReason reason) {
wsService.onClose(new Session(session));
}

@OnError
public void onError(Throwable exception, javax.websocket.Session session) {
onErrorCommon(exception, new Session(session));
wsService.onError(exception, new Session(session));
}

public SendResponseType send(String clientId, String message) {
return sendCommon(clientId, message);
}

public void broadcast(String message) {
broadcastCommon(message);
}

public boolean start() {
return true;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Integer getHashCode() {
return new Integer(session.hashCode());
}

public String getQueryString() {
public String getId() {
return session.getQueryString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,33 @@
import com.genexus.websocket.Session;

@ServerEndpoint(value = "/gxwebsocket")
public class GXWebSocket extends GXWebSocketCommon implements IGXWebSocketAsync {

private static GXWebSocket instance = null;

public GXWebSocket(){
instance = this;
}

public static IGXWebSocketAsync getInstance() {
return instance;
public class GXWebSocket {

private GXWebSocketService wsService;

public GXWebSocket() {
wsService = GXWebSocketService.getService();
}

@OnOpen
public void OnOpen (jakarta.websocket.Session session) {
OnOpenCommon(new Session(session));
public void onOpen(jakarta.websocket.Session session) {
wsService.onOpen(new Session(session));
}

@OnMessage
public void OnMessage (String txt, jakarta.websocket.Session session) {
OnMessageCommon(txt, new Session(session));
public void onMessage(String txt, jakarta.websocket.Session session) {
wsService.onMessage(txt, new Session(session));
}

@OnClose
public void myOnClose (jakarta.websocket.Session session, CloseReason reason) {
myOnCloseCommon(new Session(session));
public void onClose(jakarta.websocket.Session session, CloseReason reason) {
wsService.onClose(new Session(session));
}

@OnError
public void onError(Throwable exception, jakarta.websocket.Session session) {
onErrorCommon(exception, new Session(session));
}

public SendResponseType send(String clientId, String message) {
return sendCommon(clientId, message);
public void onError(Throwable exception, jakarta.websocket.Session session) {
wsService.onError(exception, new Session(session));
}

public void broadcast(String message) {
broadcastCommon(message);
}

public boolean start() {
return true;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import jakarta.websocket.RemoteEndpoint;
import java.io.IOException;

public class Session implements ISession{
public class Session implements ISession {
private jakarta.websocket.Session session;

public Session(jakarta.websocket.Session session) {
Expand All @@ -14,7 +14,7 @@ public Integer getHashCode() {
return new Integer(session.hashCode());
}

public String getQueryString() {
public String getId() {
return session.getQueryString();
}

Expand Down
10 changes: 3 additions & 7 deletions java/src/main/java/com/genexus/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import java.util.*;

import com.genexus.db.DBConnectionManager;
import com.genexus.db.DynamicExecute;
Expand Down Expand Up @@ -42,7 +39,6 @@ public class Application
private static Boolean isJMXEnabled;

public static Class gxCfg = ApplicationContext.getInstance().getClass();
//public static ModelContext clientContext;
private static Vector<ICleanedup> toCleanup = new Vector<>();
static LocalUtil localUtil;
static Class ClassName = null;
Expand Down Expand Up @@ -753,10 +749,10 @@ public static boolean getShowConnectError()


static boolean useSmartCache = false;

public static com.genexus.GXSmartCacheProvider getSmartCacheProvider(int handle)
{
useSmartCache = true;
return getConnectionManager().getUserInformation(handle).getSmartCacheProvider();
}

}
}
117 changes: 44 additions & 73 deletions java/src/main/java/com/genexus/internet/GXWebNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,35 @@
import com.genexus.ModelContext;
import com.genexus.diagnostics.core.ILogger;
import com.genexus.diagnostics.core.LogManager;
import com.genexus.internet.websocket.IGXWebSocketAsync;
import com.genexus.internet.websocket.GXWebSocketService;
import com.genexus.internet.websocket.SendResponseType;
import com.genexus.xml.GXXMLSerializable;

public class GXWebNotification {

public static final ILogger logger = LogManager.getLogger(GXWebNotification.class);
private GXWebSocketService wsService;
private HttpContext httpContext;

private static IGXWebSocketAsync ws;
private HttpContext _ctx;

private short _errCode;
private String _errDescription;


private short errCode;
private String errDescription;

public short getErrCode()
{
return _errCode;
return errCode;
}

public String getErrDescription()
{
return _errDescription;
return errDescription;
}

public GXWebNotification(ModelContext gxContext)
{
_ctx = (HttpContext) gxContext.getHttpContext();
if (ws == null)
{
try {
setError((short)1);
Class<?> c = Class.forName("com.genexus.internet.websocket.GXWebSocket");
java.lang.reflect.Method method = c.getDeclaredMethod("getInstance", (Class[])null);
Object o = method.invoke(null, (Object[])null);
ws = (IGXWebSocketAsync)o;
if (ws != null)
setError((short)0);
} catch (Exception e) {
logger.error("GXWebNotification", e);
}
}
if (_errCode != 0)
{
logger.error("Could not create com.genexus.internet.GXWebSocket instance. Check whether WebServer requirements are met and WebNotifications Provider Generator Property is set");
}
{
wsService = GXWebSocketService.getService();
httpContext = (HttpContext) gxContext.getHttpContext();
}

public short notifyClient(String clientId, String message)
{
return notifyClientImpl(clientId, message);
Expand All @@ -62,43 +43,38 @@ public short notifyClient(String clientId, GXXMLSerializable message)
}

public short notifyClientImpl(String clientId, String message)
{
if (ws != null)
{
SendResponseType result = wsService.send(clientId.trim(), message);
switch (result)
{
SendResponseType result = ws.send(clientId.trim(), message);
switch (result)
{
case OK:
setError((short)0);
break;
case SessionNotFound:
setError((short)2);
break;
case SessionInvalid:
setError((short)3);
break;
case SendFailed:
setError((short)4);
break;
default:
break;
}
case OK:
setError((short)0);
break;
case SessionNotFound:
setError((short)2);
break;
case SessionInvalid:
setError((short)3);
break;
case SendFailed:
setError((short)4);
break;
default:
break;
}
else
setError((short)1);

return _errCode;

return errCode;
}

public short notify(String message)
{
return notifyClient(_ctx.getClientId(), message);
return notifyClient(httpContext.getClientId(), message);
}


public short notify(GXXMLSerializable message)
{
return notifyClient(_ctx.getClientId(), message);
return notifyClient(httpContext.getClientId(), message);
}


Expand All @@ -113,44 +89,39 @@ public void broadcast(String message)
}

private void broadcastImpl(String message)
{
if (ws != null)
{
ws.broadcast(message);
setError((short)0);
}
else
setError((short)1);
{
wsService.broadcast(message);
setError((short)0);
}

private void setError(short i)
{
this._errCode = i;
this.errCode = i;
switch (i)
{
case 0:
_errDescription = "OK";
errDescription = "OK";
break;
case 1:
_errDescription = "Could not start WebSocket Server";
errDescription = "WebSocket Server has not been initialized yet. No incoming connections were received";
break;
case 2:
_errDescription = "WebSocket Session not found";
errDescription = "WebSocket Session not found. The client is not connected to socket server";
break;
case 3:
_errDescription = "WebSocket Session is closed or invalid";
errDescription = "WebSocket Session was found, but it's state was closed or invalid";
break;
case 4:
_errDescription = "Message could not be delivered to client";
errDescription = "Message could not be delivered to client because of a connection error";
break;
default:
_errDescription = "Unknown error";
errDescription = "Unknown error";
break;
}
}

public String getClientId()
{
return _ctx.getClientId();
return httpContext.getClientId();
}
}
Loading