00001 #include <stdio.h>
00002 #include <sched.h>
00003 #include <unistd.h>
00004 #include <time.h>
00005 #include <ctype.h>
00006 #include <list>
00007 #include "Connector.h"
00008 #include "ConnectionWrap.h"
00009 #include "BufferedFile.h"
00010 #include "BufferedFileFactory.h"
00011 #include "ConnectionFactory.h"
00012 #include "Connection.h"
00013 #include "FilterConnection.h"
00014 #include "OutputConnection.h"
00015 #include "Clock.h"
00016 #include "connect.h"
00020 #define BUFFSIZE 512
00021
00024 int connect(hostent * he, int port);
00028 bool isNum(char *arg);
00032 string getIP(int ip);
00036 int main(int argc, char *argv[]){
00037 try {
00038 string * name = NULL;
00039 list<hostent> hosts;
00040 list<int> ports;
00041 try {
00042 if (argc >= 3) {
00043 int index = 2;
00044 int offset = 0;
00045 if ((argc+1)%2 == 1) {
00046 cout << "NAME\n";
00047 name = new string(argv[1]);
00048 index = 2;
00049 offset = 0;
00050 } else if ((argc+1)%2 == 0) {
00051 cout << "NONAME\n";
00052 name = NULL;
00053 index = 1;
00054 offset = 1;
00055 }
00056 for (; index < argc; index++) {
00057 if ((index+offset)%2==1) {
00058 if (!isNum(argv[index])) {
00059 string error = string("Arguement Invalid: ");
00060 error += string(argv[index]);
00061 throw error.c_str();
00062 }
00063 int port = atoi(argv[index]);
00064 ports.push_back(port);
00065 } else {
00066 struct hostent *he = gethostbyname(argv[index]);
00067 if (he == NULL) {
00068 string errstr = "Could not resolve host ";
00069 errstr += string(argv[index]);
00070 throw errstr.c_str();
00071 }
00072 hosts.push_back(*he);
00073 }
00074 }
00075 } else {
00076 throw (char*)NULL;
00077 }
00078 } catch (char * err) {
00079 if (err != NULL) {
00080 cerr << "ERROR: " << err << "\n";
00081 }
00082 cout << argv[0] << " [name] [host] [port] [host] [port] ...\n";
00083 return 0;
00084 }
00085 list<ConnectionWrap *> outgoing;
00086 list<hostent>::iterator hostIter = hosts.begin();
00087 list<int>::iterator portIter = ports.begin();
00088 BufferedFileFactory bf;
00089 BufferedFile * inputBf = bf.getNewBufferedFile(STDIN_FILENO);
00090 inputBf->setReadOnly();
00091
00092
00093 char * buffer = new char[BUFFSIZE * 2];
00094 int framesize = 2;
00095 while (hostIter != hosts.end() && portIter != ports.end()) {
00096 int port = *portIter;
00097 hostent host = *hostIter;
00098 int fd = connect(&host,port);
00099 cout << host.h_name << " " << port << "\n";
00100 if (fd!=-1) {
00101 cout << "FD: " << fd <<"\n";
00102 BufferedFile * outputBf = bf.getNewBufferedFile(fd);
00103 outputBf->setWriteOnly();
00104 OutputConnection<short> * oc = new OutputConnection<short>(outputBf);
00105 outgoing.push_back(new ConnectionWrap(oc,SHORTCONN));
00106
00107 }
00108 hostIter++;
00109 portIter++;
00110 }
00111 if (name!=NULL) {
00112 string headStart("<header><name>");
00113 string nameEnd("</name>");
00114 string headEnd("</header>");
00115 int length = headStart.length() + headEnd.length() + nameEnd.length() + name->length();
00116 if (length%framesize!=0) {
00117 int count = framesize - length%framesize - 1;
00118 for (int i = 0 ; i < count; i++) {
00119 nameEnd += " ";
00120 }
00121 length = headStart.length() + headEnd.length() + nameEnd.length() + name->length();
00122 }
00123 const char * header = (headStart+(*name)+nameEnd+headEnd).c_str();
00124
00125 bf.process(100);
00126 list<ConnectionWrap *>::iterator iter;
00127 for (iter = outgoing.begin(); iter != outgoing.end(); iter++) {
00128 ConnectionWrap * cw = (*iter);
00129
00130 cw->write((char*)header,length);
00131
00132 }
00133
00134 }
00135 for (;;) {
00136 int rw = bf.process(100);
00137 Clock::clock++;
00138 if (rw >= 2) {
00139
00140
00141 read(STDIN_FILENO,buffer,BUFFSIZE*2);
00142 list<ConnectionWrap *>::iterator iter;
00143 for (iter = outgoing.begin(); iter != outgoing.end(); iter++) {
00144 ConnectionWrap * cw = (*iter);
00145 if (cw->writeReady()) {
00146 cw->write((char*)buffer,BUFFSIZE);
00147 }
00148 }
00149 }
00150 }
00151 } catch (char * str) {
00152 cerr<< "G EXCEPTION: " <<str<<"\n";
00153 } catch (string * str) {
00154 cerr<< "G EXCEPTION: " <<*str<<"\n";
00155 }
00156 return 0;
00157 }
00158 bool isNum(char *arg) {
00159 for (int i = 0; arg[i]!='\0' ; i++) {
00160 if (!isdigit(arg[i])) {
00161 return false;
00162 }
00163 }
00164 return true;
00165 }
00166 string getIP(int ip) {
00167 int i1 = ip >> 24;
00168 int i2 = (ip << 8 )>> 24;
00169 int i3 = (ip << 16)>> 24;
00170 int i4 = (ip << 24)>> 24;
00171 char data[12];
00172 string out = "";
00173 out+=string((snprintf(data,12,"%d",i1),data));
00174 out+=string((snprintf(data,12,"%d",i2),data));
00175 out+=string((snprintf(data,12,"%d",i3),data));
00176 out+=string((snprintf(data,12,"%d",i4),data));
00177 return out;
00178 }