aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/ro.c
blob: 541dae4ee54df0236ed4e4c6ec65b7a15fbf9f08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright (c) 2004 Russ Cox */
#include <u.h>
#include <libc.h>
#include <venti.h>
#include <thread.h>
#include <libsec.h>

#ifndef _UNISTD_H_
#pragma varargck type "F" VtFcall*
#pragma varargck type "T" void
#endif

VtConn *z;
int verbose;

enum
{
	STACK = 8192,
};

void
usage(void)
{
	fprint(2, "usage: venti/ro [-v] [-a address] [-h address]\n");
	threadexitsall("usage");
}

void
readthread(void *v)
{
	char err[ERRMAX];
	VtReq *r;
	uchar *buf;
	int n;
	
	r = v;
	buf = vtmalloc(r->tx.count);
	if((n=vtread(z, r->tx.score, r->tx.blocktype, buf, r->tx.count)) < 0){
		r->rx.msgtype = VtRerror;
		rerrstr(err, sizeof err);
		r->rx.error = vtstrdup(err);
		free(buf);
	}else{
		r->rx.data = packetforeign(buf, n, free, buf);
	}
	if(verbose)
		fprint(2, "-> %F\n", &r->rx);
	vtrespond(r);
}

void
threadmain(int argc, char **argv)
{
	VtReq *r;
	VtSrv *srv;
	char *address, *ventiaddress;

	fmtinstall('F', vtfcallfmt);
	fmtinstall('V', vtscorefmt);
	
	address = "tcp!*!venti";
	ventiaddress = nil;
	
	ARGBEGIN{
	case 'v':
		verbose++;
		break;
	case 'a':
		address = EARGF(usage());
		break;
	case 'h':
		ventiaddress = EARGF(usage());
		break;
	default:
		usage();
	}ARGEND

	if((z = vtdial(ventiaddress)) == nil)
		sysfatal("vtdial %s: %r", ventiaddress);
	if(vtconnect(z) < 0)
		sysfatal("vtconnect: %r");

	srv = vtlisten(address);
	if(srv == nil)
		sysfatal("vtlisten %s: %r", address);

	while((r = vtgetreq(srv)) != nil){
		r->rx.msgtype = r->tx.msgtype+1;
		if(verbose)
			fprint(2, "<- %F\n", &r->tx);
		switch(r->tx.msgtype){
		case VtTping:
			break;
		case VtTgoodbye:
			break;
		case VtTread:
			threadcreate(readthread, r, 16384);
			continue;
		case VtTwrite:
			r->rx.error = vtstrdup("read-only server");
			r->rx.msgtype = VtRerror;
			break;
		case VtTsync:
			break;
		}
		if(verbose)
			fprint(2, "-> %F\n", &r->rx);
		vtrespond(r);
	}
	threadexitsall(nil);
}