MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Reading/Writing files across networks

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36024] Re: Reading/Writing files across networks
  • From: Rolf Mertig <rolf at mertig.com>
  • Date: Tue, 13 Aug 2002 05:22:47 -0400 (EDT)
  • Organization: Mertig Consulting
  • Sender: owner-wri-mathgroup at wolfram.com

>Hello,
> we're running Mathematica 4.2 under Linux.
>Since we're working alot across networks we wonder if it is possible
>with Mathematica to read and write files directly between the Linux
> cluster where we've Mathematica installed and another Unix machine at
> another domain (provided the permissions are set). I.e., is it
> possible by calling OpenRead[...] with the appropriate options to open
>a file on another domain machine for reading ? Perhaps by using pipes
>?! The same applies to OpenWrite, Import, Export...
>Many thanks, regards
>Justus

Hi,
no, Mathematica is (unfortunately) still not integrated enough with Java.
However, you can use a modified GetURL (originally from the JLink manual) and
write wrapper funcionts (like the NetIfiy below) around it.
I.e., you would have to put give users on your cluster 
(local, and even proxied) access to an Apache server with the
filesystem where the Mathematica (or other files) live visible.

E.g.:

Netify[func_, url_String/;StringMatchQ[url,"http:*"], args___]:= 
Block[{imp,tmp},tmp = GetURL[url]; imp=func[tmp,args];DeleteFile[tmp];imp ];

Netify[Get, "http://www.mertig.com/mathdepot/misc/Isolate.m";]
(* this is the same as calling  Get["Isolate.m"]  if Isolate.m would be 
lokal*)


Netify[Import,"http://www.linux.de/pics/Linux.de-logo.gif","GIF"]//Show
(* this just displays a gif *)


(*Here the GetURL funcion: *)

Needs["JLink`"]
$ProxyHost=None;
$ProxyPort=8080;

Options[GetURL]={ProxyHost :> $ProxyHost,
      ProxyPort :> $ProxyPort};
GetURL[url_String, opts___?OptionQ] :=
    JavaBlock[
        Module[{u, stream, numRead, outFile, buf, prxyHost, prxyPort},
            {prxyHost, prxyPort} = {ProxyHost, ProxyPort} /. 
Flatten[{opts}] /. Options[GetURL];
            StartJava[];
            If[StringQ[prxyHost],
                (* Set properties to force use of proxy. *)
                SetInternetProxy[prxyHost, prxyPort]
            ];
            u = JavaNew["java.net.URL", url];
            (* This is where the error will show up if the URL is not valid.
               A Java exception will be thrown during openStream, which
               causes the method to return $Failed.
            *)
            stream = u@openStream[];
            If[stream === $Failed, Return[$Failed]];
            buf = JavaNew["[B", 5000]; (* 5000 is an arbitrary buffer size *)
            outFile = OpenTemporary[DOSTextFormat->False];
            While[(numRead = stream@read[buf]) > 0,
                WriteString[outFile, FromCharacterCode[If[# < 0, # + 256, #]& 
/@ Take[Val[buf], numRead]]]
            ];
            stream@close[];
            Close[outFile]   (* Close returns the filename *)
        ]
    ]

You can find other useful applications of GetURL at 
http://www.mertig.com/neu/HTMLLinks/index_5.html

One could even think of "Netifying" complete complex Mathematica packages and 
applications.
Kind of "ASP" (application service provider). 
Now, if someone pays me I'll set such a system up for them ... (of course 
paying attention to license issues etc.;  maybe the normal Mathematica license 
should just fade out and in the near ".Net-like webservice futer" 
there should be only webMathematica licenses anymore).

Regards from Berlin-Mitte,

Rolf Mertig

Mertig Consulting
http://www.mertig.com


  • Prev by Date: AW: Re: Corresponding function to given values...
  • Next by Date: Mathematica and computing bifurcations
  • Previous by thread: Reading/Writing files across networks
  • Next by thread: Re: Re: Reading/Writing files across networks