package com.example.vaadin7scratch;
import java.io.*;
import java.net.*;
import javax.servlet.annotation.WebServlet;
import com.google.gwt.thirdparty.guava.common.eventbus.AllowConcurrentEvents;
import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import dispatching.EventBroadcastEvent;
import dispatching.EventBusSingleton;
@SuppressWarnings("serial")
@Theme("vaadin7scratch")
public class Vaadin7scratchUI extends UI {
Socket client = null;
DataOutputStream out = null;
BufferedWriter bw = null;
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Vaadin7scratchUI.class)
public static class Servlet extends VaadinServlet {
}
@Subscribe
@AllowConcurrentEvents
private void readIncomingMessages(EventBroadcastEvent e){
Notification.show("Message containing: "+e.getName());
}
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
EventBusSingleton.getInstance().register(this);
write("This is message is written as soon as the user beholds the newly created UI."+"\n");
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
write("New Message");
layout.addComponent(new Label("Thank you for clicking and Your new message"));
}
});
layout.addComponent(button);
}
private void write(String towrite){
try {
client = new Socket("127.0.0.1", 8754);
out = new DataOutputStream(client.getOutputStream());
bw = new BufferedWriter(new OutputStreamWriter(out));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
bw.write(towrite);
bw.flush();
//bw.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
At the moment, I am writing a chat application for autodidactic purposes. I have created a Server socket which is supposed to broadcast an event after receiving a message by a stream, and I want to catch this event and show it by a Notification. The VaadinGUI should simply send a String “New Message” to the server, and the server should broadcast an event which I want to catch with an event subscriber method (see above).
Now this is my server:
[code]
package sockets;
import java.net.;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.io.;
import com.google.gwt.thirdparty.guava.common.eventbus.EventBus;
import com.vaadin.ui.Notification;
import dispatching.EventBroadcastEvent;
import dispatching.EventBusSingleton;
public class Server extends Thread
{
private ServerSocket serverSocket;
private HashMap<String,Socket> dispatch = new HashMap<String,Socket>();
private BufferedReader br = null;
private BufferedWriter bw = null;
public Server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
}
public void run()
{
while(true)
{
try
{
Socket server = serverSocket.accept();
try{
br = new BufferedReader(new InputStreamReader(new DataInputStream(server.getInputStream())));
String message = br.readLine();
EventBusSingleton.getInstance().post(new EventBroadcastEvent(message));
}catch(IOException e){e.printStackTrace();}
}catch(SocketTimeoutException s)
{
System.out.println(“Socket timed out!”);
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String args)
{
try
{
Thread t = new Server(8754);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
[/code]I create the EventBus with a singleton pattern - as follows:
[code]
package dispatching;
import com.google.gwt.thirdparty.guava.common.eventbus.EventBus;
public class EventBusSingleton {
private static EventBus eventBus = null;
private EventBusSingleton() {}
public static EventBus getInstance() {
if (eventBus == null) {
eventBus = new EventBus();
}
return eventBus;
}
}
[/code]Unfortunately, no Notification whatsoever is shown; but I cannot figure out what the mistake is.