void FCGXStream::RecvString(nsACString &s) { PRUint16 len = RecvUint16(); while (len > 0) { PRUint32 n = 0; const char *p = RecvBytes(n, len); if (!p) return; s.Append(p, n); len -= n; } }
NS_IMETHODIMP FCGXStream::ResumeDocumentReceive() { // Make sure all post data has been consumed. PRUint32 len = 0; while (0 != RecvBytes(len)) { } ; // empty loop body if (mFD == INVALID_SOCKET) goto errorAbort; // Begin next block. What we expect depends on whether EndRequest was seen // prior to receiving the first PostData block. If it was, then the next // block must be another EndRequest; otherwise, the next block may be either // Document or EndRequest. Regardless, the set-aside document text will be // read first (pretend the current block is Document so that will happen). mRecvCurBlockType = bt_Document; // Let the input pump start pumping again. PR_ExitMonitor(mLock); return NS_OK; errorAbort: PR_ExitMonitor(mLock); return NS_ERROR_ABORT; }
int SeisPipe2D(DsuTask *zz) { int nt,nx,nz,nw,ntpad,ntfft; int it,ix,iz,izz,iw,iw0,iw1,iw2,iw3,iwmin,iwmax; int nfreqs,verbose; float dt,dx,dy,dz,dw; float freqs[4],fw,w,scale,fftscl; float *p, **v, *wdxov,*sx; complex *cpx; float **qx; void *TabInfo; eTable *et; char msg[80]; int info, ToTid, MasterTid; int sz, pz, pei; int SeisIntPars[20]; float SeisFloPars[20]; /* Receive process control information */ MsgLog(zz, "Receiving Control info ... " ); MasterTid = RecvInt(SeisIntPars, 2, -1, MsgCntl); pei = SeisIntPars[0]; ToTid = SeisIntPars[1]; MsgLog(zz, " Ready \n"); /* Receive: efile and other pars ... */ MsgLog(zz, "Receiving parameters ..." ); TabInfo = RecvBytes(-1, MsgTable); RecvFI(SeisFloPars, 10, SeisIntPars, 10, -1, -1); MsgLog(zz, " Ready \n" ); /* get integer parameters */ nt = SeisIntPars[0]; nx = SeisIntPars[1]; nz = SeisIntPars[2]; ntpad = SeisIntPars[3]; verbose = SeisIntPars[4]; sz = SeisIntPars[5]; pz = SeisIntPars[6]; /* get Floating point parameters */ dt = SeisFloPars[0]; dx = SeisFloPars[1]; dz = SeisFloPars[2]; freqs[0] = SeisFloPars[3]; freqs[1] = SeisFloPars[4]; freqs[2] = SeisFloPars[5]; freqs[3] = SeisFloPars[6]; sz = nz / pz; if (pei == (pz - 1)) sz += nz % pz; sprintf(msg, "Receiving Velocity info (pei = %d, sz = %d) ... ", pei, sz); MsgLog(zz,msg); v = alloc2float(nx, sz); for (iz=0; iz<sz; ++iz) RecvFloat(v[iz], nx, -1, MsgVel); MsgLog(zz, " Ready \n" ); /* determine frequency w sampling */ ntfft = npfar(nt+ntpad); nw = ntfft/2+1; dw = 2.0*PI/(ntfft*dt); iwmin = MAX(0,MIN(nw-1,NINT(2.0*PI*freqs[0]/dw))); iwmax = MAX(0,MIN(nw-1,NINT(2.0*PI*freqs[3]/dw))); /* read extrapolator table */ et = ezread(TabInfo); /* pret(zz -> fp_log, et); */ /* allocate workspace */ MsgLog(zz, "Allocating space ... "); qx = alloc2float(nx,sz); sx = alloc1float(nx); wdxov = alloc1float(nx); cpx = alloc1complex(nx); MsgLog(zz, " Ready \n"); sprintf(msg, "Process (%d) starting loop on depth steps(%d,%d)\n", pei, pei*(nz/pz), pei*(nz/pz) + sz); MsgLog(zz, msg); /* Cleanup qx */ for (iz=0; iz<sz; ++iz) for (ix=0; ix<nx; ++ix) qx[iz][ix] = 0.0; /* loop over frequencies w */ for (iw=iwmin,w=iwmin*dw; iw<iwmax; ++iw,w+=dw) { if (verbose && !(iw%1)) { sprintf(msg, "\t%d/%d\n",iw,iwmax); MsgLog(zz, msg); } /* load wavefield */ RecvCplx(cpx, nx, -1, MsgSlice); /* loop over depth steps nz */ for (iz=0; iz<sz; ++iz) { /* compute 2.0*dx/v(x) and zero migrated data */ for (ix=0; ix<nx; ix++) sx[ix] = 2.0*dx/v[iz][ix]; /* make w*dx/v(x) */ for (ix=0; ix<nx; ++ix) wdxov[ix] = w*sx[ix]; /* extrapolate wavefield */ etextrap(et,nx,wdxov,cpx); /* accumulate migrated data */ for (ix=0; ix<nx; ++ix) qx[iz][ix] += cpx[ix].r; } /* Send down the wavefield */ if ( pei != (pz -1)) SendCplx(cpx, nx, ToTid, MsgSlice); } /* End of loop for iw */ for (iz=0; iz<sz; iz++) { izz = pei*(nz/pz) + iz; if (verbose) { sprintf(msg, "Sending values for iz = %d\n",izz); MsgLog(zz, msg); } SendFI(qx[iz], nx, &izz, 1, MasterTid, MsgDepth); } sprintf(msg, "End of processing for (%d)\n",pei); MsgLog(zz, msg); /* free workspace */ free1float(sx); free1float(wdxov); free2float(v); free2float(qx); free1complex(cpx); pvm_exit(); return(0); }
// The code below illustrates how to use sensocol, but is device/platform dependent. // Please see the inline comments for details int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in servAddr; /* Server address */ unsigned short servPort; /* Server port */ char *servIP; /* Server IP address (dotted quad) */ char sendBuffer[SNDBUFSIZE]; /* Buffer to hold data sent across TCP */ unsigned char recvBuffer[RCVBUFSIZE]; /* Buffer to hold data received */ char schemaBuffer[SCHBUFSIZE]; /* Buffer to hold schema */ unsigned int sendBufferLength, schemaLength; /* Length of string to echo */ int i; sensocol_packet_t packet; /* Sensocol packet */ md5_hash_t schema_md5; unsigned char access_token[16] = {0xbd, 0x3b, 0xf0, 0x4f, 0xe3, 0x99, 0xfc, 0x90, 0x14, 0xfa, 0x0d, 0xdb, 0x03, 0xf2, 0x75, 0x9b}; /* Used to authenticate to server */ unsigned long access_key = 2134507778; /* Used to authenticate with server */ /////////////////////// // The AES 128 encryption key for this device unsigned char key[16]; //= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; for (i = 0; i < sizeof(key);i++) key[i] = i; if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server IP> <Server Port> \n", argv[0]); exit(1); } servIP = argv[1]; /* First arg: server IP address (dotted quad) */ servPort = atoi(argv[2]); /* Use given port, if any */ /////////////////////////////// // Create the sensor packet // ///////////////////// sensocol_packet_init(&packet, "test_sensor", strlen("test_sensor")); sensocol_packet_add_long(&packet, strlen("testLong"), "testLong", 1); sensocol_packet_add_long_long(&packet, strlen("testLong64"), "testLong64", 123456789000); sensocol_packet_add_string(&packet,strlen("testString"), "testString", "testing", strlen("testing")); sensocol_packet_add_bytes(&packet, strlen("testBytes"), "testBytes", "foo foo", strlen("foo foo")); sensocol_packet_add_float(&packet, strlen("afloat"), "afloat", 123.34); sensocol_packet_add_double(&packet, strlen("testDouble"), "testDouble", 123.321); // Create the schema // NOTE: The schema in an embedded system can be HARD CODED as a constant. It need not be generated dynamically. schemaLength = sensocol_schema_from_packet(&packet, schemaBuffer); // Get schema MD5. This too can/should be hard-coded md5(schema_md5, schemaBuffer, schemaLength * 8); // For test/debugging purposes dump the MD5 & Schema dump(stdout, schema_md5, 16); printf("Schema is: %s\n", schemaBuffer); // For test/debugging purposes, dump the encoded data sensocol_print(&packet); // Generate the actual packet to send across the wire //sendBufferLength = sensocol_generate_aes128_post_packet(&packet, access_key, access_token, schema_md5, sendBuffer, key); sendBufferLength = sensocol_generate_post_packet(&packet, access_key, access_token, schema_md5, sendBuffer); printf("Sending Over Wire:\n"); dump(stdout, sendBuffer, sendBufferLength); /* Create a reliable, stream socket using TCP */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&servAddr, 0, sizeof(servAddr)); /* Zero out structure */ servAddr.sin_family = AF_INET; /* Internet address family */ servAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ servAddr.sin_port = htons(servPort); /* Server port */ /* Establish the connection to the echo server */ if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) DieWithError("connect() failed"); /* Send the string to the server */ if (send(sock, sendBuffer, sendBufferLength, 0) != sendBufferLength) DieWithError("send() sent a different number of bytes than expected"); RecvBytes(sock, recvBuffer, 1); /* Receive the same string back from the server */ switch(recvBuffer[0]){ case SENSOCOL_RESPONSE_OK: printf("Data Sent Successfully\n"); break; case SENSOCOL_REQUEST_SCHEMA: printf("Schema Request\n"); sendBufferLength = sensocol_schema_generate_response(&packet, schemaBuffer, schemaLength, sendBuffer); if (send(sock, sendBuffer, sendBufferLength, 0) != sendBufferLength) DieWithError("send() sent a different number of bytes than expected"); RecvBytes(sock, recvBuffer, 1); switch(recvBuffer[0]){ case SENSOCOL_RESPONSE_OK: printf("Data Sent Successfully\n"); break; case SENSOCOL_RESPONSE_SCHEMA_ERROR: DieWithError("Error with Schema\n"); break; case SENSOCOL_RESPONSE_PARSE_ERROR: DieWithError("Error parsing packet\n"); break; } break; case SENSOCOL_RESPONSE_AUTH_ERROR: printf("Invalid User Key or Token\n"); break; default: printf("Unknown Response"); break; } close(sock); exit(0); }
NS_IMETHODIMP FCGXStream::RequestPostData() { // Grab the lock and don't release it until document receive is resumed. // This will block Mozilla's input pump while we're fetching the post data // on the connection. PR_EnterMonitor(mLock); if (mRecvCurBlockType != bt_Document && mRecvCurBlockType != bt_EndRequest) { LogFatal("Stream not positioned correctly for requesting post data."); CloseConnection(true); return NS_ERROR_ABORT; } // Send a RequirePostData block to the web server. SendBlockType(bt_RequirePostData); Flush(false); if (mFD == INVALID_SOCKET) return NS_ERROR_ABORT; // If we're in the middle of receiving the document, save what's left of the // current block and continue receiving and saving document text until a // PostData block finally arrives. if (mRecvCurBlockType == bt_Document) { PRUint32 len = 0; while (const char *p = RecvBytes(len)) { DocText *dt = (DocText *)PR_Malloc(sizeof(DocText) + len - sizeof(void*)); dt->mNext = mDocText; dt->mLen = len; memcpy(dt->mText, p, len); mDocText = dt; } if (mFD == INVALID_SOCKET) return NS_ERROR_ABORT; } // It's possible that by the time we requested post data, the web server // already sent EndRequest. This is OK; the web server was still listening // for RequirePostData and will send it anyway, followed by a second // EndRequest. if (PeekNextBlockType() == bt_EndRequest) { RecvNextBlock(PR_FALSE); // Mark that Endrequest was seen. DocText *dt = (DocText *)PR_Malloc(sizeof(DocText) - sizeof(void*)); dt->mNext = mDocText; dt->mLen = 0; mDocText = dt; mPostDataAfterEndRequest = PR_TRUE; } // The DocText blocks were chained in reverse order, so go reverse them into // the proper order. DocText *dt = nsnull; while (mDocText) { DocText *dt2 = mDocText->mNext; mDocText->mNext = dt; dt = mDocText; mDocText = dt2; } mDocText = dt; // The next block must be a PostData, even if it's empty. if (!RecvNextBlock(PR_FALSE)) return NS_ERROR_ABORT; if (mRecvCurBlockType != bt_PostData) { LogFatal("Stream not positioned at PostData block."); g_recv.LogProtocolError(mRecvBuf + mRecvOut, mRecvIn - mRecvOut); CloseConnection(true); return NS_ERROR_ABORT; } // Post data may now be consumed. return NS_OK; }
NS_IMETHODIMP FCGXStream::Read(char* aBuf, PRUint32 aCount, PRUint32* aResult) { // Mozilla does its HTML reading on a separate thread, so we need to own the // lock. This is the *only* method called by the input pump on a non-main // thread. PR_EnterMonitor(mLock); // Before reading from the stream, check to see if (1) document text has // been set aside while waiting for post data, and (2) we have resumed // receiving the document. if (mDocText && mRecvCurBlockType != bt_PostData) { if (mDocText->mLen == 0) { // End of document was seen during accumulation. *aResult = 0; PR_Free(mDocText); mDocText = nsnull; PR_ExitMonitor(mLock); return NS_OK; } PRUint32 amt = mDocText->mLen - mDocTextOffset; if (amt > aCount) amt = aCount; memcpy(aBuf, mDocText->mText + mDocTextOffset, amt); *aResult = amt; mDocTextOffset += amt; if (mDocTextOffset == mDocText->mLen) { DocText *p = mDocText->mNext; // Save the doc for late use if (mDocTextTail) { mDocTextTail->mNext = mDocText; mDocTextTail = mDocTextTail->mNext; mDocTextTail->mNext = 0; }else { mDocTextHeader = mDocTextTail = mDocText; mDocText->mNext = 0; } mDocText = p; mDocTextOffset = 0; } PR_ExitMonitor(mLock); return NS_OK; } const char *p = RecvBytes(*aResult, aCount); if (p) { memcpy(aBuf, p, *aResult); if (mRecvCurBlockType == bt_Document) { // Save the data for possible late use DocText *dt = (DocText *)PR_Malloc(sizeof(DocText) + *aResult - sizeof(void*)); dt->mLen = *aResult; memcpy(dt->mText, p, *aResult); dt->mNext = nsnull; if (!mDocTextHeader) mDocTextHeader = mDocTextTail = dt; else { mDocTextTail->mNext = dt; mDocTextTail = mDocTextTail->mNext; } } } else *aResult = 0; PR_ExitMonitor(mLock); return NS_OK; }