N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/OrbisQuartus/server/xen/orbisquartus-xen.cpp

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 
00036 #include <sys/types.h>
00037 #include <sys/socket.h>
00038 #include <orbisquartus.h>
00039 #include <mini_mpi.h>
00040 #include <daemon.h>
00041 #include <config.h>
00042 #include <virtual_time.h>
00043 
00044 
00045 int OrbisQuartus::_init()
00046 {
00047         pthread_mutex_init(&sim_mutex, NULL);
00048 }
00049 
00050 
00051 
00052 int OrbisQuartus::_get_config(int argc, char *argv[])
00053 {
00054         int i, ctl_sock, numbytes, recvd_bytes, flag = 0;
00055         int assign_len, cfg_len;
00056         char len_str[INTEGER_STRING];
00057         char *assignmt, *assgn, *sptr, *config;
00058 
00059         MPI_Initialized(&flag);
00060         if (flag == 0) {
00061                 MPI_Init(&argc, &argv);
00062                 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
00063                 MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
00064         }
00065         MPI_Comm_socket(MPI_COMM_WORLD, &ctl_sock);
00066 
00067         /* block until assignment arrives */
00068         if ((numbytes = recv(ctl_sock, len_str, INTEGER_STRING, 0)) < 0)
00069                 return numbytes;
00070         num_mynodes = atoi(len_str);
00071         assign_len = (2 * num_mynodes) - 1;
00072 
00073         assignmt = (char*) malloc(sizeof(char) * (assign_len + 1));
00074         if ((numbytes = recv(ctl_sock, assignmt, assign_len, 0)) < 0)
00075                 return numbytes;
00076 
00077         mynodes = (unsigned long*) malloc(sizeof(unsigned long) * num_mynodes);
00078         assgn = assignmt;
00079         for (i = 0; i < num_mynodes && 
00080                      (sptr = index(assgn, ',')) != NULL; i++) {
00081                 sptr = '\0';
00082                 sscanf(assgn, "%lu", &mynodes[i]);
00083                 assgn = sptr + 1;
00084         }
00085         sscanf(assgn, "%lu", &mynodes[i]);
00086         free(assignmt);
00087 
00088         /* record config */
00089         if ((numbytes = recv(ctl_sock, len_str, INTEGER_STRING, 0)) < 0)
00090                 return numbytes;
00091         cfg_len = atoi(len_str);
00092 
00093         numbytes = 0;
00094         config = (char*) malloc(sizeof(char) * (cfg_len + 1));
00095         while (numbytes < cfg_len) {
00096                 recvd_bytes = recv(ctl_sock, config, cfg_len, 0);
00097                 numbytes += recvd_bytes;
00098         }
00099         cfg.xml_to_cfg(config);
00100         free(config);
00101 
00102         return 0;
00103 }
00104 
00105 
00106 void OrbisQuartus::stop(void)
00107 {
00108         daemon_quit();
00109         pthread_join(sim_thread, NULL);
00110         pthread_join(vt_thread, NULL);
00111         pthread_join(ctl_thread, NULL);
00112         MPI_Finalize();
00113 }


© 2007, Los Alamos National Security, LLC.