Annotation of OpenXM/src/Macaulay2/m2/oxserver.m2, Revision 1.1
1.1 ! takayama 1: --$OpenXM$
! 2: -- server commands
! 3: --ox'server'send := (OXSERVER, String) -> (...);
! 4: -- This sends a sequence of bytes to the client.
! 5: -- Note: a shutdown command is never received.
! 6:
! 7: -- Implementation of the OX stack machine, using OX_COMMAND:
! 8:
! 9:
! 10: OX'commands = new MutableHashTable;
! 11:
! 12: -- Local data for this stack machine.
! 13: ox'init'sm = () -> (
! 14: ox'stack = new MutableList from splice {200:null};
! 15: ox'sp = 0; -- Number of elements currently contained.
! 16: );
! 17:
! 18: ox'view = () -> (
! 19: if ox'sp === 0 then (stderr << "stack empty" << endl;)
! 20: else
! 21: scan(ox'sp, i -> (stderr << "at " << i << " have " << ox'stack#i << endl;)))
! 22:
! 23: ox'popObject = (packetNum) -> (
! 24: if ox'sp === 0 then
! 25: ox'error(packetNum,"stack underflow")
! 26: else (
! 27: ox'sp = ox'sp-1;
! 28: val := ox'stack#ox'sp;
! 29: ox'stack#ox'sp = null;
! 30: val))
! 31:
! 32: ox'pop = (packetNum,typ) -> (
! 33: val := ox'popObject packetNum;
! 34: if class val =!= typ then
! 35: ox'error(packetNum,"incorrect type on stack");
! 36: val)
! 37:
! 38: ox'push = (val) -> (
! 39: if ox'sp > #ox'stack then (
! 40: -- increase the size of the stack
! 41: newstack := new MutableList from splice{2 * #ox'stack : null};
! 42: scan(#ox'stack, i -> newstack#i = ox'stack#i);
! 43: ox'stack = newstack;
! 44: );
! 45: ox'stack#ox'sp = val;
! 46: ox'sp = ox'sp + 1;
! 47: )
! 48:
! 49: ox'error = (packetNum,s) -> ox'push ERROR{int32 packetNum,s}
! 50:
! 51: OX'commands#SM'popCMO = (packetNum) -> (
! 52: ox'popObject packetNum
! 53: )
! 54:
! 55: OX'commands#SM'popString = (packetNum) -> (
! 56: v := ox'popObject packetNum;
! 57: toString v
! 58: )
! 59:
! 60: OX'commands#SM'pops = (packetNum) -> (
! 61: n := ox'stack#ox'sp;
! 62: if n > ox'sp then n = ox'sp;
! 63: scan(n, i -> ox'popObject packetNum))
! 64:
! 65: OX'commands#SM'getsp = (packetNum) -> (
! 66: -- Place the number of objects onto the stack.
! 67: ox'push(INT32(ox'sp)))
! 68:
! 69: OX'commands#SM'nop = (packetNum) -> null
! 70:
! 71: OX'commands#SM'setName = (packetNum) -> (
! 72: varname := ox'pop(packetNum,String);
! 73: val := ox'popObject(packetNum);
! 74: (value ("symbol " | varname)) <- val;
! 75: )
! 76:
! 77: OX'commands#SM'evalName = (packetNum) -> (
! 78: varname := ox'pop(packetNum,String);
! 79: val := value ("symbol " | varname);
! 80: ox'push val;
! 81: )
! 82:
! 83: OX'commands#SM'executeStringByLocalParser = (packetNum) -> (
! 84: str := ox'pop(packetNum,String);
! 85: try (
! 86: val := value str;
! 87: ox'push val;
! 88: ) else
! 89: ox'error(packetNum,"Error occured");
! 90: )
! 91:
! 92: OX'commands#SM'executeFunction = (packetNum) -> (
! 93: fcn := ox'pop(PacketNum,String);
! 94: n := ox'pop(PacketNum,INT32);
! 95: n = n#0;
! 96: args := toSequence apply(n, i -> ox'popObject(packetNum));
! 97: try (
! 98: val := (value fcn)(args);
! 99: ox'push val;
! 100: ) else
! 101: ox'error(packetNum,"Error occured");
! 102: )
! 103:
! 104: OX'commands#SM'dupErrors = (packetNum) ->
! 105: ox'push select(ox'sp, i -> class i === ERROR)
! 106:
! 107: OX'commands#SM'mathcap = (packetNum) -> (
! 108: ox'push(MATHCAP'OF'M2'SERVER);
! 109: )
! 110:
! 111: OX'commands#SM'setMathCap = (packetNum) -> (
! 112: val := ox'pop(packetNum,MATHCAP);
! 113: -- BUG: we are ignoring the math cap!!
! 114: )
! 115: -----------------
! 116: -- Server code --
! 117: -----------------
! 118:
! 119: OXSERVER = new Type of MutableHashTable
! 120:
! 121: ox'send'data = () -> (
! 122: << OXDATA << toNetwork(ox'next'packet);
! 123: ox'next'packet = ox'next'packet + 1;
! 124: )
! 125:
! 126: OXserver = () -> (
! 127: -- initialize stack machine
! 128: stderr << "entering OXserver" << endl << flush;
! 129: ox'init'sm();
! 130: ox'next'packet = 72;
! 131: -- we use stdin, stdout as our two data connections
! 132: -- handshake the network byte input and output
! 133: << "\000\000" << flush;
! 134: x := read stdio; -- throw out the response.
! 135: OX = makeOXINPUT stdio;
! 136: stderr << "About to enter loop" << endl << flush;
! 137: -- enter the read/eval/write loop
! 138: while (true) do (
! 139: --readMoreData OX;
! 140: --if atEndOfFile(stdio) then (
! 141: -- stderr << "bye bye" << endl << flush;
! 142: -- return;);
! 143: typ := get32bits OX;
! 144: packetNum := get32bits OX;
! 145: if typ === OX'COMMAND then (
! 146: cmd := get32bits OX;
! 147: stderr << "Command = " << cmd << endl << flush;
! 148: val := OX'commands#cmd(packetNum);
! 149: stderr << " Result = " << val << endl << flush;
! 150: if val =!= null then (
! 151: -- val is a CMO object
! 152: ox'send'data();
! 153: CMOSerialize(OX.F, val);
! 154: OX.F << flush;
! 155: );
! 156: )
! 157: else if typ === OX'DATA then (
! 158: v := CMOfromBinary OX;
! 159: stderr << "value = " << v << endl << flush;
! 160: ox'push(v)
! 161: )
! 162: else ox'error(packetNum, "expected OX_COMMAND or OX_DATA");
! 163: ox'view()
! 164: )
! 165: )
! 166:
! 167: ///
! 168: restart
! 169: load "oxcommon.m2"
! 170: load "oxserver.m2"
! 171: ox'init'sm()
! 172: ox'push 3
! 173: ox'push 5
! 174: ox'push INT32 2
! 175: ox'push "binomial"
! 176: ox'view()
! 177: OX'commands#SM'executeFunction 32
! 178: ox'view()
! 179: ox'push INT32 12
! 180: OX'commands#SM'pops 33
! 181: ox'view()
! 182: ox'push "asdf = 1347"
! 183: OX'commands#SM'executeStringByLocalParser 34
! 184: ox'view()
! 185: ox'push "2*asdf"
! 186: OX'commands#SM'executeStringByLocalParser 34
! 187: ox'view()
! 188: ///
! 189:
! 190: OXserver()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>