How best to persist ftp connection handle
Hi folks.
I'm testing using the plugin to create and manage an ftp connection to a barcode printer. My goal is to open an ftp connection, put multiple files to an ftp printer queue, and close down the connection.
If I use the keepMemory param to PHP_Execute, my ftp connection handle will persist across multiple calls to the plugin, and I can separate opening and closing the connection, letting me call the plugin to manipulate files and upload them at my leisure.
Is there a better way than using keepMemory to make the connection handle persist? I suspect, but haven't tested, that using keepMemory once at the beginning session means that all intervening calls to the plugin need to set keepMemory as well, until I've closed the ftp connection.
One option is to manage the keepMemory state by setting a global flag for it, shoving the PHP_Execute into a custom function, and funneling all calls through it so keepMemory is explicitly managed by my ftp script.
Is there a more elegant approach?
Thanks!
rick
How best to persist ftp connection handle
Hello Rick,
I think you're on the right track and I like the custom function approach to give you a level of abstraction where you can control keepMemory. I suppose you may not need a global field if memory is always to be kept?
You could also use a FileMaker global variable and SmartPill version 1.5 now supports global variables in the plug-in as well (which provides an application wide variable).
We've talked about adding a function that allows for named threads which would allow you to create an instance of PHP just for FTP and leave it alone for just that purpose. I'm afraid this is lower on the list for now but let me know what you think.
Regards,
Micah
named threads
> We've talked about adding a function that allows for named
> threads which would allow you to create an instance of PHP
> just for FTP and leave it alone for just that purpose. I'm afraid
> this is lower on the list for now but let me know what you think.
Micah,
Having named threads would certainly help with this kind of FTP management, but I suspect it's overkill for most of the FTP situations we'll encounter. I can run multiple printers by keeping the connection file handles for each connection around while I need them, and using unique identifiers for each connection handle. Now, if I could run them in the background while doing other tasks in fm, that makes threads more interesting, as some of the things we're loading to our printer take a while to load.
rick
more on ftp
Thanks, Micah.
I do have to manage keepMemory, as I'm isolating repeated code in PHP functions, and since runkit isn't included in the plugin (possibly for good reasons!) I need to dump the run-time memory whenever I load functions, to avoid re-defining them.
I haven't used the plug-in's global variable capability, simply because I haven't explored how they differ from vanilla fm $$ globals, yet. (I did notice I don't seem to be able to do fm_evaluate('$$glob = value'), though.)
Here's the current state of the our ftp code:
Set Variable $$phpkeepMemory = True
Perform Script "Reset PHP Memory" (see note below)
// script "Check for Printer" calls a PHP routine to ping the printer IP addr, sets a global read by all FTP
// routines to avoid trying to connect if there is no printer alive on the net
Perform Script "Check for Printer"
Set Variable $$plug_in_result ExecPHP (GetPHPCode("includes")
Set Variable $$plug_in_result ExecPHP (GetPHPCode("load_dependent_printer_files")
... do some misc set-up work
Set Variable $$plug_in_result ExecPHP (GetPHPCode("open_printer_connection")
Loop to print individual labels
... create individual label record ...
Set Variable $$plug_in_result ExecPHP (GetPHPCode("write_printer_file")
Set Variable $$plug_in_result ExecPHP (GetPHPCode("upload_print_file")
Set Variable $$plug_in_result ExecPHP (GetPHPCode("delete_print_file")
End Loop
Set Variable $$plug_in_result ExecPHP (GetPHPCode("close_printer_connection")
Perform Script "Reset PHP Memory"
Set Variable $$phpkeepMemory = True
//
Notes...
ExecPHP is a custom function to call the plug-in, passing the supplied code and the value
of $$phpKeepmemory.
GetPHPCode is a simple custom function using fm_sql_select to grab a chunk of PHP from the db:
Let (
$$php_code = "echo fm_sql_select(\"SELECT code FROM php_code WHERE name = '" & code_name & "'\");"
;
ExecPHP( $$php_code )
)
Script "Reset PHP Memory" does a PHP echo, with the side effect of
clearing the run-time memory, without losing the value of our $$phpKeepMemory flag
If ($$phpKeepMemory = 1)
Set Variable $$phpKeepMemory = 0
Set Variable $$plug_in_result ExecPHP (echo \"PHP memory cleared.\n\";")
Set Variable $$phpKeepMemory = 0
Else
Set Variable $$plug_in_result ExecPHP (echo \"PHP memory cleared.\n\";")
End if