annotate wc_sigos/UnixChrisFileImpl.cpp @ 5:7599728af8ba default tip

add wc
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Sat, 19 Jun 2021 07:24:43 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #impl "UnixCharImpl.h" as "UnixCharImpl.h"
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 typedef struct UnixFileImpl <> ipml File {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 int fd; // unix file descriptor
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 int mode;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Data *fileImpl ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 __code setOutput(UnixFileImpl* file, Output *output,__code next(...)) ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 __code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 __code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 } File;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 File* createUnixChrisFileImpl(struct Context* context) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 File *file = new File();
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 file->ChrisFileImpl = (union Data*)new ChrisFileImpl();
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 return file;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 // Topology manageres determins input / output connection
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 //
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 // Topology manageres determins input / output connection
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 //
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 __code gearsConnect(Wc* wc, Input *input, Output *output,__code next(...)) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 file->reader = output ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 input->add(&file->ack,"ack");
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 goto next(...);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 int
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 putBlock(UnixFileImpl* file, Output *reader) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 Block *block = new Block();
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 int len = read(fd, BUFSIZE, block->data);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 if (len <=0 ) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 block->eof |= BLOCK_FLAG_EOF;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 close(file->fd);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 return 0;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 reader->put(block);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return 1 ;
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 __code unixOpen(UnixFileImpl* file,Key *key, Output reader, __code next(...)) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 file->fd = open(key->path,unix_mode(key->modde));
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 if (fd < 0) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 goto error("can't open");
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 if (putBlock(file,reader)) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 putBlock(file,reader);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 goto next(...);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 __code uniAck(UnixFileImpl* file,Ack *ack, Output reader, __code next(...)) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 if (!ack->isOk) {
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 close(file->fd);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 goto next(...);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 putBlock(file,reader);
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 goto next(...); // file is automaticaly put into local dataGearManger/input
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61