rejetto forum

About static variables #var: two new type, $var and @var in the build 242?

Mars · 5 · 2414

Poll

Who is ..

for this addition?
2 (33.3%)
is against not to have it?
0 (0%)
does not want to vote against?
1 (16.7%)
has no opinion against?
0 (0%)
has to go to walk the dog?
2 (33.3%)
has to ask at first his wife?
1 (16.7%)
Send your wife to walk with the dog
0 (0%)

Total Members Voted: 2

0 Members and 1 Guest are viewing this topic.

Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2039
    • View Profile
As everyone knows, rejetto set up the static variables  {.set|#X|static text.}  {.^#X.}, which allow to keep data in memory. So far, we owed to preserve data from a session to the other one, either record them in a file, or to pass on them in the URL, or in the form of form.

The only inconvenience is that it is necessary to use these variables with a lot of caution, because a variable initialized during the session of a user ' X ' will be posted shown for the user 'Y' with the same value in the template.

The idea is to be able to use a static variable but which is seen by the user as a dynamic variable: its value is kept between two sessions, and two users will not obtain the same value for an identical variable name.

There are two possible forms: a partner in the login of the user, the other partner at her address IP. a use combined of the various variables will allow to manage effectively the data for such or such type of user.

  In touch with the names of domain in the e-mail addresses, I thought of associating characters @ with the IP (@domain.com) and the $ with the login (other unused caracters were & or ! )

Only some lines of codes are necessary to use these two types of variables. (I hope only that rejetto will be comprehensive and indulgent  ;) )

Quote
 MACROS_LOG_FILE = 'macros.log';
  G_VAR_PREFIX = '#';
 IP_VAR_PREFIX = '@';
  USER_VAR_PREFIX = '$';

  SECONDS = 24*60*60; // Tdatetime * SECONDS = time in seconds
......

  function getVarSpace(var varname:string):THashedStringList;   //marsmars
  begin
  varname:=trim(varname);
 if varname[1] in ['#','$','@'] then
    begin
    result:=staticVars;
    case varname[1] of
      '#': delete(varname,1,length(G_VAR_PREFIX));
      '$': varname:=md.cd.conn.request.user+varname;   // This syntax is necessary, to make no change, used to differentiate variables
      '@': varname:=md.cd.address+varname;               // This syntax is necessary, to make no change, used to differentiate variables
      end;
    end

    else if satisfied(md.cd) then
      result:=md.cd.vars
    else
      raise Exception.create('no namespace available');
  end; // getVarSpace


It is possible to realize various forms by using the little template following in a subfolder, by using some different IP and different login

Quote
<html>
{.inc|#hello.}
STATIC count:{.^#hello.}
<br>
{.inc|@hello.}
IP count:{.^@hello.}
<br>
{.inc|$hello.}
USER count:{.^$hello.}
</html>

I shall reveal small craftiness which allows to use the same principle with the current version of HFS:
It is possible to replace  '@hello' by '#%ip%@hello' and  '$hello' by '#%user%$hello'.
  After modification of the code, it is possible to use both formulations indifferently.
Quote
<html>
{.inc|#hello.}
STATIC count:{.^#hello.}
<br>
{.inc|#%ip%@hello.}
IP count:{.^#%ip%@hello.}
<br>
{.inc|#%user%$hello.}
USER count:{.^#%user%$hello.}
</html>

with @var and $var it is more shorter   ;).
« Last Edit: June 02, 2009, 08:53:40 AM by mars »


Offline bacter

  • Operator
  • Tireless poster
  • *****
    • Posts: 681
    • View Profile
The idea to have more than the #static vairables is good. But as hfs (and templates!) should be usable in different scenarios, i will try to show some possible shortcomings with ip OR user bound variables.

1. if your users are behind the same ip (college, company, university..) you have tu use $var-user ,as #var-ip would be the same for all users of this group.

2. If you make users login to get access with public names like guest,demo etc. , you should use #var-ip.

If i have understand what the goal of this new types of variables is, then i come to the conclusion that perhaps we could go a little further: instead of user OR ip depending variable, create a session-variable.

I know, there are no real sessions with hfs - but there is a workaround to create session-ids!

The idea how is the following:

1. we need that the program allows to define 2 things:
   a) type of session id:   none, user, ip or cookie
   b) only if cookie defined:  cookiename (default: hfs) + cookiemask (default: sid=)

the following steps are only needed if session-id type is cookie when %newsid% is set.

2. we need in the template a static variable #sid or %sid% , with an initial value.

3. hfs checks with every request the presence of the cookie-value. If there is no cookie that contains the sid, it sets %newsid% true and increments %sid%.

4. templates that use connection-dependent variables, update the cookie.

If a templater thinks using cookies is no good idea because some users fear that the cookies bite, they simply have not to use session- or connection depending vars, or define them as none,ip or user.

Also, for mars's initial proposal , as for what i propose to unify and make more universal the use of the new static vars, there will be the need for something more: some garbage collection, perhaps to do in events every xx hours/days, depending of your load on hfs.

This could be done in the following way for example: set with every request a #%sid%lastaccess=%now% (or use a table with addtable) and add %sid% to a #sidlist if it is not in it.

Then in events, in a for-loop check for every element the sid's lastaccess when the last access ocurred. When more than x time has passed, remove the sid form he lastaccess list or table and from #sidlist and invoka a {.clearsid|sid.} macro to free up the space.

This garbage collection shurely could be done in an easier way - this is no elaborated how to - but is not to forget if we don't want hfs to hang if we make no shutdown for days or weeks, as the amount of required storage space may grow heavy in some cases.

   



your computer has no brain - use your own !


Offline bacter

  • Operator
  • Tireless poster
  • *****
    • Posts: 681
    • View Profile
Appendix to the poll:

Send your wife to walk with the dog, you are too busy with this forum ;D
your computer has no brain - use your own !


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13309
    • View Profile
the basic idea is good! i guess we'll need something like this when we'll have sessions.
until that, i think the value of this is very low, and doesn't deserve to be integrated in the language syntax.
the ip method is very unreliable. the user method will fail with shared accounts (as bacter said) and without accounts (classic sessions do!).
the few times someone will need to do it, he may use the explicit syntax #%user%variable
few characters more than $variable
my 0.02


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2039
    • View Profile