00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <libplumbing/config.h>
00019 #include <cstring>
00020 #include <libexplain/write.h>
00021
00022 #include <libplumbing/endpoint/daytime.h>
00023
00024
00025 plumbing::endpoint_daytime::~endpoint_daytime()
00026 {
00027 }
00028
00029
00030 plumbing::endpoint_daytime::endpoint_daytime(int a_fd) :
00031 endpoint(a_fd),
00032 data_position(0),
00033 data_size(0)
00034 {
00035 time_t now;
00036 time(&now);
00037 struct tm *tmp = localtime(&now);
00038 strftime(data, sizeof(data), "%c\n", tmp);
00039 data_size = strlen(data);
00040 }
00041
00042
00043 plumbing::endpoint::pointer
00044 plumbing::endpoint_daytime::create(int a_fd)
00045 {
00046 return pointer(new endpoint_daytime(a_fd));
00047 }
00048
00049
00050 void
00051 plumbing::endpoint_daytime::process_read()
00052 {
00053
00054 char buffer[1024];
00055 ssize_t n = ::read(fd, buffer, sizeof(buffer));
00056 if (n == 0)
00057 kill_me_now();
00058 }
00059
00060
00061 int
00062 plumbing::endpoint_daytime::get_write_file_descriptor()
00063 {
00064 return fd;
00065 }
00066
00067
00068 void
00069 plumbing::endpoint_daytime::process_write()
00070 {
00071 if (data_position >= data_size)
00072 {
00073 kill_me_now();
00074 }
00075 else
00076 {
00077 size_t nbytes = data_size - data_position;
00078 void *ptr = data + data_position;
00079 ssize_t n = ::write(fd, ptr, nbytes);
00080 if (n < 0)
00081 fatal_error("%s", explain_write(fd, ptr, nbytes));
00082 data_position += n;
00083 }
00084 }
00085
00086
00087