source: java/main/src/main/java/com/framsticks/hosting/Server.java @ 97

Last change on this file since 97 was 97, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 5.7 KB
Line 
1package com.framsticks.hosting;
2
3import org.apache.log4j.Level;
4import org.apache.log4j.Logger;
5
6import com.framsticks.core.Tree;
7import com.framsticks.params.annotations.AutoAppendAnnotation;
8import com.framsticks.params.annotations.FramsClassAnnotation;
9import com.framsticks.params.annotations.ParamAnnotation;
10import com.framsticks.util.FramsticksException;
11import com.framsticks.util.dispatching.AbstractJoinable;
12import com.framsticks.util.dispatching.Dispatching;
13import com.framsticks.util.dispatching.Joinable;
14import com.framsticks.util.dispatching.JoinableCollection;
15import com.framsticks.util.dispatching.JoinableParent;
16import com.framsticks.util.dispatching.JoinableState;
17import com.framsticks.util.dispatching.RunAt;
18import com.framsticks.util.dispatching.Task;
19import com.framsticks.util.dispatching.ThrowExceptionHandler;
20
21import java.io.IOException;
22import java.net.InetSocketAddress;
23import java.net.ServerSocket;
24import java.net.Socket;
25import com.framsticks.util.dispatching.Thread;
26
27@FramsClassAnnotation
28public class Server extends AbstractJoinable implements JoinableParent {
29
30        private final static Logger log = Logger.getLogger(Server.class);
31
32        protected int port;
33
34        protected ServerSocket acceptSocket;
35        protected Tree hosted;
36        protected final JoinableCollection<ClientAtServer> clients = new JoinableCollection<ClientAtServer>();
37
38        public static class Accept {
39        };
40
41        protected Thread<Accept> acceptThread = new Thread<>();
42
43        /**
44         *
45         */
46        public Server() {
47                log.debug("created server");
48                port = 9009;
49        }
50
51        /**
52         * @return the port
53         */
54        @ParamAnnotation
55        public int getPort() {
56                return port;
57        }
58
59        /**
60         * @param port the port to set
61         */
62        @ParamAnnotation
63        public void setPort(int port) {
64                this.port = port;
65        }
66
67        @Override
68        protected void joinableInterrupt() {
69                Dispatching.drop(acceptThread, this);
70                Dispatching.drop(hosted, this);
71                Dispatching.drop(clients, this);
72                try {
73                        acceptSocket.close();
74                } catch (IOException e) {
75                        log.debug("exception caught during socket closing: ", e);
76                }
77                finish();
78        }
79
80        /**
81         * @return the hosted
82         */
83        public Tree getHosted() {
84                return hosted;
85        }
86
87        @AutoAppendAnnotation
88        public void setHosted(Tree hosted) {
89                if (this.hosted != null) {
90                        throw new FramsticksException().msg("hosted tree is already set").arg("current", this.hosted);
91                }
92                this.hosted = hosted;
93                acceptThread.setName(hosted.getName() + " acceptor");
94                clients.setObservableName(hosted.getName() + " clients");
95        }
96
97        @Override
98        public void childChangedState(Joinable joinable, JoinableState state) {
99                proceedToState(state);
100        }
101
102        @Override
103        protected void joinableStart() {
104                Dispatching.use(acceptThread, this);
105                Dispatching.use(hosted, this);
106                Dispatching.use(clients, this);
107                try {
108                        acceptSocket = new ServerSocket();
109                } catch (IOException e) {
110                        throw new FramsticksException().msg("failed to create server socket").cause(e);
111                }
112                tryBind(0);
113        }
114
115        @Override
116        @ParamAnnotation
117        public String getName() {
118                return hosted != null ? hosted.getName() : "server";
119        }
120
121        protected void acceptNext() {
122                if (!isRunning()) {
123                        log.debug("server is not in running state, aborting accepting");
124                        return;
125                }
126                //TODO TEH
127                acceptThread.dispatch(new RunAt<Accept>(ThrowExceptionHandler.getInstance()) {
128                        @Override
129                        protected void runAt() {
130                                try {
131                                        log.debug("accepting");
132                                        final Socket socket = acceptSocket.accept();
133                                        assert socket != null;
134                                        log.debug("accepted socket: " + socket.getInetAddress().getHostAddress());
135                                        hosted.dispatch(new RunAt<Tree>(this) {
136                                                @Override
137                                                protected void runAt() {
138                                                        ClientAtServer client = new ClientAtServer(Server.this, socket);
139                                                        clients.add(client);
140                                                        log.info("client connected: " + client);
141                                                }
142                                        });
143                                } catch (IOException e) {
144                                        log.log((isRunning() ? Level.ERROR : Level.DEBUG), "failed to accept socket: " + e);
145                                }
146                                acceptNext();
147                        }
148                });
149        }
150
151        protected void tryBind(int when) {
152                acceptThread.dispatch(new Task<Accept>(ThrowExceptionHandler.getInstance(), when) {
153                        @Override
154                        protected void runAt() {
155                                try {
156                                        acceptSocket.bind(new InetSocketAddress(port));
157                                        log.debug("started accepting on port " + port);
158                                        acceptNext();
159                                        return;
160                                } catch (IOException e) {
161                                        log.warn("failed to accept on port " + port + " (repeating): " + e);
162                                }
163                                tryBind(1000);
164                        }
165                });
166        }
167
168        @Override
169        protected void joinableFinish() {
170
171        }
172
173        @Override
174        protected void joinableJoin() throws InterruptedException {
175                Dispatching.join(acceptThread);
176                Dispatching.join(hosted);
177                Dispatching.join(clients);
178        }
179
180        // @Override
181        // public FramsClass getInfoFromCache(String id) {
182        //      assert isActive();
183        //      if (id == null) {
184        //              return null;
185        //      }
186        //      FramsClass cached = registry.getFramsClass(id);
187        //      if (cached != null) {
188        //              return cached;
189        //      }
190        //      try {
191        //              Class<?> nativeClass = Class.forName(id);
192        //              FramsClass framsClass = FramsClass.build().forClass(nativeClass);
193
194        //              if (!framsClass.getId().equals(id)) {
195        //                      log.error("no matching id");
196        //                      return null;
197        //              }
198
199        //              registry.registerReflectedClass(null, id, nativeClass);
200        //              registry.putFramsClass(framsClass);
201        //              return framsClass;
202        //      } catch (ClassNotFoundException ignored) {
203        //      } catch (ConstructionException e) {
204        //              log.error("failed to use info from cache: " + e);
205        //      }
206
207        //      return null;
208        // }
209
210        // @Override
211        // protected void fetchInfo(Path path, Future<FramsClass> future) {
212        //      assert isActive();
213
214        //      FramsClass framsClass = getInfoFromCache(path.getTop().getObject().getClass().getCanonicalName());
215        //      if (framsClass == null) {
216        //              log.error("failed to create frams class for: " + path.getTop().getObject().getClass());
217        //              future.result(null, new Exception());
218        //              return;
219        //      }
220        //      future.result(framsClass, null);
221        // }
222}
Note: See TracBrowser for help on using the repository browser.