changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > ventivac / appl/cmd/ventry.b

changeset 146: 2f5a91bbd2af
parent: 7f377ffc9ad3
author: Mechiel Lukkien <mechiel@ueber.net>
date: Thu, 30 Aug 2007 16:35:29 +0200
permissions: -rw-r--r--
description: do not import Source, not needed.
1 implement Ventry;
2 
3 include "sys.m";
4 include "draw.m";
5 include "arg.m";
6 include "bufio.m";
7  bufio: Bufio;
8  Iobuf: import bufio;
9 include "venti.m";
10 include "vac.m";
11 
12 sys: Sys;
13 venti: Venti;
14 vac: Vac;
15 
16 print, sprint, fprint, fildes: import sys;
17 Score, Session, Entry, Entrysize, Datatype, Dirtype: import venti;
18 Vacfile: import vac;
19 
20 Ventry: module {
21  init: fn(nil: ref Draw->Context, args: list of string);
22 };
23 
24 addr := "net!$venti!venti";
25 dflag := 0;
26 
27 init(nil: ref Draw->Context, args: list of string)
28 {
29  sys = load Sys Sys->PATH;
30  arg := load Arg Arg->PATH;
31  bufio = load Bufio Bufio->PATH;
32  venti = load Venti Venti->PATH;
33  vac = load Vac Vac->PATH;
34 
35  venti->init();
36  vac->init();
37 
38  index := 0;
39  arg->init(args);
40  arg->setusage(arg->progname() + " [-d] [-a addr] [-i index] score");
41  while((c := arg->opt()) != 0)
42  case c {
43  'a' => addr = arg->earg();
44  'd' => dflag++;
45  'i' => index = int arg->earg();
46  * => arg->usage();
47  }
48  args = arg->argv();
49  if(len args != 1)
50  arg->usage();
51 
52  (ok, score) := Score.parse(hd args);
53  if(ok != 0)
54  error("bad score: "+hd args);
55 
56  say("dialing");
57  (cok, conn) := sys->dial(addr, nil);
58  if(cok < 0)
59  error(sprint("dialing %s: %r", addr));
60  fd := conn.dfd;
61  say("have connection");
62 
63  session := Session.new(fd);
64  if(session == nil)
65  error(sprint("handshake: %r"));
66  say("have handshake");
67 
68  ed := session.read(score, Dirtype, venti->Maxlumpsize);
69  if(ed == nil)
70  error(sprint("reading entry: %r"));
71  o := (index+1)*Entrysize;
72  if(o > len ed)
73  error(sprint("only %d entries present", len ed/Entrysize));
74  e := Entry.unpack(ed[o-Entrysize:o]);
75  if(e == nil)
76  error(sprint("parsing entry: %r"));
77  say("entry unpacked");
78 
79  bio := bufio->fopen(fildes(1), bufio->OWRITE);
80  if(bio == nil)
81  error(sprint("fopen stdout: %r"));
82 
83  f := Vacfile.new(session, e);
84  buf := array[e.dsize] of byte;
85  for(;;) {
86  n := f.read(buf, len buf);
87  if(n < 0)
88  error(sprint("reading: %r"));
89  if(n == 0)
90  break;
91  say(sprint("have %d", n));
92  if(bio.write(buf, n) != n)
93  error(sprint("writing: %r"));
94  }
95  if(bio.flush() < 0)
96  error(sprint("closing: %r"));
97 }
98 
99 error(s: string)
100 {
101  fprint(fildes(2), "%s\n", s);
102  raise "fail:"+s;
103 }
104 
105 say(s: string)
106 {
107  if(dflag)
108  fprint(fildes(2), "%s\n", s);
109 }