[BACK]Return to oxclient.m2 CVS log [TXT][DIR] Up to [local] / OpenXM / src / Macaulay2 / m2

Annotation of OpenXM/src/Macaulay2/m2/oxclient.m2, Revision 1.6

1.6     ! takayama    1: ID=" $OpenXM: OpenXM/src/Macaulay2/m2/oxclient.m2,v 1.5 2006/10/27 01:38:17 takayama Exp $ "
1.1       takayama    2:
1.6     ! takayama    3: -- This is configured for Macaulay2 0.9.95
1.1       takayama    4: load "oxcommon.m2"
                      5:
                      6: oxSession = method()
                      7: oxSession(String) := (s) -> (
                      8:      try (F := oxStartSession(s))
                      9:      else ( error "Could not start the server " | s);
                     10:      F
                     11:      )
                     12:
                     13: oxFlush = method()
                     14: oxFlush(OXSESSION, File, String) := (F, file, str) -> (
                     15:      if (F.FlushFlag == 0) then file << str << flush
                     16:      else (
                     17:          read "Press Return to flush: ";
                     18:          file << str << flush;
                     19:          );
                     20:      )
                     21:
                     22: oxToggleFlushFlag = method()
                     23: oxToggleFlushFlag(OXSESSION, ZZ) := (F, n) -> (
                     24:      F.FlushFlag = n;
                     25:      )
                     26:
                     27: oxEmptyHashTable = method()
                     28: oxEmptyHashTable(OXSESSION) := (F) -> (
                     29:      apply(keys F, i -> remove(F, i));
                     30:      )
                     31:
                     32: oxasir = () -> ( oxStartSession "ox_asir" )
                     33:
                     34: -- this method starts the server s.
                     35: oxStartSession = (s) -> (
                     36:      MINPORT := 1025;
                     37:      MAXPORT := 5000;
                     38:      MAXTRIES := 3; -- number of times to try to open new ports
                     39:      MAXTRIES = 1000; -- number of times to try to open new ports
                     40:      PASS := "M2";
                     41:
                     42:      F := null;
                     43:      G := null;
                     44:      i := 0;
                     45:      while (i < MAXTRIES and (F === null or G === null)) do (
                     46:                  if not (F === null) then (close F; F = null);
                     47:                  if not (F === null) then (close G; G = null);
                     48:          cPort := random (MAXPORT - MINPORT) + MINPORT;
                     49:          dPort := random (MAXPORT - MINPORT) + MINPORT;
                     50:          if (cPort != dPort) then (
                     51:               try F = openListener(toString "$:"|cPort) else F = null;
                     52:               try G = openListener(toString "$:"|dPort) else G = null;
                     53:               );
                     54:            i = i+1;
                     55:          );
                     56:      << "F = " << F << " and " << " G = " << G << endl << flush;
                     57:      if (F === null or G === null) then
                     58:          error "Couldn't connect to the ox server" | s;
                     59:      comm = toString "xterm -geometry 80x25-0-0 -e  ox -ox " | s | " -data " |
                     60:                       dPort | " -control " | cPort |
                     61:                      " -reverse -host localhost" | " -pass " | PASS | " &" ;
1.6     ! takayama   62:      comm = makeLaunchCommand(false,"localhost",getenv("USER"),"localhost",
1.1       takayama   63:                              s,dPort,cPort,PASS);
1.6     ! takayama   64:      -- true : connection by ssh, false: only for the local host.
1.1       takayama   65:      print comm;
                     66:      if (run (comm) != 0)
                     67:        then error "Couldn't start the ox server " | s;
                     68:
                     69:      F = openInOut F;
                     70:      G = openInOut G;
                     71:      F << "\000" << flush ; -- set to network byte order
                     72:      G << "\000" << flush;
                     73:
                     74:      FP = makeOXINPUT F;
                     75:      S := getBytes(FP,#PASS);
                     76:      if S != PASS then (
                     77:          close F;
                     78:          close G;
                     79:          error "wrong one time password";
                     80:          );
                     81:      GP = makeOXINPUT G;
                     82:      S = getBytes(GP,#PASS);
                     83:      if S != PASS then (
                     84:          close F;
                     85:          close G;
                     86:          error "wrong one time password";
                     87:          );
                     88:      getBytes(FP,2);
                     89:      getBytes(GP,2);
                     90:
                     91:      -- need to change the following.
                     92:      new OXSESSION from {
                     93:          ServerName => s,
                     94:          ControlPort => F,
                     95:          DataPort => G,
                     96:          SerialNumber => 0,
                     97:          FlushFlag => 0,
                     98:          DataBuffer => makeOXINPUT G,
                     99:          Buffer => "",
                    100:          BufferPointer => 0}
                    101:      )
                    102:
                    103: -- methods to access data fields of the OXSESSION class
                    104: oxGetDataPort = method()
                    105: oxGetDataPort(OXSESSION) := (F) -> (F.DataPort)
                    106: oxGetControlPort = method()
                    107: oxGetControlPort(OXSESSION) := (F) -> (F.ControlPort)
                    108: oxGetSerialNumber = method()
                    109: oxGetSerialNumber(OXSESSION) := (F) -> (F.SerialNumber)
                    110: oxGetFlushFlag = method()
                    111: oxGetFlushFlag(OXSESSION) := (F) -> (F.FlushFlag)
                    112:
                    113: incrementSerialNumber = method()
                    114: incrementSerialNumber(OXSESSION) := (F) -> (
                    115:      F.SerialNumber = F.SerialNumber + 1;)
                    116:
                    117: oxPushCMO = (F,s) -> (
                    118:      incrementSerialNumber(F);
                    119:      Fport := oxGetDataPort(F);
                    120:      Fport << OXDATA
                    121:            << toNetwork(oxGetSerialNumber(F));
                    122:      CMOSerialize(Fport,s);
                    123:      oxFlush(F, Fport, "");
                    124:      )
                    125:
                    126: oxData = method()
                    127: oxData(OXSESSION, String) :=
                    128: oxData(OXSESSION, ZZ) :=
                    129: oxData(OXSESSION, INT32) :=
                    130: oxData(OXSESSION, List) := oxPushCMO
                    131:
                    132: -- method to set mathcap of mine to the server.
                    133: oxSetMathcap = method()
                    134: oxSetMathcap(OXSESSION) := (F) -> (
                    135:      incrementSerialNumber(F);
                    136:      Fport := oxGetDataPort(F);
                    137:      Fport << OXDATA << toNetwork(oxGetSerialNumber(F))
                    138:            << toNetwork(CMO'MATHCAP);
                    139:      CMOSerialize(Fport,MATHCAP'OF'M2);
                    140:      oxFlush(F, oxGetDataPort(F), "");
                    141:      oxCommand(F,SM'setMathCap);
                    142:      )
                    143:
                    144: -- method to send a command to the data port
                    145: oxCommand = method()
                    146: oxCommand(OXSESSION, ZZ) := (F, TagNumber) -> (
                    147:      incrementSerialNumber(F);
                    148:      str := OXCOMMAND | toNetwork(oxGetSerialNumber(F)) | toNetwork(TagNumber);
                    149:      oxFlush(F, oxGetDataPort(F), str);
                    150:      )
                    151:
                    152: -- method to send a command to the control port
                    153: oxControl = method()
                    154: oxControl(OXSESSION, ZZ) := (F, TagNumber) -> (
                    155:      incrementSerialNumber(F);
                    156:      str := OXCOMMAND | toNetwork(oxGetSerialNumber(F)) | toNetwork(TagNumber);
                    157:      oxFlush(F, oxGetControlPort(F), str);
                    158:      )
                    159:
                    160: -- various commands
                    161: oxExecuteStringByLocalParser = method()
                    162: oxExecuteStringByLocalParser(OXSESSION) := (F) -> (
                    163:      oxCommand(F, SM'executeStringByLocalParser);
                    164:      )
                    165:
                    166: oxExecuteFunction = method()
                    167: oxExecuteFunction(OXSESSION) := (F) -> (
                    168:      oxCommand(F, SM'executeFunction);
                    169:      )
                    170:
                    171: oxPopCMO = method()
                    172: oxPopCMO(OXSESSION) := (F) -> (
                    173:      oxCommand(F, SM'popCMO);
                    174:      oxTag := get32bits(F.DataBuffer); -- read the OX tag
                    175:      serTag := get32bits(F.DataBuffer); -- read the Serial tag
                    176:      CMOfromBinary(F.DataBuffer)
                    177:      );
                    178:
                    179: oxPopString = method()
                    180: oxPopString(OXSESSION) := (F) -> (
                    181:      oxCommand(F, SM'popString);
                    182:      oxTag := get32bits(F.DataBuffer); -- read the OX tag
                    183:      serTag := get32bits(F.DataBuffer); -- read the Serial tag
                    184:      CMOfromBinary(F.DataBuffer)
                    185:      )
                    186:
                    187:
                    188: oxKill = method()
                    189: oxKill(OXSESSION) := (F) -> (
                    190:      oxControl(F, SM'control'kill);
                    191:      oxEmptyHashTable(F);
                    192:      )
                    193:
                    194: oxRestart = method()
                    195: oxRestart(OXSESSION) := (F) -> (
                    196:      oxControl(F, SM'control'kill);
                    197:      tmp := oxStartSession(F.ServerName);
                    198:      scan(keys tmp, i -> (F#i = tmp#i));
                    199:      F.DataBuffer = makeOXINPUT (F.DataPort);
                    200:      F
                    201:      )
                    202:
                    203: oxPopMathcap = method()
                    204: oxPopMathcap(OXSESSION) := (F) ->(
                    205:         oxCommand(F,SM'mathcap);
                    206:         oxPopCMO(F)
                    207:         )
                    208:
                    209: oxPrimDecomp = method()
                    210: oxPrimDecomp(OXSESSION, Ideal) := (F, I) -> (
                    211:      -- translate to asir notation
1.6     ! takayama  212:      s := toString flatten entries gens I;
        !           213:      s = replace("\\{","[",s);   -- substring(2,5,s)
        !           214:      s = replace ("\\}","]",s);
1.1       takayama  215:      v = toString gens ring I;
1.6     ! takayama  216:      v = replace("\\{","[",v);
        !           217:      v = replace("\\}","]",v);
1.1       takayama  218:      s = "primadec(" | s | "," | v | ");";
                    219:      << "sending" << s << endl;
                    220:      oxData(F, s);
                    221:      oxExecuteStringByLocalParser(F);
                    222:      str = oxPopString(F);
1.6     ! takayama  223:      -- BUG: oxPopCMO(F) is still buggy.
1.1       takayama  224:      toList apply(value str, i -> {ideal(toList(i#0)), ideal(toList(i#1))})
                    225:      )
                    226:
                    227: -- makeLaunchCommand generates an argument of run.
                    228: -- Replace comm = toString "xterm ...." in oxStartSession (oxcommon.m2) by
                    229: --  comm = makeLauchCommand with proper arguments.
                    230: -- I think that the examples below speak itself.
                    231: -- Some arguments are not necessary when UseSSH is true.
                    232: -- there may be a neat way to define optional arguments in M2.
                    233:
                    234: makeLaunchCommand=(UseSSH,MyHostName,RemoteLoginName,RemoteHostName,OxServerName,dPort,cPort,PASS)->(
                    235:      commXterm := toString " xterm -geometry 80x25-0-0 " ;
1.3       takayama  236:      commOX :=toString " -e ox -ox " |
                    237:                getenv("OpenXM_HOME") | "/bin/" |
                    238:                OxServerName |
1.1       takayama  239:                        " -data " | dPort |
                    240:                       " -control " | cPort |
                    241:                       " -reverse " |
                    242:                       " -host " | RemoteHostName |
                    243:                       " -pass " | PASS   | "  ";
                    244:      if (UseSSH) then (
1.4       takayama  245:          comm = toString "ssh -f -X -A -l " | RemoteLoginName | " " |
1.1       takayama  246:                           RemoteHostName |
                    247:                           " '" | commXterm |
                    248:                           --uncomment when X11 forwarding is not allowed.
1.3       takayama  249:                           -- " -display " | MyHostName | ":0 " |
1.1       takayama  250:                           commOX | "'  >/dev/null ";
                    251:          )
                    252:      else  (
                    253:          comm = toString commXterm | commOX | " &";
                    254:          );
                    255:      comm
                    256:      )
                    257:
                    258: ///
                    259: print makeLaunchCommand(false,"localhost","grobner","localhost","ox_asir",1300,1200,"M2")
                    260: print " "
                    261: print makeLaunchCommand(true,"localhost","grobner","localhost","ox_asir",1300,1200,"M2")
                    262: print " "
                    263: print makeLaunchCommand(true,"mymachine.edu","graver","oxservers.com","ox_m2",1300,1200,"M2")
                    264: print " "
                    265: print makeLaunchCommand(true,"hogehoge.com","goober","servers.openxm.org","ox_sm1",1300,1200,"M2");
                    266: print " "
                    267: ///

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>