OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / consoles / components.h
1 // file.cxx - Joint header file for nearby component classes.
2 // -*- C++ -*-
3
4 // Copyright (C) 1999, 2000 Red Hat.
5 // This file is part of SID and is licensed under the GPL.
6 // See the file COPYING.SID for conditions for redistribution.
7
8 #include "config.h"
9
10 #include <sidcomp.h>
11 #include <sidso.h>
12 #include <sidcomputil.h>
13 #include <sidattrutil.h>
14 #include <sidpinutil.h>
15 #include <sidbusutil.h>
16 #include <sidtypes.h>
17 #include <sidmiscutil.h>
18 #include <sidschedutil.h>
19 #include <sidpinattrutil.h>
20
21 #include <vector>
22 #include <string>
23 #include <fstream>
24 #include <iostream>
25
26 #include <cstdlib>
27 #include <cerrno>
28 #include <ctime>
29 #include <cassert>
30 extern "C" {
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #define BSD_COMP
35 #include <sys/ioctl.h>
36 #if HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39 #if HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
44 #endif
45 }
46
47
48 namespace console_stuff 
49 {
50
51 using std::vector;
52 using std::string;
53 using std::ostream;
54 using std::istream;
55 using std::cout;
56 using std::flush;
57 using std::cerr;
58 using std::endl;
59   
60 using sid::component;
61 using sid::bus;
62 using sid::host_int_1;
63 using sid::host_int_2;
64 using sid::little_int_1;
65 using sid::host_int_4;
66 using sid::host_int_8;
67 using sid::component_library;
68   
69 using sidutil::no_bus_component;
70 using sidutil::fixed_attribute_map_component;
71 using sidutil::fixed_pin_map_component;
72 using sidutil::no_accessor_component;
73 using sidutil::no_relation_component;
74 using sidutil::output_pin;
75 using sidutil::callback_pin;
76 using sidutil::string2stream;
77 using sidutil::stream2string;
78 using sidutil::make_attribute;
79 using sidutil::parse_attribute;
80 using sidutil::scheduler_event_subscription;
81 using sidutil::std_error_string;
82 using sidutil::tokenize;
83 using sidutil::recursion_limited;
84 using sidutil::recursion_record;
85 using sidutil::make_numeric_attribute;
86
87
88 // -------------------------------------------------------------------------
89
90 class stdioConsole: public virtual component, 
91                     protected fixed_attribute_map_component,
92                     protected no_bus_component,
93                     protected fixed_pin_map_component,
94                     protected no_accessor_component,
95                     protected no_relation_component
96 {
97 public:
98   stdioConsole();
99
100 private:
101   void write(host_int_4 value);
102   void read(host_int_4);
103
104   callback_pin<stdioConsole> activity_pin;
105   callback_pin<stdioConsole> stdout_pin;
106   output_pin stdin_pin;
107
108   // save & restore state
109   string save_state ( );
110   component::status restore_state (const string& state);
111
112 };
113
114
115 // -------------------------------------------------------------------------
116
117   class socketio: public virtual component,
118                   protected no_bus_component,
119                   protected fixed_attribute_map_component,
120                   protected fixed_pin_map_component,
121                   protected no_accessor_component,
122                   protected no_relation_component,
123                   protected recursion_limited
124   {
125   private:
126     callback_pin<socketio> init_pin, fini_pin;
127     void init_handler (host_int_4);
128     void fini_handler (host_int_4);
129
130     // mode settings
131     bool server_p; // server or client mode?
132     bool verbose_p; // verbosity?
133     bool trace_traffic_p; // trace all socket traffic?
134     bool buffer_disconnected_p; // keep outbound data while disconnected?
135
136     // this and other side 
137     sockaddr_in sock_name;
138     sockaddr_in peer_name;
139
140     // outbound data
141     callback_pin<socketio> out_pin;
142     void tx_eof_handler();
143     void tx_handler(host_int_4 value);
144     // inbound data
145     output_pin in_pin;
146
147     // low level state
148     int fd;
149     int serv_fd;
150     bool connected_p;
151     string out_buffer;
152     float avg_out_buffer_size;
153     enum _bs { in_buffer_size = 2048 };
154     char in_buffer [in_buffer_size];
155     host_int_8 in_count;
156     host_int_8 out_count;
157     host_int_8 poll_count;
158
159     // polling info
160     friend class scheduler_event_subscription<socketio>;
161     scheduler_event_subscription<socketio> poll_control;
162
163     // Make these types small enough to avoid having to request long
164     // delays from the scheduler.
165     host_int_2 poll_interval;
166     host_int_2 max_poll_interval;
167
168     void asyncificate (int fd);
169     void update_poll_interval();
170     void poll ();
171     void poll_connect ();
172     void poll_receive ();
173     void poll_transmit ();
174     void wait_more();
175     void wait_less();
176
177     // state control
178     friend ostream& operator << (ostream& o, const socketio& it);
179     friend istream& operator >> (istream& i, socketio& it);
180     string save_state () { return make_attribute(*this); }
181     sid::component::status restore_state (const string& state)
182       { return parse_attribute (state, *this); }
183
184
185   public:
186     socketio(bool server_p);
187   };
188
189
190 } // end of namespace console_stuff
191