Showing posts with label Implementation. Show all posts
Showing posts with label Implementation. Show all posts

Sunday, January 6, 2008

GWT Proxy

Here is a nice way to write GWT Proxy to call the server services using the proxy design pattern:

The advantages of this proxy are:

  • The client call the service as it's a local call
  • The proxy hides the difference between calling the services in hosted and development modes.
  • It improves performance since the end point is set only once

First write this Client util class:

public class ClientUtil {
private static final String PRODUCTION_URL = "http://www.koko.com";

/**
* Check if the code run in production or development mode
* @return true if we run in production mode (i.e. Ruler.html is called)
*/
public static boolean isProduction() {
return GWT.isScript();
}

/**
* Set endpoint for service which want to access the server
* @param service the service class
* @param name the name of the service
*/
public static void setEndpointForService(Object service, String name) {
ServiceDefTarget endpoint = (ServiceDefTarget) service;
String moduleRelativeURL = getRelativeURL() + name;
endpoint.setServiceEntryPoint(moduleRelativeURL);
}

public static String getRelativeURL() {
String moduleRelativeURL = null;

if (!isProduction) {
moduleRelativeURL = GWT.getModuleBaseURL();
} else {
moduleRelativeURL = PRODUCTION_URL;
}

return moduleRelativeURL;
}
}

Then write your proxy class, for example:

package com.mycompany.client.util;

import com.google.gwt.core.client.GWT;

public class MyProxy {

private static MyServiceAsync myService = (MyServiceAsync) GWT.create(MyService.class);

static {
// serviceName is the name which define in app.gwt.xml file
ClientUtil.setEndpointForService(myService, "serviceName");
}

public static void foo() {
MyCallback myCallback = new MyCallback();
myService.getServerLog(myCallback);
}
}

finally to call the server use:

MyProxy.foo();



You can read more at:

Tuesday, December 11, 2007

Free Chat Implementation in GWT

I want to share my chat implementation for GWT, this is a pretty simple code which demonstrate how to write an Instand Messaging system inside the browser using AJAX and GWT technology.

The idea is simple, we have a chat factory in which each client can subscribe, There is a Chat Panel in which the user can send messages to the sever in async way. In the server there is a List which holds all the messages which all the clients sent.

There is a polling mechanism which run every few seconds and check if there are new chat messages to display.

The implementation is not state of the art but it works and it can be improved to work better.

Enjoy the implementation:


public class MyChatFactory {

private static final MyChatFactory instance = new MyChatFactory();
private static final int DELAY = 10000;

private MyChatFactory() {
}

public static MyChatFactory getInstance() {
return instance;
}

public void subscribeToEvent(final String queueName, final ChatCallback callback) {
ClientUtil.log("start subscribeToEvent");

Timer eventsTimer = new Timer() {
public void run() {
ChatProxy.getEvents(queueName, callback);
schedule(DELAY);
}
};

eventsTimer.schedule(DELAY);
}

public void sendMessage(String queueName, ChatMessage message) {
ChatProxy.sendMessage(queueName, message);
}

public void sendMessageToAll(ChatMessage message) {
ChatProxy.sendMessage(null, message);
}
}

public class ChatCallback extends GeneralCallback {

public ChatCallback() {
}

public void onSuccess(Object result) {
//ClientUtil.log("start ChatCallback.onSuccess");
if (result!=null) {
ChatMessage chatMessage = (ChatMessage) result;

if (chatMessage.isLogin()) {
ChatPanel.getInstance().addUser(chatMessage.getMessage());
} else {
ChatPanel.getInstance().addText(chatMessage.getMessage());
}
}
//ClientUtil.log("finish ChatCallback.onSuccess");
}

public void onFailure(Throwable caught) {
ClientUtil.log(caught.getMessage());
}
}

public class ChatMessage implements IsSerializable{

private String message;
private boolean isLogin;

public ChatMessage() {
}

public ChatMessage(String message, boolean isLogin) {
this.message=message;
this.isLogin=isLogin;
}


public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

public class ChatProxy {

private static ChatServiceAsync chatService = (ChatServiceAsync) GWT.create(ChatService.class);

static {
ClientUtil.setEndpointForService(chatService, "chat");
}

public static void getEvents(String queueName, ChatCallback callback) {
chatService.getEvents(queueName, callback);

}

public static void sendMessage(String queueName, ChatMessage message) {
VoidCallback callback = new VoidCallback();
chatService.sendMessage(queueName, message, callback);
}

}

public interface ChatService extends RemoteService {

public ChatMessage getEvents(String queueName);

public void sendMessage(String queueName, ChatMessage chatMessage);
}

public class ChatServiceImpl extends RemoteServiceServlet implements ChatService {

private static final long serialVersionUID = 1L;

private static Map subscribers = new HashMap();
//private static final long INTERVAL = 200000-1000;

public ChatMessage getEvents(String queueName) {
List list = null;

if (!subscribers.containsKey(queueName)) {
list = new ArrayList();
subscribers.put(queueName, list);
} else {
list = (List) subscribers.get(queueName);
}

if (!list.isEmpty()) {
ChatMessage message = (ChatMessage) list.get(0);
list.remove(0);
return message;
}

return null;
}

public void sendMessage(String queueName, ChatMessage chatMessage) {

if (queueName!=null) {
if (subscribers.containsKey(queueName)) {
List list = (List) subscribers.get(queueName);
list.add(chatMessage);
}
} else { // If null then send to all
Iterator subscribeIter = subscribers.values().iterator();

while (subscribeIter.hasNext()) {
List list = (List) subscribeIter.next();
list.add(chatMessage);
// list.notify();
}
}
}

}

Links

 
RSS Feeds Submission Directory