Logo
~Apps~
~Projects~
~Contact~

THREAD Class Reference

#include <THREAD.h>

Inheritance diagram for THREAD:

Inheritance graph
[legend]
Collaboration diagram for THREAD:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 THREAD ()
virtual ~THREAD ()
int ioloop (char *, int *)
int read_text (char *, int *)
int write_text (char *, int ll=-1)
void echo_on ()
void echo_off ()
void add_event (char *s)
int catchup ()
int get_adv ()

Public Attributes

THREADnext
pthread_t thread
int running
int socket
int ib
int it
int iq
int ob
int ot
int oq
char inbuf [INBUFSIZE]
char outbuf [OUTBUFSIZE]
Parser cmdline
db::Account * account
EVENTcurrent_event
ADVselected
int prompt_shown
int adjust_case
char last_print [1024]
char prompt [200]
char instr [1024]

Detailed Description

Definition at line 27 of file THREAD.h.


Constructor & Destructor Documentation

THREAD::THREAD  ) 
 

Definition at line 39 of file THREAD.cpp.

References current_event, event_mutex, eventbase, instr, last_print, next, prompt, and running.

00040 :next(NULL)
00041 ,running(0)
00042 ,socket(0)
00043 ,ib(0),it(0),iq(0)
00044 ,ob(0),ot(0),oq(0)
00045 ,account(NULL)
00046 ,current_event(NULL)
00047 ,selected(NULL)
00048 ,prompt_shown(0)
00049 ,adjust_case(0)
00050 {
00051         *last_print = 0;
00052         *prompt = 0;
00053         *instr = 0;
00054 
00055         pthread_mutex_lock(&event_mutex);
00056 
00057         current_event = eventbase;
00058         while (current_event -> next)
00059                 current_event = current_event -> next;
00060 
00061         pthread_mutex_unlock(&event_mutex);
00062 }

THREAD::~THREAD  )  [virtual]
 

Definition at line 65 of file THREAD.cpp.

00066 {
00067 }


Member Function Documentation

void THREAD::add_event char *  s  ) 
 

Definition at line 810 of file THREAD.cpp.

References event_mutex, eventbase, and next.

Referenced by read_text().

00811 {
00812         EVENT *e;
00813         EVENT *q;
00814 
00815         e = new EVENT;
00816         e -> next = NULL;
00817         e -> source = this;
00818         strcpy(e -> text,s);
00819 
00820         pthread_mutex_lock(&event_mutex);
00821 
00822         for (q = eventbase; q -> next; q = q -> next)
00823                 ;
00824         q -> next = e;
00825 
00826         pthread_mutex_unlock(&event_mutex);
00827 }

int THREAD::catchup  ) 
 

Definition at line 830 of file THREAD.cpp.

References current_event, event_mutex, instr, next, PRINT, prompt, and prompt_shown.

Referenced by read_text().

00831 {
00832         int dirty = 0;
00833 
00834         if (current_event -> next)
00835         {
00836                 pthread_mutex_lock(&event_mutex);
00837 
00838                 while (current_event -> next)
00839                 {
00840                         current_event = current_event -> next;
00841                         if (current_event -> source != this)
00842                         {
00843                                 if (!dirty)
00844                                 {
00845                                         PRINT("\n");            // destroys last_print
00846                                 }
00847                                 PRINT( current_event -> text );
00848                                 dirty = 1;
00849                         }
00850                 }
00851 
00852                 pthread_mutex_unlock(&event_mutex);
00853 
00854                 if (dirty)
00855                 {
00856                         PRINT("\n");
00857                         PRINT( prompt );
00858                         PRINT( instr );
00859                         prompt_shown = 1;
00860                 }
00861         }
00862         return 0;
00863 }

void THREAD::echo_off  ) 
 

Definition at line 775 of file THREAD.cpp.

References socket.

00776 {
00777         char    off_string[] = 
00778         {
00779                 (char) IAC,
00780                 (char) WILL,
00781                 (char) TELOPT_ECHO,
00782                 (char)  0,
00783         };
00784 
00785         write(socket, off_string, 3);
00786 }

void THREAD::echo_on  ) 
 

Definition at line 794 of file THREAD.cpp.

References socket.

00795 {
00796         char    off_string[] = 
00797         {
00798                 (char) IAC,
00799                 (char) WONT,
00800                 (char) TELOPT_ECHO,
00801                 (char) TELOPT_NAOFFD,
00802                 (char) TELOPT_NAOCRD,
00803                 (char)  0,
00804         };
00805 
00806         write(socket, off_string, 5);
00807 }

int THREAD::get_adv  ) 
 

Definition at line 866 of file THREAD.cpp.

References advbase, EA_DISKS, INPUT, next, PRINT, and selected.

00867 {
00868         ADV *adv;
00869         ADV *adv2;
00870         int i;
00871         int n;
00872         char slask[200];
00873 
00874         i = 0;
00875         adv2 = NULL;
00876         for (adv = advbase; adv; adv = adv -> next, i++)
00877         {
00878                 sprintf(slask,"%4d) %s\n",adv -> num,adv -> descr);
00879                 PRINT( slask );
00880                 if (i >= 10 && adv -> next)
00881                 {
00882                         PRINT("#/Q/[M]ORE? ");
00883                         INPUT(slask)
00884                         else
00885                         {
00886                                 slask[n] = 0;
00887                                 if (*slask == 'Q')
00888                                 {
00889                                         break;
00890                                 }
00891                                 else
00892                                 if (*slask >= '0' && *slask <= '9')
00893                                 {
00894                                         i = atoi(slask);
00895                                         for (adv2 = advbase; adv2; adv2 = adv2 -> next)
00896                                                 if (adv2 -> num == i)
00897                                                         break;
00898                                         if (adv2)
00899                                                 break;
00900                                 }
00901                         }
00902                         i = 0;
00903                 }
00904         }
00905         if (!adv2 && *slask != 'Q')
00906         {
00907                 PRINT("Adventure #? ");
00908                 INPUT(slask)
00909                 else
00910                 {
00911                         slask[n] = 0;
00912                         if (*slask >= '0' && *slask <= '9')
00913                         {
00914                                 i = atoi(slask);
00915                                 for (adv2 = advbase; adv2; adv2 = adv2 -> next)
00916                                         if (adv2 -> num == i)
00917                                                 break;
00918                         }
00919                 }
00920         }
00921         selected = adv2;
00922         if (adv2)
00923         {
00924                 sprintf(slask,"../%s",adv2 -> dir);
00925                 if (chdir(slask) == -1)
00926                 {
00927                         sprintf(slask,"cp -a %s/%s ..",
00928                                 EA_DISKS,adv2 -> dir);
00929                         printf("Copying Adventure: %s\n",slask);
00930                         system(slask);
00931                 }
00932                 else
00933                 {
00934                         chdir("../ea.master");
00935                 }
00936         }
00937         return 0;
00938 }

int THREAD::ioloop char *  ,
int * 
 

Definition at line 70 of file THREAD.cpp.

References adjust_case, c(), cmdline, ib, inbuf, INBUFSIZE, iq, it, Parser::m_next_line, ob, oq, outbuf, OUTBUFSIZE, and socket.

Referenced by read_text().

00071 {
00072         struct timeval tv;
00073         fd_set rfds;
00074         fd_set wfds;
00075         int n;
00076         int max_l = *l;
00077         char c;
00078         char buffer[1024];
00079 
00080         *l = 0;
00081         while (1)
00082         {
00083                 if (oq)
00084                 {
00085 printf(" - writing %d bytes from output buffer\n",oq);
00086                         if (ob + oq > OUTBUFSIZE)
00087                         {
00088                                 int l = OUTBUFSIZE - ob;
00089                                 n = write(socket, outbuf + ob, l);
00090                         }
00091                         else
00092                         {
00093                                 n = write(socket, outbuf + ob, oq);
00094                         }
00095 printf(" -- %d bytes written\n",n);
00096                         if (n == -1)
00097                         {
00098                                 perror("ioloop() write() failed");
00099 //                              return -1;
00100                         }
00101                         else
00102                         if (!n)
00103                         {
00104                                 cmdline.m_next_line = NULL;
00105                                 return 0;
00106                         }
00107                         else
00108                         {
00109                                 ob += n;
00110                                 if (ob >= OUTBUFSIZE)
00111                                         ob -= OUTBUFSIZE;
00112                                 oq -= n;
00113                         }
00114                 }
00115 
00116                 if (iq)
00117                 {
00118                         c = inbuf[ib++];
00119                         if (ib >= INBUFSIZE)
00120                                 ib -= INBUFSIZE;
00121                         iq--;
00122                         if (c == 13 || c == 10)
00123                         {
00124                                 if (iq && (inbuf[ib] == 13 || inbuf[ib] == 10))
00125                                 {
00126                                         ib++;
00127                                         if (ib >= INBUFSIZE)
00128                                                 ib -= INBUFSIZE;
00129                                         iq--;
00130                                 }
00131                                 if (*l < max_l)
00132                                         s[*l] = '\n';
00133                                 *l = *l + 1;
00134                                 return *l;
00135                         }
00136                         else
00137                         {
00138                                 if (adjust_case)
00139                                         if (c >= 'a' && c <= 'z')
00140                                                 c -= 32;
00141                                 if (*l < max_l)
00142                                         s[*l] = c;
00143                                 *l = *l + 1;
00144                                 if (*l >= max_l)
00145                                 {
00146                                         while (iq && (inbuf[ib] == 13 || inbuf[ib] == 10))
00147                                         {
00148                                                 ib++;
00149                                                 if (ib >= INBUFSIZE)
00150                                                         ib -= INBUFSIZE;
00151                                                 iq--;
00152                                         }
00153                                         return *l;
00154                                 }
00155                         }
00156                 }
00157 
00158                 FD_ZERO(&rfds);
00159                 FD_ZERO(&wfds);
00160                 FD_SET(socket, &rfds);
00161                 FD_SET(socket, &wfds);
00162                 tv.tv_sec = 1;
00163                 tv.tv_usec = 0;
00164                 n = select(socket + 1,&rfds,&wfds,NULL,&tv);
00165                 if (n == -1)
00166                 {
00167                         perror("ioloop() select() failed");
00168                         cmdline.m_next_line = NULL;
00169                         return -1;
00170                 }
00171                 if (!n)
00172                 {
00173                         return 0;       // timeout
00174                 }
00175                 else
00176                 {
00177                         if (FD_ISSET(socket, &rfds))
00178                         {
00179                                 n = read(socket, buffer, 1024);
00180                                 if (n == -1)
00181                                 {
00182                                         perror("ioloop() read() failed");
00183                                         cmdline.m_next_line = NULL;
00184                                         return -1;
00185                                 }
00186                                 if (!n)
00187                                 {
00188                                         cmdline.m_next_line = NULL;
00189                                         return 0;
00190                                 }
00191                                 else
00192                                 {
00193                                         if (it + n > INBUFSIZE)
00194                                         {
00195                                                 int l = INBUFSIZE - it;
00196                                                 memmove(inbuf + it,buffer,l);
00197                                                 memmove(inbuf,buffer + l,n - l);
00198                                         }
00199                                         else
00200                                         {
00201                                                 memmove(inbuf + it,buffer,n);
00202                                         }
00203                                         it += n;
00204                                         if (it >= INBUFSIZE)
00205                                                 it -= INBUFSIZE;
00206                                         iq += n;
00207                                 }
00208                         }
00209                         if (FD_ISSET(socket, &wfds) && oq)
00210                         {
00211                                 if (ob + oq > OUTBUFSIZE)
00212                                 {
00213                                         int l = OUTBUFSIZE - ob;
00214                                         n = write(socket, outbuf + ob, l);
00215                                 }
00216                                 else
00217                                 {
00218                                         n = write(socket, outbuf + ob, oq);
00219                                 }
00220                                 if (n == -1)
00221                                 {
00222                                         perror("ioloop() write() failed");
00223 //                                      return -1;
00224                                 }
00225                                 else
00226                                 if (!n)
00227                                 {
00228                                         cmdline.m_next_line = NULL;
00229                                         return 0;
00230                                 }
00231                                 else
00232                                 {
00233                                         ob += n;
00234                                         if (ob >= OUTBUFSIZE)
00235                                                 ob -= OUTBUFSIZE;
00236                                         oq -= n;
00237                                 }
00238                         }
00239                 }
00240         } // while (1)
00241 }

int THREAD::read_text char *  ,
int * 
[virtual]
 

Implements ParserIO.

Definition at line 245 of file THREAD.cpp.

References account, add_event(), adjust_case, catchup(), cmdline, Parser::get_variable(), instr, ioloop(), last_print, Parser::m_next_line, next, PRINT, prompt, prompt_shown, selected, thread_mutex, threadbase, and value.

00246 {
00247         FILE *fil;
00248         int n = 0;
00249         int i;
00250         int max_l = *l;
00251         int x;
00252         int crlf = 0;
00253         int iol;
00254         char slask[1024];
00255 
00256         if (strstr(last_print,"INSERT DISKETTE WITH ADVENTURE (OR KEEP") &&
00257             strstr(last_print,"THIS DISKETTE FOR BEGINNERS CAVE) IN") &&
00258             strstr(last_print,"DISK DRIVE IN SLOT SIX, DRIVE ONE THEN") &&
00259             strstr(last_print,"HIT 'C'"))
00260         {
00261 printf(" *** CHANGE DIR TO SELECTED ADVENTURE\n");
00262                 if (selected)
00263                 {
00264                         sprintf(slask,"../%s",selected -> dir);
00265                         if (chdir(slask) == -1)
00266                         {
00267                                 perror("chdir() failed");
00268                         }
00269                 }
00270                 strcpy(s,"C");
00271                 *l = 1;
00272                 return 1;
00273         }
00274 
00275         x = -1;
00276         for (i = 0; i < (int)strlen(last_print); i++)
00277                 if (last_print[i] == 13 || last_print[i] == 10)
00278                         x = i;
00279         strcpy(prompt, (x > -1) ? last_print + x + 1 : last_print);
00280         prompt_shown = 1;
00281 
00282         *l = 0;
00283         *instr = 0;
00284 
00285         while (1)
00286         {
00287                 // show events
00288                 if (catchup() == -1)
00289                         return -1;
00290 
00291                 if (!prompt_shown)
00292                 {
00293                         PRINT( prompt );
00294                         PRINT( instr );
00295                         prompt_shown = 1;
00296                 }
00297 
00298                 iol = max_l;
00299                 n = ioloop(slask, &iol);
00300 printf(" - ioloop() returns %d\n",n);
00301                 if (n == -1)
00302                 {
00303                         perror("read() failed");
00304                         cmdline.m_next_line = NULL;
00305                         return -1;
00306                 }
00307                 else
00308                 if (n == 0)
00309                 {
00310                         cmdline.m_next_line = NULL;
00311                         return 0;
00312                 }
00313                 else
00314                 {
00315                         for (i = 0; i < n; i++)
00316                         {
00317                                 if (*l < max_l) // check buffer overflow
00318                                 {
00319                                         instr[*l] = slask[i];
00320                                         s[*l] = slask[i];
00321                                 }
00322                                 if (slask[i] == 13 || slask[i] == 10)
00323                                         crlf = 1;
00324                                 *l = *l + 1;
00325                         }
00326                         instr[*l] = s[*l] = 0;
00327 
00328                         if (crlf || *l >= max_l)
00329                         {
00330                                 n = *l;
00331                                 s[*l] = 0;
00332                                 while (s[strlen(s) - 1] == 13 || s[strlen(s) - 1] == 10)
00333                                         s[strlen(s) - 1] = 0;
00334                                 switch (*s)
00335                                 {
00336                                 case '#':
00337                                         fil = fopen("/home/eamon/ends.txt","at");
00338                                         if (!fil)
00339                                                 fil = fopen("/home/eamon/ends.txt","wt");
00340                                         fprintf(fil,"%s\n\n",last_print);
00341                                         fclose(fil);
00342                                         PRINT("Ok.\n");
00343                                         break;
00344                                 case '"':
00345                                 case '\'':
00346                                         sprintf(slask,"%s says '%s'.",account -> login.c_str(),s + 1);
00347                                         add_event( slask );
00348                                         PRINT("Ok.\n");
00349                                         break;
00350                                 case '/':
00351                                         if (!strcasecmp(s, "/abort"))
00352                                         {
00353                                                 PRINT("Aborting program, press enter.\n");
00354                                                 cmdline.m_next_line = NULL;
00355                                         }
00356                                         else
00357                                         if (!strncasecmp(s, "/e", 2)) // emote
00358                                         {
00359                                                 x = -1;
00360                                                 for (i = 0; i < (int)strlen(s); i++)
00361                                                         if (s[i] == ' ')
00362                                                         {
00363                                                                 x = i;
00364                                                                 break;
00365                                                         }
00366                                                 if (x > -1)
00367                                                 {
00368                                                         sprintf(slask,"%s %s\n",account -> login.c_str(),s + x + 1);
00369                                                         add_event(slask);
00370                                                         PRINT( slask );
00371                                                 }
00372                                         }
00373                                         else
00374                                         if (!strncasecmp(s, "/w", 2))
00375                                         {
00376                                                 THREAD *t;
00377                                                 VARIABLE *v;
00378                                                 pthread_mutex_lock(&thread_mutex);
00379                                                 for (t = threadbase; t; t = t -> next)
00380                                                 if (t -> account) //login)
00381                                                 {
00382                                                         PRINT( (char *)t -> account -> login.c_str());
00383                                                         v = t -> cmdline.get_variable("NA$");
00384                                                         if (v)
00385                                                         {
00386                                                                 PRINT("  ");
00387                                                                 PRINT(v -> svalue);
00388                                                         }
00389                                                         v = t -> cmdline.get_variable("RO");
00390                                                         if (v)
00391                                                         {
00392                                                                 PRINT("  Room#");
00393                                                                 sprintf(slask,"%d",(int)v -> value);
00394                                                                 PRINT( slask );
00395                                                         }
00396                                                         PRINT("\n");
00397                                                 }
00398                                                 pthread_mutex_unlock(&thread_mutex);
00399                                         }
00400                                         break;
00401                                 default:
00402                                         // convert to eamon standard uppercase
00403                                         if (adjust_case)
00404                                         {
00405                                                 for (i = 0; i < (int)strlen(s); i++)
00406                                                         if (s[i] >= 'a' && s[i] <= 'z')
00407                                                                 s[i] -= 32;
00408                                         }
00409                                         return n;
00410                                 }
00411                         } // if (crlf)
00412                         if (crlf)
00413                         {
00414                                 prompt_shown = 0;
00415                                 *l = 0;
00416                                 crlf = 0;
00417                                 *instr = 0;
00418                         }
00419                 }
00420         } // while (1)
00421         return -1;
00422 }

int THREAD::write_text char *  ,
int  ll = -1
[virtual]
 

Implements ParserIO.

Definition at line 424 of file THREAD.cpp.

References adjust_case, c(), last_print, oq, ot, outbuf, and OUTBUFSIZE.

00425 {
00426         int l = (ll == -1) ? strlen(s) : ll;
00427         int i;
00428         int cr = 0;
00429         int uc = 1;
00430         char c;
00431 
00432         strncpy(last_print, s, l);
00433         last_print[l] = 0;
00434 
00435         for (i = 0; i < l; i++)
00436         {
00437                 if (s[i] == '\n')
00438                 {
00439                         if (!cr)
00440                         {
00441 outbuf[ot++] = '\r';
00442 if (ot > OUTBUFSIZE)
00443         ot -= OUTBUFSIZE;
00444 oq++;
00445 outbuf[ot++] = '\n';
00446 if (ot > OUTBUFSIZE)
00447         ot -= OUTBUFSIZE;
00448 oq++;
00449                                 cr = 1;
00450                         }
00451                         else
00452                         {
00453                                 cr = 0;
00454                         }
00455                 }
00456                 else
00457                 {
00458                         c = s[i];
00459                         if (adjust_case)
00460                         {
00461                         switch (c)
00462                         {
00463                         case '.':
00464 //                      case '\'':
00465                         case '"':
00466                         case '!':
00467                         case ':':
00468                         case '?':
00469                         case '-':
00470                         case '=':
00471                         case '(':
00472                         case ')':
00473                                 uc = 1;
00474                                 break;
00475                         }
00476                         if (s[i] >= 'A' && s[i] <= 'Z')
00477                         {
00478                                 if (!uc)
00479                                         c += 32;
00480                                 uc = 0;
00481                         }
00482                         } // if (adjust_case)
00483 outbuf[ot++] = c;
00484 if (ot > OUTBUFSIZE)
00485         ot -= OUTBUFSIZE;
00486 oq++;
00487                         cr = 0;
00488                 }
00489         }
00490         return l;
00491 }


Member Data Documentation

db::Account* THREAD::account
 

Definition at line 54 of file THREAD.h.

Referenced by read_text().

int THREAD::adjust_case
 

Definition at line 58 of file THREAD.h.

Referenced by ioloop(), read_text(), and write_text().

Parser THREAD::cmdline
 

Definition at line 53 of file THREAD.h.

Referenced by ioloop(), and read_text().

EVENT* THREAD::current_event
 

Definition at line 55 of file THREAD.h.

Referenced by catchup(), and THREAD().

int THREAD::ib
 

Definition at line 47 of file THREAD.h.

Referenced by ioloop().

char THREAD::inbuf[INBUFSIZE]
 

Definition at line 49 of file THREAD.h.

Referenced by ioloop().

char THREAD::instr[1024]
 

Definition at line 61 of file THREAD.h.

Referenced by catchup(), read_text(), and THREAD().

int THREAD::iq
 

Definition at line 47 of file THREAD.h.

Referenced by ioloop().

int THREAD::it
 

Definition at line 47 of file THREAD.h.

Referenced by ioloop().

char THREAD::last_print[1024]
 

Definition at line 59 of file THREAD.h.

Referenced by read_text(), THREAD(), and write_text().

THREAD* THREAD::next
 

Definition at line 43 of file THREAD.h.

Referenced by add_event(), catchup(), get_adv(), read_text(), and THREAD().

int THREAD::ob
 

Definition at line 48 of file THREAD.h.

Referenced by ioloop().

int THREAD::oq
 

Definition at line 48 of file THREAD.h.

Referenced by ioloop(), and write_text().

int THREAD::ot
 

Definition at line 48 of file THREAD.h.

Referenced by write_text().

char THREAD::outbuf[OUTBUFSIZE]
 

Definition at line 50 of file THREAD.h.

Referenced by ioloop(), and write_text().

char THREAD::prompt[200]
 

Definition at line 60 of file THREAD.h.

Referenced by catchup(), read_text(), and THREAD().

int THREAD::prompt_shown
 

Definition at line 57 of file THREAD.h.

Referenced by catchup(), and read_text().

int THREAD::running
 

Definition at line 45 of file THREAD.h.

ADV* THREAD::selected
 

Definition at line 56 of file THREAD.h.

Referenced by get_adv(), and read_text().

int THREAD::socket
 

Definition at line 46 of file THREAD.h.

Referenced by echo_off(), echo_on(), and ioloop().

pthread_t THREAD::thread
 

Definition at line 44 of file THREAD.h.


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2005 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4