Guidance
指路人
g.yi.org
Software / Reginald / Examples / VarServer / VarServer.rex

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
/*
GUIBEGIN

WINDOW , 30, 0, 400, 200, POPUP|CAPTION|SYSMENU|MINBOX|MAXBOX|THICK, , Variable Server
	FONT 8, 400, MS Shell Dlg
	PUSH 344, 4, 53, 14, TABSTOP, , ShutDown, , ShutDown
	PUSH 286, 4, 53, 14, TABSTOP, , save, , Save
	PUSH 170, 4, 53, 14, TABSTOP, , Cancel, , Cancel
DEND
GUIEND
*/

/* ============================================================================================ */
idlemax=900             /* Maximum time in seconds Server can be idle before automatic shutdown */ 
capturetime=180         /* Time in Seconds in which changed entries are physically stored       */ 
/* ============================================================================================ */

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
servertype='normal'
/*****$$ADD DDEXEC   */  /* ADD DDCALL SOURCES AT THE END (ONLY FOR DICTIONARY REQURED       */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */   

servermode='Variable Server'               
IF lock('EXCLUSIVE',servermode)>0 THEN DO
   SAY 'Server from location 'DIRECTORY()' already active, start aborted'
   EXIT 8
END
lock('EXCLUSIVE',servermode'Start') 
LIBRARY rexxgui
LIBRARY rxclip
LIBRARY rxconsole

guierr = "SYNTAX"
guiheading = 1
guicreatewindow('NORMAL')
ERROR = clipclear()
windowhandle = guiinfo("HANDLE", guiwindow)
nextwindow = clipinform(windowhandle)

again:
DO FOREVER
	guigetmsg(,40000) 
	consay(,guiobject guisignal)
	IF EXISTS('GuiObject') == 0 THEN DO
		IF EXISTS('GuiSignal') THEN DO
		END
	END
	ELSE DO
 		IF EXISTS('GuiSignal') == 0 THEN DROP (guiobject)
		ELSE SELECT guiobject
			WHEN 0 THEN NOP
			OTHERWISE
		END /* SELECT GuiObject */

	END /* Some child script signaled us */
     CATCH NOVALUE
         SAY 'Novalue Error occured'
         CALL sysready
         SIGNAL again
      CATCH SYNTAX
         SAY 'Syntax Error occured'
         CALL sysready
         SIGNAL again 
	CATCH HALT
	FINALLY
	guidestroywindow()
END
RETURN
/* ----------------------------------------------------------------
 * Insert Console Window  
 * ----------------------------------------------------------------
 */
wm_initdialog:
guigetctlplacement(guiwindow, , , "width", "height")
guigetctlplacement('ShutDown', "SayX", "SayY", "SayWidth", "SayHeight")
sayy = sayy+sayheight+5
height = height - sayy
concreate(, guiwindow, "CHILD", , sayy, width, height)
openfile=''
testmpin=0
mypid=GETPID()
consay(,ptime()' Variable Server Started ')
consay(,ptime()' Server PID is: 'mypid)
consay(,ptime()' Window handle: 'guiinfo("HANDLE", guiwindow))
capture=capturetime*1000
ERROR = guiaddctl("TIMER "capture)  /* set Timer to x minutes for capture of data */ 
CALL varserverini()   /* load globals */
CALL divin() 
consay(,ptime()' Variable Pool loaded')
dictin =clipnewformat('MPIN'DIRECTORY())
testmpin=1
keptdata.0=0
CALL sysini
consay(,ptime()' Server waiting for requests')
lock('RELEASE',servermode'Start') 
RETURN ""
/* ----------------------------------------------------------------
 * Called whenever some data is copied to the clipboard 
 * ----------------------------------------------------------------
 */
wm_drawclipboard:
DO
/* Is this someone else's data? */
testmpin=testgetvar('TestMpin',0) 
IF testmpin \== 0 THEN DO
/* Do we have some data in our custom format? */
   IF clipavailable(dictin) THEN CALL getclip
END
testmpin=1
clipinformend(nextwindow)
END
RETURN ""
/* --------------------------------------------------------------------------------------------
 * Get Clipboard
 * --------------------------------------------------------------------------------------------
 */
getclip:
starttime=TIME('R')
processstarttime=ptime()
savekept=0
clipget("MyVar.", dictin)
mprout=WORD(myvar.1,1)     /* set OPTIONS.parms */ 
INTERPRET myvar.2
CALL readparm()
consay(,COPIES('-',25))
consay(,ptime()' Received MPIN for 'mprout)  
testmpin=0
CALL sysbusy()
IF servertype='DICTIONARY' THEN CALL ddcall()   /* call dictionary Variable Server */
   ELSE CALL varrun()                           /* normal Variable Server Mode */
execendtime=ptime()
CALL mprout(TIME('E'),myvar.3)  /* pass elapsed time, as time('E') doesn't work across calls */
/* clean up Option and Command Control variables */
DROP option.
DROP cmd.
IF save=0 THEN consay(,ptime()' MPIN Processed, no update flag set')  
   ELSE consay(,ptime()' MPIN Processed, variables updated')  
resettime=TIME('R')
CALL sysready()
RETURN 
/* --------------------------------------------------------------------------------------------
 * Run Command in normal Variable Server  
 * --------------------------------------------------------------------------------------------
 */
varrun:
SAY ptime() 'Requested call: 'myvar.3'('p.1','p.2','p.3','p.4','p.5')'
DROP mpout.
mpout.0=0
DO
   CALL [myvar.3](p.1,p.2,p.3,p.4,p.5)
   CATCH NOVALUE
      CALL ERROR('Novalue Error occured')
   CATCH SYNTAX
       CALL ERROR('Syntax Error occured')
   CATCH ERROR
       CALL ERROR('Error condition occured')
   CATCH FAILURE
       CALL ERROR('Failure condition occured')  
END
mpmax=mpout.0+1
mpout.mpmax=mprout
mpout.0=mpmax
save=0
DO i=1 TO dict$.variable_name.0
/* save must be performed if Update flag has been set, or not available */ 
   IF testgetvar(dict$.variable_name.i'.UPDATE',0) \= 0 THEN save=1
END
RETURN 
/* ----------------------------------------------------------------
 * Handle call "command" errors   
 * ----------------------------------------------------------------
 */
error:
PARSE ARG errmsg 
mpout.2=myvar.3'('myvar.4')'
mpout.3='   'errmsg' in Procedure 'myvar.3
mpout.4=myvar.3' Command aborted'
mpout.0=4
consay(,ptime() errmsg 'in' myvar.3'('myvar.4')')
RETURN 
/* ----------------------------------------------------------------
 * Capture process to Save and maintain or stop server   
 * ----------------------------------------------------------------
 */
wm_timer:
CALL wm_click_save
idle=stime()-idletime
IF idle>300 THEN SAY 'Server idle for 'idle' Seconds'
IF idle>idlemax THEN CALL wm_close
RETURN ''    /* return '' to keep TIMER */ 
/* ----------------------------------------------------------------
 * Save Variable Server Files if necessary   
 * ----------------------------------------------------------------
 */
wm_click_save:
CALL sysheart      /* set heart beat to show server is alive (also set at timer) */ 
save=0
DO i=1 TO dict$.variable_name.0
/* save must be performed if Update flag has been set, or not available */ 
   IF testgetvar(dict$.variable_name.i'.UPDATE',0) \= 0 THEN save=1
END
IF save=0 THEN RETURN  
consay(,COPIES('-',25))
CALL divout()
consay(,ptime()' Variable Pool changes captured')
dive$.update=0
RETURN
/* ----------------------------------------------------------------
 * Close Variable Server  
 * ----------------------------------------------------------------
 */
wm_click_shutdown:
wm_close:
wm_click_cancel:
CALL wm_click_save
CALL sysclose 
lock('RELEASE',servermode)  
guidestroywindow()
RETURN
/* ----------------------------------------------------------------
 * Handle 
 * ----------------------------------------------------------------
 */
wm_extra:
p0=ARG(1)
p1=ARG(2)
event=ARG(3)
IF event=2710 THEN RETURN 2710   /* immediately follows a CLIPSET of client, if actioned 
                                  * we know server has processed it successfully, we trigger 
                                  * client to continue (it waited for completion 
                                  */   
/*
   else SAY 'Received unknown event: 'event' with ARG(1)='p0' and ARG(2) ='p1 */
/* Don't let Rexx Gui process this event. */
RETURN ""
/* --------------------------------------------------------------------------------------------
 * Init and maintain SysChannel  **** Doesn't seem to work  **** 
 * --------------------------------------------------------------------------------------------
 */
sysini:
tcount=0
execendtime=ptime()
pid=mypid                 /* Server's Pid Address  */
mpin=dictin               /* input Channel */
start=ptime()             /* Server Start time */
status='READY'            /* Server Status */
incommands=tcount         /* count of input commands */
ideltime=stime()          /* Idle since  */
sysheart:                 /* set heart beat at start up and timer event */ 
heartbeat=stime()         /* set by Timer to show Server is alive  */
RETURN 
sysbusy:
tcount=tcount+1
status='BUSY'             /* Server Status */
incommands=tcount         /* count of input commands */
processtime=processstarttime  /* Time last input received */
lastcommand=ptime()       /* Time last command started */
idletime=stime()          /* relative time when server was last busy */
RETURN 
sysready:
status='READY'            /* Server Status */
processend=execendtime    /* Time last command ended */
lastcommand=ptime()       /* Time server was last busy */
idletime=stime()          /* relative time when server was last busy */
RETURN
sysclose:
heartbeat=stime()         /* set by Timer to show Server is alive  */
status='SHUT-DOWN'        /* Server Status */
testmpin=0
RETURN
/* --------------------------------------------------------------------------------------------
 * Read Received Parameters and Command(s) 
 * --------------------------------------------------------------------------------------------
 */
readparm:
pmax=WORD(myvar.1,2)
IF pmax='' |datatype(pmax) \= 'NUM' THEN pmax=0
DROP p.
svar=''
DO i=1 TO 5
   p.i=''
END
DO i=4 TO pmax
   j=i-3 
   INTERPRET 'p.'j'=getvar(myVar.,i)'
   svar=svar||p.j' '
END
svar='('STRIP(svar)')'
RETURN
/* --------------------------------------------------------------------------------------------
 * Save Output in Clipboard
 * --------------------------------------------------------------------------------------------
 */
mprout:
PARSE ARG elapsed,proc 
mpmax=mpout.0
option.file=testgetvar('option.file') 
option.fileonly=testgetvar('option.fileonly') 
IF option.fileonly \='' THEN option.file=''
IF option.file \='' | option.fileonly \= '' THEN DO
   mpout.mpmax=''
   LOADTEXT('MPOUT.',option.file,'BS')
END  
IF option.fileonly \= '' THEN DO   /* output to client */
   mpout.1='Output redirected to 'option.fileonly
   mpout.0=1
END
dictout=clipnewformat(mprout)
mpmax=mpmax+1
mpout.mpmax=mprout' 'mpout.0' 'elapsed /* Variable received from CLIP later may contain old lines, save in CB */
mpout.0=mpmax
clipset('MPOUT.',dictout)
SAY ptime() mpout.0 'Lines written to MPOUT by procedure 'proc'()' 
RETURN
/* --------------------------------------------------------------------------------------------
 * Get Variable (test if exists)
 * --------------------------------------------------------------------------------------------
 */
getvar:
USE ARG testvar.,ind
IF EXISTS('testvar.'ind)=0 THEN RETURN ''
ELSE RETURN STRIP(testvar.ind)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/*** --------------------------------------------------------------------------- */
/*** Auto Loaded Modules, source changes should be done in the original modules  */
/*** --------------------------------------------------------------------------- */  /*$$AUTOLOAD ALL */
lock: PROCEDURE
PARSE ARG mode,resource
lockfile=DIRECTORY()'\'resource'.lck'
IF ABBREV('EXCLUSIVE',mode)=1 THEN DO 
   IF STREAM(lockfile, "C", "OPEN WRITE REPLACE")='READY:' THEN RETURN 0
   ELSE RETURN 8
END
IF ABBREV('TESTEXCLUSIVE',mode)=1 THEN DO 
   IF STREAM(lockfile, "C", "OPEN WRITE REPLACE")='READY:' THEN DO
      STREAM(lockfile, "C", "CLOSE")
      DELETEFILE(lockfile)
      RETURN 0
   END
   ELSE RETURN 8
END
IF ABBREV('RELEASE',mode)=1 THEN DO 
      STREAM(lockfile, "C", "CLOSE")
      DELETEFILE(lockfile)
      RETURN 0
END
testgetvar:
USE ARG tvar,default
default=ARG(2)
IF EXISTS(tvar)=0 THEN DO 
   tvar=TRANSLATE(tvar)
   IF EXISTS(tvar)=0 THEN RETURN default
END
IF STRIP(VALUE(tvar))='' THEN RETURN default
RETURN VALUE(tvar)
ptime: PROCEDURE
ptime=TIME('l')
len=LENGTH(ptime)-3
RETURN SUBSTR(ptime,1,len)
stime: PROCEDURE
PARSE ARG itime
IF itime='' THEN PARSE VALUE TIME('L') WITH hh ':' mm ':' ss '.' tt
   ELSE PARSE VALUE itime WITH hh ':' mm ':' ss '.' tt
stime=hh*3600+mm*60+ss
stime=stime||'.'||tt
RETURN stime                    /*$$AUTOLOAD END ALL */
/*** ------------------------------------------------------------------------------------------------------- */
/*** XREF of (Auto-) Loaded Modules                                                                          */
/*** ------------------------------------------------------------------------------------------------------- */
/*** LOCK.REX                I:\MPSF\SysProc\                       AUTOLOAD         1KB 06/01/2009 08:01:16 */
/*** TESTGETVAR.REX          I:\MPSF\SysProc\                       AUTOLOAD       448BT 03/01/2009 13:01:08 */
/*** PTIME.REX               I:\MPSF\SysProc\                       AUTOLOAD       281BT 23/12/2008 15:12:04 */
/*** STIME.REX               I:\MPSF\SysProc\                       AUTOLOAD       407BT 29/12/2008 12:12:00 */
/*** ------------------------------------------------------------------------------------------------------- */
/*** XREF of Procedures      Defined  Used in Line   Suffix "+" Label, none Procedure                        */
/*** ------------------------------------------------------------------------------------------------------- */
/***   1 AGAIN                  40+                                                                          */
/***   2 ERROR                 174+   153 155 157 159                                                        */
/***   3 GETCLIP               118+                                                                          */
/***   4 GETVAR                320+   105 167 201 286 298 299                                                */
/***   5 LOCK                  347    24 28 95 218                                                           */
/***   6 MPROUT                295+   133                                                                    */
/***   7 PTIME                 376    82 83 84 89 94 120 127 132 137 138                                     */
/***                                  147 180 206 243 246 258 264 314                                        */
/***   8 READPARM              276+   125                                                                    */
/***   9 STIME                 380    188 249 251 259 265 268                                                */
/***  10 SYSBUSY               253+   129                                                                    */
/***  11 SYSCLOSE              267+                                                                          */
/***  12 SYSHEART              250+                                                                          */
/***  13 SYSINI                241+                                                                          */
/***  14 SYSREADY              261+   140                                                                    */
/***  15 TESTGETVAR            367+   105 167 201 298 299                                                    */
/***  16 VARRUN                146+   131                                                                    */
/***  17 WM_CLICK_CANCEL       215+                                                                          */
/***  18 WM_CLICK_SAVE         196+                                                                          */
/***  19 WM_CLICK_SHUTDOWN     213+                                                                          */
/***  20 WM_CLOSE              214+                                                                          */
/***  21 WM_DRAWCLIPBOARD      102+                                                                          */
/***  22 WM_EXTRA              225+                                                                          */
/***  23 WM_INITDIALOG          73+                                                                          */
/***  24 WM_TIMER              186+                                                                          */
/*** ------------------------------------------------------------------------------------------------------- */
/*** XREF of Variables              Modified in Line   Suffix "+" Parse Arg, "*" Parse Variable, none SET    */
/*** ------------------------------------------------------------------------------------------------------- */
/***   1 CAPTURE                             85                                                              */
/***   2 CAPTURETIME                         15                                                              */
/***   3 DEFAULT                             369                                                             */
/***   4 DICTIN                              90                                                              */
/***   5 DICTOUT                             309                                                             */
/***   6 DIVE$.UPDATE                        207                                                             */
/***   7 ELAPSED                             296+                                                            */
/***   8 ERRMSG                              175+                                                            */
/***   9 ERROR                               36 86                                                           */
/***  10 EVENT                               228                                                             */
/***  11 EXECENDTIME                         132 243                                                         */
/***  12 GUIERR                              33                                                              */
/***  13 GUIHEADING                          34                                                              */
/***  14 HEARTBEAT                           251 268                                                         */
/***  15 HEIGHT                              77                                                              */
/***  16 IDELTIME                            249                                                             */
/***  17 IDLE                                188                                                             */
/***  18 IDLEMAX                             14                                                              */
/***  19 IDLETIME                            259 265                                                         */
/***  20 INCOMMANDS                          248 256                                                         */
/***  21 ITIME                               381+                                                            */
/***  22 J                                   285                                                             */
/***  23 KEPTDATA.0                          92                                                              */
/***  24 LASTCOMMAND                         258 264                                                         */
/***  25 LEN                                 378                                                             */
/***  26 LOCKFILE                            349                                                             */
/***  27 MODE                                348+                                                            */
/***  28 MPIN                                245                                                             */
/***  29 MPMAX                               161 297 310                                                     */
/***  30 MPOUT.0                             149 163 179 307 312                                             */
/***  31 MPOUT.1                             306                                                             */
/***  32 MPOUT.2                             176                                                             */
/***  33 MPOUT.3                             177                                                             */
/***  34 MPOUT.4                             178                                                             */
/***  35 MPOUT.MPMAX                         162 302 311                                                     */
/***  36 MPROUT                              123                                                             */
/***  37 MYPID                               81                                                              */
/***  38 NEXTWINDOW                          38                                                              */
/***  39 OPENFILE                            79                                                              */
/***  40 OPTION.FILE                         298                                                             */
/***  41 OPTION.FILEONLY                     299                                                             */
/***  42 P.I                                 282                                                             */
/***  43 P0                                  226                                                             */
/***  44 P1                                  227                                                             */
/***  45 PID                                 244                                                             */
/***  46 PMAX                                277                                                             */
/***  47 PROCESSEND                          263                                                             */
/***  48 PROCESSSTARTTIME                    120                                                             */
/***  49 PROCESSTIME                         257                                                             */
/***  50 PTIME                               377                                                             */
/***  51 RESETTIME                           139                                                             */
/***  52 RESOURCE                            348+                                                            */
/***  53 SAVE                                164 198                                                         */
/***  54 SAVEKEPT                            121                                                             */
/***  55 SAYY                                76                                                              */
/***  56 SERVERMODE                          23                                                              */
/***  57 SERVERTYPE                          19                                                              */
/***  58 START                               246                                                             */
/***  59 STARTTIME                           119                                                             */
/***  60 STATUS                              247 255 262 269                                                 */
/***  61 STIME                               384 385                                                         */
/***  62 SVAR                                280 287 289                                                     */
/***  63 TCOUNT                              242 254                                                         */
/***  64 TESTMPIN                            80 91 105 110 128 270                                           */
/***  65 TVAR                                371                                                             */
/***  66 WINDOWHANDLE                        37                                                              */
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-4-25  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2011-06-15 11:46:11