source: java/main/src/main/java/com/framsticks/hosting/InstanceClient.java @ 96

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

HIGHLIGHTS:

  • cleanup Instance management
    • extract Instance interface
    • extract Instance common algorithms to InstanceUtils?
  • fix closing issues: Ctrl+C or window close button

properly shutdown whole program

by Java Framsticks framework

  • fix parsing and printing of all request types
  • hide exception passing in special handle method of closures
    • substantially improve readability of closures
    • basically enable use of exception in asynchronous closures

(thrown exception is transported back to the caller)

  • implement call request on both sides

CHANGELOG:
Further improve calling.

Improve instance calling.

Calling is working on both sides.

Improve exception handling in testing.

Waiters do not supercede other apllication exception being thrown.

Finished parsing and printing of all request types (with tests).

Move implementation and tests of request parsing to Request.

Add tests for Requests.

Improve waits in asynchronours tests.

Extract more algorithms to InstanceUtils?.

Extract Instance.resolve to InstanceUtils?.

Improve naming.

Improve passing exception in InstanceClient?.

Hide calling of passed functor in StateCallback?.

Hide Exception passing in asynchronous closures.

Hide exception passing in Future.

Make ResponseCallback? an abstract class.

Make Future an abstract class.

Minor change.

Move getPath to Path.to()

Move bindAccess to InstanceUtils?.

Extract common things to InstanceUtils?.

Fix synchronization bug in Connection.

Move resolve to InstanceUtils?.

Allow names of Joinable to be dynamic.

Add support for set request server side.

More fixes in communication.

Fix issues with parsing in connection.

Cut new line characters when reading.

More improvements.

Migrate closures to FramsticksException?.

Several changes.

Extract resolveAndFetch to InstanceUtils? algorithms.

Test resolving and fetching.

More fixes with function signature deduction.

Do not print default values in SimpleAbstractAccess?.

Add test of FramsClass? printing.

Improve FramsticksException? messages.

Add explicit dispatcher synchronization feature.

Rework assertions in tests.

Previous solution was not generic enough.

Allow addition of joinables to collection after start.

Extract SimulatorInstance? from RemoteInstance?.

Remove PrivateJoinableCollection?.

Improve connections.

Move shutdown hook to inside the Monitor.

It should work in TestNG tests, but it seems that
hooks are not called.

In ServerTest? client connects to testing server.

Move socket initialization to receiver thread.

Add proper closing on Ctrl+C (don't use signals).

Fix bugs with server accepting connections.

Merge Entity into Joinable.

Reworking ServerInstance?.

Extract more algorithm to InstanceUtils?.

Extract some common functionality from AbstractInstance?.

Functions were placed in InstanceUtils?.

Hide registry of Instance.

Use ValueParam? in Instance interface.

Minor change.

Extract Instance interface.

Old Instance is now AbstractInstance?.

File size: 4.9 KB
Line 
1package com.framsticks.hosting;
2
3import static com.framsticks.util.lang.Strings.assureNotEmpty;
4
5import com.framsticks.communication.*;
6import com.framsticks.communication.queries.ApplicationRequest;
7import com.framsticks.communication.queries.CallRequest;
8import com.framsticks.communication.queries.GetRequest;
9import com.framsticks.communication.queries.InfoRequest;
10import com.framsticks.communication.queries.SetRequest;
11import com.framsticks.core.Instance;
12import com.framsticks.core.InstanceUtils;
13import com.framsticks.core.Path;
14import com.framsticks.params.*;
15import com.framsticks.params.types.ProcedureParam;
16import com.framsticks.parsers.Savers;
17import com.framsticks.util.FramsticksException;
18import com.framsticks.util.dispatching.AbstractJoinable;
19import com.framsticks.util.dispatching.Dispatching;
20import com.framsticks.util.dispatching.Future;
21import com.framsticks.util.dispatching.Joinable;
22import com.framsticks.util.dispatching.JoinableParent;
23import com.framsticks.util.dispatching.JoinableState;
24import static com.framsticks.core.InstanceUtils.*;
25
26import java.net.Socket;
27import java.util.LinkedList;
28import java.util.List;
29
30/**
31 * @author Piotr Sniegowski
32 */
33public class InstanceClient extends AbstractJoinable implements RequestHandler, JoinableParent {
34
35        protected final Server server;
36        protected final Instance instance;
37        protected final ServerConnection connection;
38
39        public InstanceClient(Server server, Socket socket) {
40                this.server = server;
41                this.instance = server.hosted;
42                this.connection = new ServerConnection(socket, this);
43        }
44
45        @Override
46        public String getName() {
47                return connection + " to " + server;
48        }
49
50        @Override
51        public void handle(final ApplicationRequest request, final ResponseCallback<?> responseCallback) {
52                assureNotEmpty(request.getPath());
53
54                resolve(instance, request.getPath(), new Future<Path>(responseCallback) {
55                        @Override
56                        protected void result(final Path path) {
57
58                                // final AccessInterface access = instance.prepareAccess(path);
59                                final AccessInterface access = instance.prepareAccess(path.getTop().getParam());
60
61                                if (request instanceof SetRequest) {
62                                        SetRequest set = (SetRequest) request;
63                                        //TODO Proxy - here is break of chain, instance should have hosted
64                                        //hosted set
65                                        AccessInterface access2 = InstanceUtils.bindAccess(path);
66                                        int flag = access2.set(set.getField(), set.getValue());
67
68                                        responseCallback.process(new Response(true, Flags.write(flag, null), null));
69                                        return;
70                                }
71
72                                if (request instanceof GetRequest) {
73                                        InstanceUtils.findInfo(path, new Future<FramsClass>(responseCallback) {
74                                                @Override
75                                                protected void result(FramsClass result) {
76                                                        if (result == null) {
77                                                                throw new FramsticksException().msg("failed to find info for access bind");
78                                                        }
79                                                        List<File> files = new LinkedList<File>();
80                                                        AccessInterface access = bindAccess(path);
81
82                                                        if (access == null) {
83                                                                throw new FramsticksException().msg("failed to bind access");
84                                                        }
85
86                                                        ListSink sink = new ListSink();
87                                                        access.save(sink);
88                                                        files.add(new File(path.getTextual(), new ListSource(sink.getOut())));
89                                                        responseCallback.process(new Response(true, "", files));
90                                                }
91                                        });
92                                        return;
93                                }
94                                if (request instanceof CallRequest) {
95                                        final CallRequest callRequest = (CallRequest) request;
96                                        instance.call(path, access.getFramsClass().getParamEntry(callRequest.getProcedure(), ProcedureParam.class), callRequest.getArguments().toArray(), new Future<Object>(responseCallback) {
97                                                @Override
98                                                protected void result(Object result) {
99                                                        ListSink sink = new ListSink();
100                                                        sink.print("Result:").breakLine();
101                                                        sink.print("value:").print("[");
102                                                        if (result != null) {
103                                                                sink.print(result);
104                                                        }
105                                                        sink.print("]");
106
107                                                        responseCallback.process(new Response(true, "", File.single(new File("", new ListSource(sink.getOut())))));
108                                                }
109                                        });
110                                        return;
111                                }
112                                if (request instanceof InfoRequest) {
113                                        findInfo(path, new Future<FramsClass>(responseCallback) {
114                                                @Override
115                                                protected void result(FramsClass result) {
116                                                        if (result == null) {
117                                                                throw new FramsticksException().msg("info not found");
118                                                        }
119                                                        responseCallback.process(new Response(true, null, File.single(new File(path.getTextual(), new ListSource(Savers.saveFramsClass(new ListSink(), result).getOut())))));
120                                                }
121                                        });
122                                        return;
123                                }
124
125                                throw new FramsticksException().msg("invalid request type: " + request.getCommand());
126                        }
127                });
128
129        }
130
131        @Override
132        protected void joinableStart() {
133                Dispatching.use(connection, this);
134        }
135
136        @Override
137        protected void joinableInterrupt() {
138                Dispatching.drop(connection, this);
139        }
140
141        @Override
142        protected void joinableFinish() {
143        }
144
145        @Override
146        protected void joinableJoin() throws InterruptedException {
147                Dispatching.join(connection);
148        }
149
150        @Override
151        public void childChangedState(Joinable joinable, JoinableState state) {
152                proceedToState(state);
153        }
154
155
156}
Note: See TracBrowser for help on using the repository browser.