World OMstatus OMconnUnix(OMconn conn, char *socketpath) { OMdev indev, outdev; int nsd; struct sockaddr_un nad; winInit(); nsd = socket(AF_UNIX, SOCK_STREAM, 0); if (nsd < 0) { conn->error = OMerrorSys; return OMfailed; } ZERO(nad); nad.sun_family = AF_UNIX; strcpy(nad.sun_path, socketpath); if (connect(nsd, (struct sockaddr *) &nad, sizeof nad) < 0) { return OMfailed; } indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(nsd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(nsd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
World OMstatus OMbindUnix(OMconn conn, char *file) { OMdev indev, outdev; int nsd, ansd; int dummy; struct sockaddr_un nad; struct sockaddr_un target; winInit(); nsd = socket(AF_UNIX, SOCK_STREAM, 0); if (nsd < 0) { conn->error = OMerrorSys; return OMfailed; } /* fill the address */ ZERO(nad); nad.sun_family = AF_UNIX; strcpy(nad.sun_path, file); /* bind "nsd" at the UNIX-domain address. "+2" seems necessary... */ if (bind(nsd, (struct sockaddr *) &nad, strlen(nad.sun_path) + 2) != 0) { if (getErrno() == EADDRINUSE) { conn->error = OMaddrInUse; return OMaddrInUse; } else { conn->error = OMerrorSys; return OMfailed; } } listen(nsd, 1); /* now, we can accept */ dummy = sizeof(target); ansd = accept(nsd, (struct sockaddr *) &target, &dummy); if (ansd < 0) { conn->error = OMerrorSys; return OMfailed; } /* don't need it anymore */ close(nsd); indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
/*! \brief Fonction principale: on peut choisir le nombre de points * en utilisant l'option "-nX" où X est un entier strictement * positif. * \remark Mettre opterr a 0 equivaut a supprimer volontairement les messages d'erreur renvoyes par getopt * lors de la lecture d'options non prevus sur la ligne de commande. Dans ces cas, l'erreur est traitee grace au * fait que getopt retourne la valeur '?', qui est donc traitee a part * \remark "n:" signifie que l'option n s'accompagne de la lecture immediate d'une valeur. Il ne faut pas * laisser d'espace entre -n et le nombre tape. Exemple: two -n1000.\par * La lecture est effectuee par un sscanf (lecture d'une chaine supposee formattee) sur l'argument ad hoc, optarg, * declare dans les routines de lecture de la ligne de commande. */ int main(int argc, char **argv) { int c; opterr = 0; while ((c = getopt(argc, argv, "n:f:?h")) != EOF) { switch (c) { case 'n': if ((sscanf(optarg, "%d", &nbPoints) != 1) || nbPoints <= 0) nbPoints = 50; break; case 'f': if ((sscanf(optarg, "%d", &nbPoints) != 1) || nbPoints <= 0) nbPoints = 50; break; case 'h': case '?': printf("use option -nX or -fX (no space), with 0 < X.\n"); return EXIT_SUCCESS; break; default : printf("Shouldn't be here, really...\n"); break; } } assert(nbPoints > 0); T = (vertex *) malloc(sizeof(vertex)*nbPoints); assert(T != NULL); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition(5,5); glutInitWindowSize(300,300); glutCreateWindow("fenetre"); lireFichier("coord2.txt"); winInit(); selectPoints(); positionPointsParRapportPolygone(); glutDisplayFunc(display); glutMainLoop(); return EXIT_SUCCESS; }
World OMstatus OMbindTCP(OMconn conn, int port) { OMdev indev, outdev; int nsd, ansd, dummy; struct sockaddr_in nad; struct sockaddr_un target; winInit(); nsd = socket(AF_INET, SOCK_STREAM, 0); ZERO(nad); nad.sin_family = AF_INET; nad.sin_addr.s_addr = INADDR_ANY; nad.sin_port = htons((unsigned short) port); if (bind(nsd, (struct sockaddr *) &nad, sizeof nad) != 0) { close(nsd); if (getErrno() == EADDRINUSE) { conn->error = OMaddrInUse; return OMaddrInUse; } else { conn->error = OMerrorSys; return OMfailed; } } listen(nsd, 1); dummy = sizeof(target); ansd = accept(nsd, (struct sockaddr *) &target, &dummy); close(nsd); indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
World OMstatus OMconnTCP(OMconn conn, char *machine, int port) { OMdev indev, outdev; int nsd; struct sockaddr_in nad; winInit(); nsd = socket(AF_INET, SOCK_STREAM, 0); ZERO(nad); nad.sin_family = AF_INET; nad.sin_port = htons((unsigned short) port); { unsigned long addr; /* if we are lucky, the machine is XXX.XXX.XXX.XXX ... */ if ((addr = inet_addr(machine)) == -1) { struct hostent *host; if ((host = gethostbyname(machine)) == (struct hostent *) NULL) { return OMconnectFailed; } memcpy((char *) &nad.sin_addr, host->h_addr, host->h_length); } else { memcpy((char *) &nad.sin_addr, (char *) &addr, sizeof addr); } } if (connect(nsd, (struct sockaddr *) &nad, sizeof nad) < 0) { return OMconnectFailed; } indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(nsd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(nsd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
Module OMstatus ONlaunchEnvLocal(OMconn conn, char *cmd, char *env) { OMdev indev, outdev; int nsd, ansd, dummy; struct sockaddr_un nad, target; char basename[MAXPATHLEN]; char cmdline[MAXCMDLEN]; int i, len; winInit(); nsd = socket(AF_UNIX, SOCK_STREAM, 0); ZERO(nad); nad.sun_family = AF_UNIX; /* construct the basename of the UNIX socket */ /* FIXME */ #ifndef WIN32 sprintf(basename, "%s%d", OM_UNIX_PATH, (int) getuid()); #else { char tmp[255]; len = sizeof(tmp); if (!GetUserName(tmp, &len)) { sprintf(tmp, "winuser"); } sprintf(basename, "%s%s", OM_UNIX_PATH, tmp); } #endif strcpy(nad.sun_path, basename); /* try to bind */ len = strlen(basename); for (i = 0; i < MAXNB; i++) { sprintf(nad.sun_path + len, "_%d", i); /* the +2 is still a mystery for me... */ if (bind(nsd, (struct sockaddr *) &nad, strlen(nad.sun_path) + 2) == 0) { break; } else { close(nsd); nsd = socket(AF_UNIX, SOCK_STREAM, 0); /* Check... */ } } /* store the socket name to pass to the server */ if (i < MAXNB) sprintf(basename + len, "_%d", i); else /* FIXME */ ; listen(nsd, 1); /* Now, we can launch the service */ /* FIXME redirections ? */ sprintf(cmdline, "OM_CALLER_UNIX_SOCKET=%s %s &", basename, cmd); system(cmdline); /* and accept connections ... */ dummy = sizeof(target); ansd = accept(nsd, (struct sockaddr *) &target, &dummy); indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
Module OMstatus OMlaunchEnvRemote(OMconn conn, char *machine, char *cmd, char *env) { OMdev indev, outdev; int nsd, dummy, ansd; struct sockaddr_in nad, target; char local[MAXHOSTNAMELEN + 1]; int port; char rshcmd[MAXCMDLEN]; int i; winInit(); nsd = socket(AF_INET, SOCK_STREAM, 0); ZERO(nad); nad.sin_family = AF_INET; nad.sin_addr.s_addr = INADDR_ANY; for (i = IPPORT_RESERVED + 1; i < MAXPORT; i++) { nad.sin_port = htons((unsigned short) i); if (bind(nsd, (struct sockaddr *) &nad, sizeof nad) == 0) { break; } else { /* when "bind" failed, we obtain a "fresh" socket to avoid strange problems... */ close(nsd); nsd = socket(AF_INET, SOCK_STREAM, 0); } } if (i < MAXPORT) port = i; else return OMfailed; /* we where unable to find a free local IP port. */ listen(nsd, 1); /* get our host IP address or IP name */ OMgetLocalHostAddress(local); /* Now, we can launch the service */ /* this computation is wrong... */ if (MAXCMDLEN < (strlen(RSH) + strlen(machine) + ((env == (char *) 0) ? 0 : strlen(env)) + strlen(cmd) + 10 + strlen(local) + strlen(REDIRECT))) { return OMfailed; } if (env != (char *) 0) sprintf(rshcmd, "%s %s OM_CALLER_MACHINE=%s OM_CALLER_PORT=%d %s 'sh -c \" %s %s &\"' &", RSH, machine, local, port, env, cmd, REDIRECT); else sprintf(rshcmd, "%s %s OM_CALLER_MACHINE=%s OM_CALLER_PORT=%d 'sh -c \" %s %s &\"' &", RSH, machine, local, port, cmd, REDIRECT); printf("OMlaunchEnvRemote: %s\n", OMlaunchEnvRemote); system(rshcmd); if (!OMwaitOn(nsd, conn->timeout)) return OMfailed; dummy = sizeof(target); ansd = accept(nsd, (struct sockaddr *) &target, &dummy); close(nsd); indev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); outdev = OMmakeDevice(DEFAULT_ENCODING, OMmakeIOFd(ansd)); conn->in = indev; conn->out = outdev; return OMsuccess; }
int main(int argc, char **argv) { int c; int pol,conv; int option1 = 0, option2 = 0, option3 = 0, option4 = 0; int nbPoints = 50; vertex *v; opterr = 0; while ((c = getopt(argc, argv, "1i:2o:34n:")) != EOF) { switch (c) { case '1': option1 = 1; break; case '2': option2 = 1; break; case '3': option3 = 1; break; case '4': option4 = 1; break; case 'n': if ((sscanf(optarg, "%d", &nbPoints) != 1) || nbPoints <= 0) nbPoints = 50; break; case 'o': /*verifier non null*/ out = optarg; break; case 'i': /*verifier non null*/ in = optarg; break; case 'h': case '?': printf("-1 partie 1 du tp\n"); printf("-2 partie 2 du tp\n"); printf("-3 partie 3 du tp\n"); printf("-ichaine ouvre le fichier \"chaine\" en lecture \n"); printf("-ochaine ouvre le fichier \"chaine\" en écriture \n"); return EXIT_SUCCESS; break; default : printf("Shouldn't be here, really...\n"); break; } } glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition(5,5); glutInitWindowSize(500,500); glutCreateWindow("fenetre"); definitionFenetre(0, 500, 0, 500, 10); winInit(); if(option1 && out != NULL) { glutDisplayFunc(display); glutMouseFunc(coordonnesPoint); ecrireFichier(out, &P); } else if(option2 && in != NULL) { lireFichier(in, &P); assert(P.p != NULL); pol = controlePolygoneSimple(); printf("controlePolygoneSimple : %d\n", pol); //glutDisplayFunc(displayPolygone); } else if(option3 && in != NULL) { lireFichier(in, &P); assert(P.p != NULL); if(controlePolygoneSimple()) conv = estConvexe(); } else if(option4 && in != NULL) { lireFichier(in, &P); assert(P.p != NULL); ALLOUER(v,nbPoints); selectPoints (v, nbPoints); if(controlePolygoneSimple() && estConvexe()) positionPointsParRapportPolygone(v, nbPoints); free(v); } glutMainLoop(); clearFenetre(); return EXIT_SUCCESS; }