Ignore:
Timestamp:
07/04/13 20:29:50 (11 years ago)
Author:
psniegowski
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • java/main/src/main/java/com/framsticks/hosting/InstanceClient.java

    r90 r96  
    11package com.framsticks.hosting;
     2
     3import static com.framsticks.util.lang.Strings.assureNotEmpty;
    24
    35import com.framsticks.communication.*;
    46import com.framsticks.communication.queries.ApplicationRequest;
     7import com.framsticks.communication.queries.CallRequest;
    58import com.framsticks.communication.queries.GetRequest;
    69import com.framsticks.communication.queries.InfoRequest;
     10import com.framsticks.communication.queries.SetRequest;
    711import com.framsticks.core.Instance;
     12import com.framsticks.core.InstanceUtils;
    813import com.framsticks.core.Path;
    914import com.framsticks.params.*;
     15import com.framsticks.params.types.ProcedureParam;
    1016import com.framsticks.parsers.Savers;
    11 import com.framsticks.core.LocalInstance;
     17import com.framsticks.util.FramsticksException;
    1218import com.framsticks.util.dispatching.AbstractJoinable;
    1319import com.framsticks.util.dispatching.Dispatching;
     
    1622import com.framsticks.util.dispatching.JoinableParent;
    1723import com.framsticks.util.dispatching.JoinableState;
     24import static com.framsticks.core.InstanceUtils.*;
    1825
    1926import java.net.Socket;
    20 import java.util.ArrayList;
    2127import java.util.LinkedList;
    2228import java.util.List;
    23 import com.framsticks.util.dispatching.RunAt;
    2429
    2530/**
     
    2833public class InstanceClient extends AbstractJoinable implements RequestHandler, JoinableParent {
    2934
    30         protected final LocalInstance instance;
     35        protected final Server server;
     36        protected final Instance instance;
    3137        protected final ServerConnection connection;
    3238
    33         public InstanceClient(LocalInstance instance, Socket socket) {
    34                 this.instance = instance;
    35                 connection = new ServerConnection(socket, this);
     39        public InstanceClient(Server server, Socket socket) {
     40                this.server = server;
     41                this.instance = server.hosted;
     42                this.connection = new ServerConnection(socket, this);
    3643        }
    3744
    3845        @Override
    39         public String toString() {
    40                 return instance + "|" + connection.toString();
     46        public String getName() {
     47                return connection + " to " + server;
    4148        }
    4249
    4350        @Override
    4451        public void handle(final ApplicationRequest request, final ResponseCallback<?> responseCallback) {
    45                 instance.dispatch(new RunAt<Instance>() {
     52                assureNotEmpty(request.getPath());
     53
     54                resolve(instance, request.getPath(), new Future<Path>(responseCallback) {
    4655                        @Override
    47                         public void run() {
    48                                 final Path path = instance.getPath(request.getPath());
    49                                 if (!path.isResolved(request.getPath())) {
    50                                         responseCallback.process(new Response(false, "\"invalid path\"", null));
     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));
    5169                                        return;
    5270                                }
     71
    5372                                if (request instanceof GetRequest) {
    54                                         instance.findInfo(path, new Future<FramsClass>() {
     73                                        InstanceUtils.findInfo(path, new Future<FramsClass>(responseCallback) {
    5574                                                @Override
    56                                                 public void result(FramsClass result, Exception e) {
     75                                                protected void result(FramsClass result) {
    5776                                                        if (result == null) {
    58                                                                 responseCallback.process(new Response(false, "\"failed to find info for access bind\"", null));
    59                                                                 return;
     77                                                                throw new FramsticksException().msg("failed to find info for access bind");
    6078                                                        }
    6179                                                        List<File> files = new LinkedList<File>();
    62                                                         AccessInterface access = instance.bindAccess(path);
     80                                                        AccessInterface access = bindAccess(path);
    6381
    6482                                                        if (access == null) {
     83                                                                throw new FramsticksException().msg("failed to bind access");
     84                                                        }
    6585
    66                                                                 responseCallback.process(new Response(false, "\"failed to bind access\"", null));
    67                                                                 return;
    68                                                         }
    6986                                                        ListSink sink = new ListSink();
    7087                                                        access.save(sink);
     
    7592                                        return;
    7693                                }
    77                                 if (request instanceof InfoRequest) {
    78                                         instance.findInfo(path, new Future<FramsClass>() {
     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) {
    7997                                                @Override
    80                                                 public void result(FramsClass result, Exception e) {
    81                                                         if (result == null) {
    82                                                                 responseCallback.process(new Response(false, "\"info not found\"", null));
    83                                                                 return;
     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);
    84104                                                        }
    85                                                         ListSink sink = new ListSink();
    86                                                         Savers.saveFramsClass(sink, result);
    87                                                         List<File> files = new ArrayList<File>();
    88                                                         files.add(new File(path.getTextual(), new ListSource(sink.getOut())));
    89                                                         responseCallback.process(new Response(true, null, files));
     105                                                        sink.print("]");
     106
     107                                                        responseCallback.process(new Response(true, "", File.single(new File("", new ListSource(sink.getOut())))));
    90108                                                }
    91109                                        });
    92110                                        return;
    93111                                }
    94                                 responseCallback.process(new Response(false, "invalid", null));
     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());
    95126                        }
    96127                });
     
    122153        }
    123154
     155
    124156}
Note: See TracChangeset for help on using the changeset viewer.