logo       
Google Custom Search
    AddThis Social Bookmark Button

r9947 - trunk/libraries/koala/sources/examples/buddha: msg#00006

Subject: r9947 - trunk/libraries/koala/sources/examples/buddha
Author: hannes
Date: Fri Sep  2 20:30:18 2005
New Revision: 9947

Modified:
   trunk/libraries/koala/sources/examples/buddha/buddha.dylan
   trunk/libraries/koala/sources/examples/buddha/cidr.dylan
   trunk/libraries/koala/sources/examples/buddha/cisco-telnet.dylan
   trunk/libraries/koala/sources/examples/buddha/host.dylan
   trunk/libraries/koala/sources/examples/buddha/ipv4.dylan
   trunk/libraries/koala/sources/examples/buddha/library.dylan
   trunk/libraries/koala/sources/examples/buddha/subnet.dylan
   trunk/libraries/koala/sources/examples/buddha/zone.dylan
Log:
Bug: 7257
* use as(foo == type, object) => (object :: <type>)
  as conversion method (no ip-address-to-string,...)
* implement user-interface for save and restore database


Modified: trunk/libraries/koala/sources/examples/buddha/buddha.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/buddha.dylan  (original)
+++ trunk/libraries/koala/sources/examples/buddha/buddha.dylan  Fri Sep  2 
20:30:18 2005
@@ -23,6 +23,8 @@
 define page host end;
 define page zone end;
 define page user end;
+define page save end;
+define page restore end;
 
 define macro with-buddha-template
   { with-buddha-template(?stream:variable, ?title:expression)
@@ -41,6 +43,8 @@
                    gen-link(?stream, "/host", "Host");
                    gen-link(?stream, "/zone", "Zone");
                    gen-link(?stream, "/user", "User Interface");
+                   gen-link(?stream, "/save", "Save to disk");
+                   gen-link(?stream, "/restore", "Restore from disk");
                    gen-link(?stream, "/koala/shutdown", "Shutdown");
                  end;
                end;
@@ -50,6 +54,68 @@
          end; }
 end;
 
+define variable *directory* = "/home/hannes/dylan/libraries/koala/www/buddha/";
+
+define method respond-to-get
+    (page == #"save", request :: <request>, response :: <response>)
+  let out = output-stream(response);
+  with-buddha-template(out, "Save Database")
+    with-div (out, "id", "content")
+      with-form (out, "/save")
+        form-field(out, "filename");
+        submit-form-field(out, "save-button", "Save");
+      end;
+    end;
+  end;
+end;
+
+define method respond-to-post
+    (page == #"save", request :: <request>, response :: <response>)
+  let file = get-query-value("filename");
+  let dood = make(<dood>,
+                  locator: concatenate(*directory*, base64-encode(file)),
+                  direction: #"output",
+                  if-exists: #"replace");
+  dood-root(dood) := *config*;
+  dood-commit(dood);
+  dood-close(dood);
+  format(output-stream(response), "Saved database\n");
+  respond-to-get(#"save", request, response);
+end;
+
+define method respond-to-get
+    (page == #"restore", request :: <request>, response :: <response>)
+  let out = output-stream(response);
+  with-buddha-template(out, "Restore Database")
+    with-div (out, "id", "content")
+      with-form(out, "/restore")
+        with-select(out, "filename")
+          do-directory(method(directory :: <pathname>,
+                              name :: <string>,
+                              type :: <file-type>)
+                           if (type == #"file")
+                             gen-option(out, name, base64-decode(name));
+                           end if;
+                       end, *directory*);
+        end;
+        submit-form-field(out, "restore-button", "Restore");
+      end;
+    end;
+  end;
+end;
+
+define method respond-to-post
+    (page == #"restore", request :: <request>, response :: <response>)
+  let file = get-query-value("filename");
+  let dood = make(<dood>,
+                  locator: concatenate(*directory*, file),
+                  direction: #"input");
+  *config* := dood-root(dood);
+  dood-close(dood);
+  format(output-stream(response), "Restored database\n");
+  respond-to-get(#"restore", request, response);
+end;
+
 define method respond-to-get
     (page == #"net", request :: <request>, response :: <response>)
   let out = output-stream(response);
@@ -93,7 +159,7 @@
           submit-form-field(out,
                             "add-subnet-button",
                             concatenate("Add Subnet to ",
-                                        cidr-to-string(net.network-cidr)));
+                                        as(<string>, net.network-cidr)));
         end;
       end;
       with-form (out, "/net")
@@ -226,7 +292,6 @@
 
 define method do-action (action == #"gen-dhcpd", response :: <response>)
  => (show-get? :: <boolean>)
-  //generate dhcpd.conf
   let network = get-query-value("network");
   network := *config*.config-nets[string-to-integer(network)];
   set-content-type(response, "text/plain");
@@ -236,39 +301,43 @@
 
 define method do-action (action == #"add-subnet", response :: <response>)
  => (show-get? :: <boolean>)
-  //add-subnet
   let network = get-query-value("network");
-  format-out("ADD SUBNET\n");
   let cidr = get-query-value("cidr");
   let vlan = string-to-integer(get-query-value("vlan"));
   let dhcp? = if (get-query-value("dhcp") = "dhcp") #t else #f end;
-  format-out("FOO %= %= %=\n", cidr, vlan, dhcp?);
-  let default-lease-time
-    = string-to-integer(get-query-value("default-lease-time"));
-  let max-lease-time
-    = string-to-integer(get-query-value("max-lease-time"));
-  format-out("DHCP %= %=\n", default-lease-time, max-lease-time);
-  let options = parse-options(get-query-value("options"));
-  let dhcp-start = parse-ip(get-query-value("dhcp-start"));
-  let dhcp-end = parse-ip(get-query-value("dhcp-end"));
-  let dhcp-router = parse-ip(get-query-value("dhcp-router"));
-  let subnet = make(<subnet>,
-                    cidr: cidr,
-                    vlan: vlan,
-                    dhcp?: dhcp?,
-                    default-lease-time: default-lease-time,
-                    max-lease-time: max-lease-time,
-                    options: options,
-                    dhcp-start: dhcp-start,
-                    dhcp-end: dhcp-end,
-                    dhcp-router: dhcp-router);
-  add-subnet(*config*.config-nets[string-to-integer(network)], subnet);
+  if (dhcp?)
+    let default-lease-time
+      = string-to-integer(get-query-value("default-lease-time"));
+    let max-lease-time
+      = string-to-integer(get-query-value("max-lease-time"));
+    format-out("DHCP %= %=\n", default-lease-time, max-lease-time);
+    let options = parse-options(get-query-value("options"));
+    let dhcp-start = parse-ip(get-query-value("dhcp-start"));
+    let dhcp-end = parse-ip(get-query-value("dhcp-end"));
+    let dhcp-router = parse-ip(get-query-value("dhcp-router"));
+    let subnet = make(<subnet>,
+                      cidr: cidr,
+                      vlan: vlan,
+                      dhcp?: dhcp?,
+                      default-lease-time: default-lease-time,
+                      max-lease-time: max-lease-time,
+                      options: options,
+                      dhcp-start: dhcp-start,
+                      dhcp-end: dhcp-end,
+                      dhcp-router: dhcp-router);
+    add-subnet(*config*.config-nets[string-to-integer(network)], subnet);
+  else
+    let subnet = make(<subnet>,
+                      cidr: cidr,
+                      vlan: vlan,
+                      dhcp?: dhcp?);
+    add-subnet(*config*.config-nets[string-to-integer(network)], subnet);
+  end;
   #t;
 end;
 
 define method do-action (action == #"add-network", response :: <response>)
  => (show-get? :: <boolean>)
-  //add network
   let cidr = get-query-value("cidr");
   let dhcp? = if (get-query-value("dhcp") = "dhcp") #t else #f end;
   let default-lease-time
@@ -300,7 +369,7 @@
 
 define method respond-to-post
     (page == #"vlan", request :: <request>, response :: <response>)
-  let number = string-to-integer(get-query-value("number"));
+  let number = string-to-integer(get-query-value("vlan"));
   let name = get-query-value("name");
   let description = get-query-value("description");
   let vlan = make(<vlan>,
@@ -314,8 +383,7 @@
 define method respond-to-post
     (page == #"host", request :: <request>, response :: <response>)
   let name = get-query-value("name");
-  let ip = make(<ip-address>,
-                ip: string-to-ip-address(get-query-value("ip")));
+  let ip = make(<ip-address>, ip: get-query-value("ip"));
   let mac = get-query-value("mac");
   let zone = get-query-value("zone");
   let network = find-network(*config*, ip);
@@ -405,6 +473,11 @@
   foo := make(<ip-address>, ip: "192.168.0.1") - 2;
   foo := make(<ip-address>, ip: "23.24.24.231") - 66000;
 
+  format-out("as(<string>, 23.23.23.23): %s\n",
+             as(<string>, make(<ip-address>, ip: "23.23.23.23")));
+  format-out("as(<ip-address>, \"23.23.23.23\": %=\n", as(<ip-address>,
+                                                          "23.23.23.23"));
+  format-out("as(<ip-address>, 23): %=\n", as(<ip-address>, 23));
 /*  let dood = make(<dood>,
                   locator: *config*.config-name,
                   direction: #"output",

Modified: trunk/libraries/koala/sources/examples/buddha/cidr.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/cidr.dylan    (original)
+++ trunk/libraries/koala/sources/examples/buddha/cidr.dylan    Fri Sep  2 
20:30:18 2005
@@ -44,16 +44,17 @@
 
 define method print-object (cidr :: <cidr>, stream :: <stream>)
  => ()
-  format(stream, "%s", cidr-to-string(cidr));
+  format(stream, "%s", as(<string>, cidr));
 end;
 
 define method print-html (cidr :: <cidr>, stream :: <stream>)
  => ()
-  format(stream, "<td>%s</td>", cidr-to-string(cidr));
+  format(stream, "<td>%s</td>", as(<string>, cidr));
 end;
 
-define method cidr-to-string (cidr :: <cidr>) => (string :: <string>)
-  concatenate(ip-address-to-string(cidr.cidr-network-address), "/",
+define method as (class == <string>, cidr :: <cidr>)
+ => (res :: <string>)
+  concatenate(as(<string>, network-address(cidr)), "/",
               integer-to-string(cidr.cidr-netmask));
 end;
 
@@ -61,7 +62,7 @@
  => (ip-address :: <ip-address>)
   make(<ip-address>,
        ip: map(logand,
-               netmask-to-vector(cidr.cidr-netmask),
+               ip(netmask-address(cidr)),
                ip(network-address(cidr))));
 end;
 
@@ -69,7 +70,7 @@
  => (ip-address :: <ip-address>)
   let mask = map(method(x)
                      logand(255, lognot(x));
-                 end, netmask-to-vector(cidr.cidr-netmask));
+                 end, ip(netmask-address(cidr)));
   make(<ip-address>,
        ip: map(logior,
                ip(network-address(cidr)),
@@ -83,6 +84,5 @@
 
 define method netmask-address (cidr :: <cidr>)
  => (ip-address :: <ip-address>)
-  make(<ip-address>,
-       ip: netmask-to-vector(cidr.cidr-netmask));
+  as(<ip-address>, cidr.cidr-netmask);
 end;

Modified: trunk/libraries/koala/sources/examples/buddha/cisco-telnet.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/cisco-telnet.dylan    
(original)
+++ trunk/libraries/koala/sources/examples/buddha/cisco-telnet.dylan    Fri Sep 
 2 20:30:18 2005
@@ -14,7 +14,7 @@
 
 define method connect-to-cisco(cisco :: <cisco-ios-device>)
   let address = make(<internet-address>,
-                     address: cisco.host-ipv4-address.ip-address-to-string);
+                     address: as(<string>, cisco.host-ipv4-address));
 
   let socket = make(<tcp-socket>, host: address, port: 23);
 
@@ -121,5 +121,5 @@
                      concatenate(control.device.host-name, "#"));
   copy-sequence(result, end: result.size - control.device.host-name.size - 1);
 end method send-command;
-  
+
 

Modified: trunk/libraries/koala/sources/examples/buddha/host.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/host.dylan    (original)
+++ trunk/libraries/koala/sources/examples/buddha/host.dylan    Fri Sep  2 
20:30:18 2005
@@ -29,8 +29,8 @@
          host.host-zone.zone-name,
          mac-to-string(host.host-mac));
   format(stream, "IP %s Net %s\n",
-         ip-address-to-string(host.host-ipv4-address),
-         cidr-to-string(host.host-net.network-cidr));
+         as(<string>, host.host-ipv4-address),
+         as(<string>, host.host-net.network-cidr));
 end;
 
 define method \< (a :: <host>, b :: <host>) => (res :: <boolean>)
@@ -40,8 +40,8 @@
 define method print-html (host :: <host>, stream :: <stream>)
   gen-row(stream,
           list(host.host-name,
-               ip-address-to-string(host.host-ipv4-address),
-               cidr-to-string(host.host-net.network-cidr),
+               as(<string>, host.host-ipv4-address),
+               as(<string>, host.host-net.network-cidr),
                mac-to-string(host.host-mac),
                host.host-zone.zone-name));
 end;
@@ -50,8 +50,7 @@
  => ()
   format(stream, "host %s {\n", host.host-name);
   format(stream, "\thardware ethernet %s;\n", mac-to-string(host.host-mac));
-  format(stream, "\tfixed-address %s;\n",
-         ip-address-to-string(host.host-ipv4-address));
+  format(stream, "\tfixed-address %s;\n", as(<string>, 
host.host-ipv4-address));
   format(stream, "}\n\n");
 end;
 

Modified: trunk/libraries/koala/sources/examples/buddha/ipv4.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/ipv4.dylan    (original)
+++ trunk/libraries/koala/sources/examples/buddha/ipv4.dylan    Fri Sep  2 
20:30:18 2005
@@ -12,12 +12,20 @@
                     #all-keys) => (res :: <ip-address>)
   let args = rest;
   if (instance?(ip, <string>))
-    args := exclude(args, #"ip");
-    ip := string-to-ip-address(ip);
-  end;
-  apply(next-method, ip-address, ip: ip, args);
+    ip := as(<ip-address>, ip);
+  else
+    apply(next-method, ip-address, ip: ip, args);
+  end if;
 end;
 
+//print-object
+define method print-object (ip :: <ip-address>,
+                            stream :: <stream>)
+ => ()
+  format(stream, "%s", as(<string>, ip));
+end;
+
+//arithmetic operations: +(ip, int) +(int, ip) -(ip, int)
 define method \+ (a :: <ip-address>, b :: <integer>)
  => (res :: <ip-address>)
   let rem :: <integer> = b;
@@ -60,43 +68,44 @@
   res;
 end;
 
-define method print-object (ip :: <ip-address>,
-                            stream :: <stream>)
- => ()
-  format(stream, "%s", ip-address-to-string(ip));
-end;
-  
-
-define method string-to-netmask (string :: <string>)
- => (netmask :: <integer>)
-  //"255.255.255.0"
-  let vec = reverse(string-to-ip-address(string));
-  //0, 255, 255, 255
-  let mask = 32;
-  block (not-zero)
-    for (ele in vec)
-      if (ele = 0)
-        mask := mask - 8;
-      else
-        for (i from 7 to 0 by -1)
-          unless (logbit?(i, ele))
-            mask := mask - i - 1;
-            not-zero();
-          end unless;
-        end for;
+define method \< (a :: <ip-address>, b :: <ip-address>)
+ => (res :: <boolean>)
+  block(done)
+    for (ele1 in a.ip,
+         ele2 in b.ip)
+      if (ele1 < ele2)
+        done(#t);
+      elseif (ele1 > ele2)
+        done(#f);
       end;
     end for;
+    #f;
   end block;
-  mask;
 end;
 
-define method netmask-to-string (netmask :: <integer>)
- => (string :: <string>)
-  integer-to-string(netmask);
+define method \= (a :: <ip-address>, b :: <ip-address>)
+  => (res :: <boolean>)
+  every?(method(x) x = #t end,
+         map(\=,
+             a.ip,
+             b.ip));
+end;
+
+
+// conversions (string, ip, integer)
+define method as (class == <string>, ip-address :: <ip-address>)
+ => (res :: <string>)
+  let strings = make(<list>);
+  for (ele in ip-address.ip)
+    strings := add(strings, integer-to-string(ele));
+  end;
+  reduce1(method(x, y)
+              concatenate(x, ".", y)
+          end, reverse(strings));
 end;
 
-define method netmask-to-vector (netmask :: <integer>)
- => (vec :: <byte-vector>)
+define method as (class == <ip-address>, netmask :: <integer>)
+ => (res :: <ip-address>)
   let res = make(<byte-vector>, size: 4, fill: 255);
   for (i from 0 below 4,
        mask from netmask by -8)
@@ -106,65 +115,45 @@
       res[i] := logand(255, ash(255, 8 - mask));
     end if
   end for;
-  res;
+  make(<ip-address>, ip: res);
 end;
 
-define method string-to-ip-address (string :: <string>)
- => (ip :: <byte-vector>)
+define method as (class == <ip-address>, string :: <string>)
+ => (res :: <ip-address>)
   let numbers = split(string, '.');
   let ints = map(string-to-integer, numbers);
   let res = make(<byte-vector>, size: 4, fill: 0);
   for (i from 0 below res.size)
     res[i] := as(<byte>, ints[i]);
   end;
-  res;
-end method;
-
-define method ip-address-to-string (ip-address :: <byte-vector>)
- => (string :: <string>)
-  let strings = make(<list>);
-  for (ele in ip-address)
-    strings := add(strings, integer-to-string(ele));
-    //let strings = map(integer-to-string, ip-address);
-    //doesn't work. not sure why... '"10" is not of type limited(<integer>)'
-  end;
-//  let res = 
-  reduce1(method(x, y)
-              concatenate(x, ".", y)
-          end, reverse(strings));
-  //format-out("res %=\n", res);
-  //res;
+  make(<ip-address>, ip: res);
 end;
 
-define method ip-address-to-string (ip-address :: <ip-address>)
- => (string :: <string>)
-  ip-address-to-string(ip-address.ip);
-end;
 
-define method parse-ip (ip) => (ip-address :: false-or(<ip-address>))
-  format-out("PARSE %= %=\n", ip, object-class(ip));
-  #f;
-end;
-
-define method \< (a :: <ip-address>, b :: <ip-address>)
- => (res :: <boolean>)
-  block(done)
-    for (ele1 in a.ip,
-         ele2 in b.ip)
-      if (ele1 < ele2)
-        done(#t);
-      elseif (ele1 > ele2)
-        done(#f);
+define method string-to-netmask (string :: <string>)
+ => (netmask :: <integer>)
+  //"255.255.255.0"
+  let vec = reverse(as(<ip-address>, string).ip);
+  //0, 255, 255, 255
+  let mask = 32;
+  block (not-zero)
+    for (ele in vec)
+      if (ele = 0)
+        mask := mask - 8;
+      else
+        for (i from 7 to 0 by -1)
+          unless (logbit?(i, ele))
+            mask := mask - i - 1;
+            not-zero();
+          end unless;
+        end for;
       end;
     end for;
-    #f;
   end block;
+  mask;
 end;
 
-define method \= (a :: <ip-address>, b :: <ip-address>)
-  => (res :: <boolean>)
-  every?(method(x) x = #t end,
-         map(\=,
-             a.ip,
-             b.ip));
+define method parse-ip (ip) => (ip-address :: false-or(<ip-address>))
+  format-out("PARSE %= %=\n", ip, object-class(ip));
+  #f;
 end;

Modified: trunk/libraries/koala/sources/examples/buddha/library.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/library.dylan (original)
+++ trunk/libraries/koala/sources/examples/buddha/library.dylan Fri Sep  2 
20:30:18 2005
@@ -9,6 +9,8 @@
   use string-extensions, import: { character-type };
   use regular-expressions;
   use network;
+  use system, import: { file-system };
+  use xml-rpc-common;
   export buddha;
 end;
 
@@ -25,4 +27,6 @@
   use dood;
   use regular-expressions;
   use sockets, import: { <tcp-socket>, <internet-address> };
+  use file-system;
+  use xml-rpc-common, import: { base64-encode, base64-decode };
 end;

Modified: trunk/libraries/koala/sources/examples/buddha/subnet.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/subnet.dylan  (original)
+++ trunk/libraries/koala/sources/examples/buddha/subnet.dylan  Fri Sep  2 
20:30:18 2005
@@ -64,11 +64,11 @@
  => ()
   if (subnet.dhcp?)
     format(stream, "subnet %s netmask %s {\n",
-           ip-address-to-string(network-address(subnet.network-cidr)),
-           ip-address-to-string(netmask-address(subnet.network-cidr)));
+           as(<string>, network-address(subnet.network-cidr)),
+           as(<string>, netmask-address(subnet.network-cidr)));
     if (subnet.dhcp-router)
       format(stream, "\toption routers %s;\n",
-             ip-address-to-string(subnet.dhcp-router));
+             as(<string>, subnet.dhcp-router));
     end if;
     if (subnet.dhcp-default-lease-time)
       format(stream, "\tdefault-lease-time %d;\n",
@@ -83,8 +83,8 @@
        end, subnet.dhcp-options);
     do(method(x)
            format(stream, "\trange %s %s;\n",
-                  ip-address-to-string(head(x)),
-                  ip-address-to-string(tail(x)));
+                  as(<string>, head(x)),
+                  as(<string>, tail(x)));
        end, generate-dhcp-ranges(subnet));
     format(stream, "}\n\n");
   end if;

Modified: trunk/libraries/koala/sources/examples/buddha/zone.dylan
==============================================================================
--- trunk/libraries/koala/sources/examples/buddha/zone.dylan    (original)
+++ trunk/libraries/koala/sources/examples/buddha/zone.dylan    Fri Sep  2 
20:30:18 2005
@@ -57,7 +57,7 @@
     do(method(x)
            format(stream, "%s\tIN\tA\t%s\n",
                   x.host-name,
-                  ip-address-to-string(x.host-ipv4-address))
+                  as(<string>, x.host-ipv4-address))
        end, zone.zone-hosts);
     do(method(x)
            format(stream, "%s\tCNAME\t%s\n", head(x), tail(x))
@@ -82,7 +82,7 @@
     do(method(x)
            format(stream, "^%s:%s\n",
                   x.host-name,
-                  ip-address-to-string(x.host-ipv4-address));
+                  as(<string>, x.host-ipv4-address));
        end, zone.zone-hosts);
   else
     //MX
@@ -94,7 +94,7 @@
     do(method(x)
            format(stream, "+%s:%s\n",
                   x.host-name,
-                  ip-address-to-string(x.host-ipv4-address));
+                  as(<string>, x.host-ipv4-address));
        end, zone.zone-hosts);
     //CNAME
     do(method(x)
-- 
Gd-chatter mailing list
Gd-chatter@xxxxxxxxxxxxxxxx
https://gauss.gwydiondylan.org/mailman/listinfo/gd-chatter




Try Searching:
servers, voip, java, networking, microsoft ...
<Prev in Thread] Current Thread [Next in Thread>