- Timestamp:
- 07/04/13 20:29:50 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/hosting/InstanceClient.java
r90 r96 1 1 package com.framsticks.hosting; 2 3 import static com.framsticks.util.lang.Strings.assureNotEmpty; 2 4 3 5 import com.framsticks.communication.*; 4 6 import com.framsticks.communication.queries.ApplicationRequest; 7 import com.framsticks.communication.queries.CallRequest; 5 8 import com.framsticks.communication.queries.GetRequest; 6 9 import com.framsticks.communication.queries.InfoRequest; 10 import com.framsticks.communication.queries.SetRequest; 7 11 import com.framsticks.core.Instance; 12 import com.framsticks.core.InstanceUtils; 8 13 import com.framsticks.core.Path; 9 14 import com.framsticks.params.*; 15 import com.framsticks.params.types.ProcedureParam; 10 16 import com.framsticks.parsers.Savers; 11 import com.framsticks. core.LocalInstance;17 import com.framsticks.util.FramsticksException; 12 18 import com.framsticks.util.dispatching.AbstractJoinable; 13 19 import com.framsticks.util.dispatching.Dispatching; … … 16 22 import com.framsticks.util.dispatching.JoinableParent; 17 23 import com.framsticks.util.dispatching.JoinableState; 24 import static com.framsticks.core.InstanceUtils.*; 18 25 19 26 import java.net.Socket; 20 import java.util.ArrayList;21 27 import java.util.LinkedList; 22 28 import java.util.List; 23 import com.framsticks.util.dispatching.RunAt;24 29 25 30 /** … … 28 33 public class InstanceClient extends AbstractJoinable implements RequestHandler, JoinableParent { 29 34 30 protected final LocalInstance instance; 35 protected final Server server; 36 protected final Instance instance; 31 37 protected final ServerConnection connection; 32 38 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); 36 43 } 37 44 38 45 @Override 39 public String toString() {40 return instance + "|" + connection.toString();46 public String getName() { 47 return connection + " to " + server; 41 48 } 42 49 43 50 @Override 44 51 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) { 46 55 @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)); 51 69 return; 52 70 } 71 53 72 if (request instanceof GetRequest) { 54 instance.findInfo(path, new Future<FramsClass>() {73 InstanceUtils.findInfo(path, new Future<FramsClass>(responseCallback) { 55 74 @Override 56 p ublic void result(FramsClass result, Exception e) {75 protected void result(FramsClass result) { 57 76 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"); 60 78 } 61 79 List<File> files = new LinkedList<File>(); 62 AccessInterface access = instance.bindAccess(path);80 AccessInterface access = bindAccess(path); 63 81 64 82 if (access == null) { 83 throw new FramsticksException().msg("failed to bind access"); 84 } 65 85 66 responseCallback.process(new Response(false, "\"failed to bind access\"", null));67 return;68 }69 86 ListSink sink = new ListSink(); 70 87 access.save(sink); … … 75 92 return; 76 93 } 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) { 79 97 @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); 84 104 } 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()))))); 90 108 } 91 109 }); 92 110 return; 93 111 } 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()); 95 126 } 96 127 }); … … 122 153 } 123 154 155 124 156 }
Note: See TracChangeset
for help on using the changeset viewer.