home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / com / zocdev / pip / pipplug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-30  |  11.5 KB  |  281 lines

  1. /***********************************************************************
  2. *                                                                      *
  3. *   This is an non compilable file that demonstrates, what is          *
  4. *   necessary for a plug                                               *
  5. *                                                                      *
  6. *   ------------------------------------------------------------------ *
  7. *                                                                      *
  8. *   No copyright by Markus Schmidt, 1993                               *
  9. *                                                                      *
  10. ***********************************************************************/
  11.  
  12.  
  13. #define INCL_WINWINDOWMGR
  14. #define INCL_DOSPROCESS
  15. #define INCL_DOSFILEMGR
  16.  
  17. #define INCL_NOCOMMON
  18. #include <os2.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. // PIP include 
  23. // (set PIP_INCL_PLUG to include prototypes)
  24. #define PIP_INCL_PLUG
  25. #include "pip.h"
  26.  
  27. // Key for OS2.INI (keep it uniqe, there might be another Ascii)
  28. #define INI_KEY     "Ascii by M.Schmidt"            
  29.  
  30. // declare a parameter struct for the transfer
  31. typedef struct _TRANSFER_PARM {
  32.     int parm1;        // define ...
  33.     BOOL parm2;        // ... anything ...
  34.     char parm3[10];    // ... you need
  35. } TRANSFER_PARM;
  36.  
  37.  
  38. // private functionprototypes come here
  39. // (externs are declared in PIP.H)
  40.  
  41.  
  42.  
  43. /***********************************************************************
  44. *   Introduce ourselves to the socket                                  *
  45. *                                                                      *
  46. *   Abstract: First entrypoint into the plug to see what it is.        *
  47. *                                                                      *
  48. *   Todo: Fill the PlugName, PlugDescription, PlugType and PlugBits    *
  49. *         from the pps-structure. No internal initialisation to be     *
  50. *         done.                                                        *
  51. ***********************************************************************/
  52. int pipIntro(PIP_SOCKET const * const pps, PIP_PLUG * const ppp)
  53. {
  54.     // My name is TRANSFER, XXX TRANSFER
  55.     strcpy(ppp->PlugName, "TRANSFER");
  56.     strcpy(ppp->PlugDescription, "Trasfer files via xxx protocol");
  57.     // ... and I'm a file transfer protocol
  58.     ppp->PlugType= PIP_TYPE_PROTOCOL;
  59.  
  60.     // Tell the socket what we can do. 
  61.     // If we can filerequest, the socket will not do it himself.
  62.     ppp->PlugBits=    PIP_PBIT_XXXXXX |
  63.                     PIP_PBIT_XXXXXX |
  64.                     PIP_PBIT_XXXXXX;
  65.  
  66.     return (PIP_OK);
  67. }
  68.  
  69.  
  70. /***********************************************************************
  71. *                                                                      *
  72. *   Initialize things                                                  *
  73. *                                                                      *
  74. *   Abstract: Opportunity to init protocol internal stuff. No external *
  75. *             functionality.                                           *
  76. *                                                                      *
  77. *   Todo: Alloc a parameter struct (static would not be reentrant)     *
  78. *         init it with default values, then try to read preset parms   *
  79. *         from OS2.INI                                                 *
  80. *                                                                      *
  81. ***********************************************************************/
  82. int pipInit(PIP_SOCKET const * const pps, PIP_PLUG * const ppp)
  83. {
  84.     int rc=PIP_ERROR;
  85.     ULONG howmany;
  86.     TRANSFER_PARM *ptp;        // pointer transfer parameter
  87.  
  88.     // alloc struct to private pointer
  89.     ptp= ppp->PlugPrivate= malloc(sizeof(TRANSFER_PARM));
  90.  
  91.     // if success
  92.     if (ppp->PlugPrivate!=NULL) {
  93.  
  94.         // preset values, in case no INI-Section there
  95.         ptp->parm1= 0;
  96.         ptp->parm2= TRUE;
  97.         strcpy(ptp->parm2, "");
  98.  
  99.         // try to load init params from OS2.INI
  100.         // if not found, initialisation from above remains valid
  101.         howmany= sizeof(TRANSFER_PARM);
  102.         PrfQueryProfileData(HINI_USERPROFILE, PIP_INIAPPL, INI_KEY,
  103.                             ppp->PlugPrivate, &howmany);
  104.  
  105.         rc= PIP_OK;
  106.     }
  107.  
  108.     return (rc);
  109. }
  110.  
  111.  
  112. /***********************************************************************
  113. *                                                                      *
  114. *   Display a setup-window and store data to TRANSFER_PARM             *
  115. *                                                                      *
  116. *   Abstract: Give the user the opportunity to set some parameters     *
  117. *                                                                      *
  118. *   Todo: See if it is a PM or VIO Application, build a setup          *
  119. *         procedure of some kind.                                      *
  120. *                                                                      *
  121. *                                                                      *
  122. ***********************************************************************/
  123. int pipSetup(PIP_SOCKET const * const pps, PIP_PLUG * const ppp)
  124. {
  125.     HWND hwndDlg;
  126.     int rc; 
  127.  
  128.     // this should not happen, but: never trust a socket
  129.     if (ppp->PlugPrivate==NULL)
  130.         return (PIP_ERROR);    // err if init failed 
  131.  
  132.     // !! the next two if's might not apply for you !!
  133.     if (pps->hab==0 || pps->dlgHwndFrame==0)
  134.         return (PIP_ERROR);    // err not PM appl.
  135.  
  136.     if (pps->hab!=0 && pps->dlgHwndFrame!=0)
  137.         return (PIP_ERROR);    // err not VIO appl.
  138.  
  139.      // open and process dialog 
  140.  
  141.     ....
  142.  
  143.  
  144.     // if dialog not cancelled, save new values to OS2.INI
  145.     if (rc==DID_OK) {
  146.         PrfWriteProfileData(HINI_USERPROFILE, PIP_INIAPPL, INI_KEY, 
  147.                     ppp->PlugPrivate, sizeof(TRANSFER_PARM));
  148.     }
  149.  
  150.     return (PIP_OK);
  151. }
  152.  
  153.  
  154. /***********************************************************************
  155. *   Transfer a file                                                    *
  156. *                                                                      *
  157. *   Abstract: Transfer bits and bytes to the remote station            *
  158. *                                                                      *
  159. *   Todo: Check if path,multipath is filled und eventually display     *
  160. *         a filerequester. If you can't do a filerequester, and        *
  161. *         no name is given, return error.                              *
  162. *         For users convenience show a dialog box with progress or     *
  163. *         at least, show progress via ioConPutData in the terminal     *
  164. *         window.                                                      *
  165. *                                                                      *
  166. *   Parameters: Path - pointer to string containing a single file      *
  167. *                      and pathname.                                   *
  168. *                                                                      *
  169. *               Multifile - Array of pointers to pathnames.            *
  170. *                           Last pointer is NULL.                      *
  171. *                           Process multifile like this:               *
  172. *                           for (i=0; multifile[i]!=NULL; i++) {       *
  173. *                                // send multifile[i]                  *
  174. *                           }                                          *
  175. *                                                                      *
  176. ***********************************************************************/
  177. int pipSend(PIP_SOCKET const * const pps, PIP_PLUG * const ppp,
  178.             unsigned char *path, unsigned char **multipath)
  179. {
  180.     char filename[CCHMAXPATH]= "";
  181.     int rc= PIP_ERROR;
  182.     TRANSFER_PARM *ptp= ppp->PlugPrivate;
  183.  
  184.     // this should not happen, but: never trust a socket
  185.     if (ppp->PlugPrivate==NULL )
  186.         return (rc);
  187.  
  188.     // check for proper combination of path and application type 
  189.     if (multipath!=NULL ||             // maybe we can't multifile 
  190.         path==NULL && pps->hab==0)    // maybe we can't filereq. outside pm
  191.         return (rc);
  192.  
  193.     // if no filename given, ask for one
  194.     if (path==NULL) {
  195.         // use default upload dir for the filerequester
  196.         strcpy(filename, pps->szUploadDir);
  197.  
  198.         // show filerequester (private function)
  199.         path= AskFilename("File to send", filename);
  200.     }
  201.  
  202.     if (path!=NULL) {    // could be NULL, if the user canceled
  203.  
  204.         // here we go to send our stuff
  205.         // load the transferwindow from resource
  206.         // read and send bytes
  207.  
  208.         // IMPORTANT!! IMPORTANT!! IMPORTANT!! IMPORTANT!!
  209.         // IMPORTANT!! IMPORTANT!! IMPORTANT!! IMPORTANT!!
  210.         // Since we control the application, we must ensure
  211.         // that incoming messages are processed now and then!!
  212.         // Put this loop to the core of your processing
  213.         while (WinPeekMsg(pps->hab, &qmsg, 0, 0, 0, PM_REMOVE) ) {
  214.             WinDispatchMsg(pps->hab, &qmsg);
  215.         }
  216.         // IMPORTANT!! IMPORTANT!! IMPORTANT!! IMPORTANT!!
  217.         // IMPORTANT!! IMPORTANT!! IMPORTANT!! IMPORTANT!!
  218.  
  219.     }
  220.  
  221.     return (rc);
  222. }
  223.  
  224.  
  225. /***********************************************************************
  226. *                                                                      *
  227. *   Receive a file                                                     *
  228. *                                                                      *
  229. *   Abstract: Receive bits and bytes from the remote station           *
  230. *                                                                      *
  231. *   Todo: Check if path,multipath is filled und eventually display     *
  232. *         a filerequester. If you can't do a filerequester, and        *
  233. *         no name is given, return error.                              *
  234. *         For users convenience show a dialog box with progress or     *
  235. *         at least, show progress via ioConPutData in the terminal     *
  236. *         window.                                                      *
  237. *                                                                      *
  238. *   Parameters: Path - pointer to string containing a single file      *
  239. *                      and pathname.                                   *
  240. *                                                                      *
  241. *               Multifile - Array of pointers to pathnames.            *
  242. *                           Last pointer is NULL.                      *
  243. *                           Process multifile like this:               *
  244. *                           for (i=0; multifile[i]!=NULL; i++) {       *
  245. *                                // send multifile[i]                  *
  246. *                           }                                          *
  247. *                                                                      *
  248. ***********************************************************************/
  249. int pipReceive(PIP_SOCKET const * const pps, PIP_PLUG * const ppp,
  250.                 unsigned char *path, unsigned char **multipath)
  251. {
  252.  
  253.     // see pipSend-Function
  254.  
  255.     return (PIP_ERROR);
  256. }
  257.  
  258.  
  259. /***********************************************************************
  260. *                                                                      *
  261. *   Free the parameter block                                           *
  262. *                                                                      *
  263. *   Abstract: Opportunity to clean protocol internal mess. No external *
  264. *             functionality required.                                  *
  265. *                                                                      *
  266. *   Todo: Free the parameter block                                     *
  267. *                                                                      *
  268. ***********************************************************************/
  269. int pipCleanup(PIP_SOCKET const * const pps, PIP_PLUG * const ppp)
  270. {
  271.     int rc= PIP_ERROR; 
  272.  
  273.     if (ppp->PlugPrivate!=NULL) {
  274.         free(ppp->PlugPrivate);
  275.         ppp->PlugPrivate= NULL;
  276.         rc= PIP_OK;
  277.     }
  278.  
  279.     return (rc);
  280. }
  281.