asignal_handlers.c 215 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012
  1. #include "signal_handlers.h"
  2. //extern int *pid_num;
  3. extern int num_idags, node_id ,my_idag;//, fd_log;
  4. extern FILE *log_file, *app_log_file;
  5. extern core_states state, pending_state;
  6. extern inter_list **core_inter_head,**core_inter_tail,*init_pending_head, *init_pending_tail;
  7. extern app my_app;
  8. extern app init_app;
  9. extern app far_req_app;
  10. extern metrics my_stats, total_stats;
  11. extern int far_req_or_sender;//far_req_max_man_cores, far_req_max_man, far_req_max_man_count,
  12. extern int *idag_mask, *idag_id_arr;
  13. extern int *Cl_x_max_arr, *Cl_y_max_arr;
  14. extern int DDS_count, my_cores_count;
  15. extern DDS_list *DDS, *DDS_tail;
  16. extern core_list *my_cores, *my_cores_tail;
  17. extern offer_list *init_man_offers, *selfopt_man_offers;
  18. extern offer_list *far_man_offers, *far_man_offers_tail;
  19. extern int far_list_count, far_man_agent_count;
  20. extern int my_agent, time_worked;
  21. extern int debug_global;
  22. extern time_t cur_time;
  23. extern struct tm *cur_t;
  24. extern struct sigevent sev;
  25. extern struct itimerspec its, chk_timer;
  26. extern timer_t timerid;
  27. extern long int selfopt_time_rem;//-1 means it is not set
  28. extern long int upper_work_bound;
  29. extern int time_for_farman;
  30. extern char scen_num[4];
  31. extern int chk_rem_count, chk_rem_num, sum_rem_time;
  32. extern float old_Speedup, my_Speedup;
  33. extern int nodes_ended_cnt, app_terminated, nodes_initialised, stats_replied;
  34. extern int init_DDS_replies, selfopt_DDS_replies, init_DDS_idags, selfopt_DDS_idags, selfopt_interval, init_idags_areas_replies;//, init_near_areas_num;
  35. extern target_list *init_targets_head, *init_targets_tail;
  36. extern target_list *selfopt_targets_head, *selfopt_targets_tail;
  37. extern application_states app_state;
  38. extern int *sig_array, *data_array, NUES, idags_replied;
  39. extern RCCE_FLAG flag_signals_enabled,flag_data_written;
  40. /*void send_init_reqs (int sender_id) {
  41. int agent_id, i;
  42. inter_list *tmp_inter_list;
  43. //core_list *tmp_cores_list;
  44. target_list *tmp_target_list;
  45. //offer_list *tmp_offer_list;
  46. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  47. agent_id = tmp_target_list->target;
  48. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  49. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  50. if (tmp_inter_list == NULL) {
  51. if (core_inter_head[agent_id] == NULL){
  52. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  53. core_inter_tail[agent_id] = core_inter_head[agent_id];
  54. } else {
  55. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  56. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  57. }
  58. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  59. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  60. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  61. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  62. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  63. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  64. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  65. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  66. }
  67. fprintf(log_file,"\n");
  68. fflush(log_file);
  69. core_inter_tail[agent_id]->next = NULL;
  70. //kill(pid_num[agent_id], SIG_REQ_CORES);
  71. if (core_inter_head[agent_id]->next == NULL) {
  72. kill(pid_num[agent_id], SIG_REQ_CORES);
  73. my_stats.msg_count++;
  74. my_stats.distance += distance(node_id,agent_id);
  75. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  76. kill(pid_num[agent_id], SIG_REQ_CORES);
  77. my_stats.msg_count++;
  78. my_stats.distance += distance(node_id,agent_id);
  79. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  80. } else
  81. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  82. }
  83. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  84. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error412\n");
  85. }
  86. void send_selfopt_reqs (int sender_id) {
  87. int agent_id, i;
  88. //inter_list *tmp_inter_list;
  89. //core_list *tmp_cores_list;
  90. target_list *tmp_target_list;
  91. //offer_list *tmp_offer_list;
  92. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  93. agent_id = tmp_target_list->target;
  94. if (core_inter_head[agent_id] == NULL){
  95. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  96. core_inter_tail[agent_id] = core_inter_head[agent_id];
  97. } else {
  98. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  99. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  100. }
  101. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  102. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  103. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  104. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  105. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  106. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  107. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  108. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  109. }
  110. fprintf(log_file,"\n");
  111. fflush(log_file);
  112. core_inter_tail[agent_id]->next = NULL;
  113. if (core_inter_head[agent_id]->next == NULL) {
  114. kill(pid_num[agent_id], SIG_REQ_CORES);
  115. my_stats.msg_count++;
  116. my_stats.distance += distance(node_id,agent_id);
  117. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  118. kill(pid_num[agent_id], SIG_REQ_CORES);
  119. my_stats.msg_count++;
  120. my_stats.distance += distance(node_id,agent_id);
  121. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  122. }
  123. }
  124. void sig_SEGV_handler(int signo, siginfo_t *info, void *context){
  125. signals_disable();
  126. cur_time = time(NULL);
  127. cur_t = localtime(&cur_time);
  128. fprintf(log_file, "[%d:%d:%d]: Segmentation fault\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  129. fflush(log_file);
  130. fflush(stderr);
  131. fflush(stdout);
  132. signals_enable();
  133. fclose(log_file);
  134. exit(1);
  135. }*/
  136. void sig_TERMINATE_handler(int sender_id)
  137. {
  138. //metrics some_stats;
  139. inter_list *tmp_inter_list;
  140. int data_array_local[LINE_SIZE];
  141. int i, error, str_len;
  142. //RCCE_FLAG_STATUS receiver_status;
  143. char error_str[64];
  144. signals_disable();
  145. cur_time = time(NULL);
  146. cur_t = localtime(&cur_time);
  147. fprintf(log_file, "[%d:%d:%d]: I entered sig_TERMINATE_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  148. fflush(log_file);
  149. if (my_idag != -1 && core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS)
  150. while (core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS) {
  151. fprintf(log_file,"I am still doing smth with my agent %d interaction = %d\n",sender_id,core_inter_head[sender_id]->type);
  152. fflush(log_file);
  153. tmp_inter_list = core_inter_head[sender_id];
  154. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  155. free(tmp_inter_list);
  156. }
  157. if (core_inter_head[sender_id] == NULL){ //edw tha mpei otan exw allaksei agent
  158. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  159. core_inter_tail[sender_id] = core_inter_head[sender_id];
  160. core_inter_tail[sender_id]->type = REP_STATISTICS;
  161. core_inter_tail[sender_id]->next = NULL;
  162. if (my_idag != -1) {
  163. core_inter_tail[sender_id]->data.stats = my_stats;
  164. //kill(info->si_pid, SIG_TERMINATE);
  165. scc_kill(sender_id, SIG_TERMINATE);
  166. } else
  167. state = IDAG_ENDING;
  168. } else if (core_inter_head[sender_id]->type == TERMINATION_STATS) {
  169. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  170. scc_kill(sender_id, SIG_ACK);
  171. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  172. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  173. if (error != RCCE_SUCCESS) {
  174. RCCE_error_string(error, error_str, &str_len);
  175. fprintf(log_file,"I got an error in get data in sig_TERMINATE_handler from %d with descr %s\n",sender_id,error_str);
  176. fflush(log_file);
  177. }
  178. total_stats.msg_count += data_array_local[0];//some_stats.msg_count;
  179. total_stats.message_size += data_array_local[1];//some_stats.message_size;
  180. total_stats.distance += data_array_local[2];//some_stats.distance;
  181. total_stats.app_turnaround += data_array_local[3];//some_stats.app_turnaround;
  182. total_stats.comp_effort += data_array_local[4];//some_stats.comp_effort;
  183. total_stats.cores_utilized += data_array_local[5];//some_stats.cores_utilized;
  184. total_stats.times_accessed += data_array_local[6];//some_stats.times_accessed;
  185. stats_replied++;
  186. fprintf(log_file,"My node %d replied stats stats_replied = %d my_cores_count = %d msg_count=%d\n",sender_id,stats_replied,my_cores_count,data_array_local[0]);
  187. fflush(log_file);
  188. tmp_inter_list = core_inter_head[sender_id];
  189. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  190. free(tmp_inter_list);
  191. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  192. else printf("I am %d in sig_terminate and after free i am still doing smth with my agent interaction = %d\n",node_id,core_inter_head[sender_id]->type);
  193. } else printf("I am %d in sig_terminate and i am still doing smth with my agent %d interaction = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  194. cur_time = time(NULL);
  195. cur_t = localtime(&cur_time);
  196. fprintf(log_file, "[%d:%d:%d]: I ended sig_TERMINATE_handler with sender = %d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  197. fflush(log_file);
  198. signals_enable();
  199. }
  200. void sig_INIT_APP_handler(int sender_id)
  201. {
  202. int data_array_local[LINE_SIZE];
  203. int i, error, str_len;
  204. //RCCE_FLAG_STATUS receiver_status;
  205. char error_str[64];
  206. signals_disable();
  207. inter_list *tmp_inter_list, *tmp_inter_prev;
  208. cur_time = time(NULL);
  209. cur_t = localtime(&cur_time);
  210. fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_APP_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  211. fflush(log_file);
  212. if (node_id == 0) {
  213. tmp_inter_prev = NULL;
  214. for (tmp_inter_list = init_pending_head; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next) {
  215. if (tmp_inter_list->data.new_app.num_of_cores == sender_id) break;
  216. tmp_inter_prev = tmp_inter_list;
  217. }
  218. if (tmp_inter_list != NULL) {
  219. fprintf(log_file,"I am sending an aborted init_app\n");
  220. fflush(log_file);
  221. if (core_inter_head[sender_id] == NULL){
  222. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  223. core_inter_tail[sender_id] = core_inter_head[sender_id];
  224. } else {
  225. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  226. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  227. }
  228. core_inter_tail[sender_id]->type = INIT_APP;
  229. core_inter_tail[sender_id]->data.new_app = tmp_inter_list->data.new_app;
  230. core_inter_tail[sender_id]->data.new_app.num_of_cores = 0;
  231. core_inter_tail[sender_id]->next = NULL;
  232. if (core_inter_head[sender_id]->next == NULL) {
  233. //kill(pid_num[sender_id],SIG_INIT_APP);
  234. scc_kill(sender_id,SIG_INIT_APP);
  235. my_stats.msg_count++;
  236. my_stats.distance += distance(node_id,sender_id);
  237. }
  238. if (tmp_inter_prev == NULL) {
  239. init_pending_head = init_pending_head->next;
  240. //free(tmp_inter_list);
  241. } else {
  242. tmp_inter_prev->next = tmp_inter_list->next;
  243. if (tmp_inter_prev->next == NULL) init_pending_tail = tmp_inter_prev;
  244. }
  245. free(tmp_inter_list);
  246. }
  247. } else if (state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == IDLE_INIT_MAN || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  248. //printf("I have to reject sig_INIT_APP sender_id=%d node_id=%d state=%d\n",sender_id,node_id,state);
  249. fprintf(log_file,"I have to reject sig_INIT_APP sender_id=%d\n",sender_id);
  250. fflush(log_file);
  251. scc_kill(sender_id, SIG_REJECT);
  252. //kill(info->si_pid, SIG_REJECT);
  253. my_stats.msg_count++;
  254. my_stats.distance += distance(node_id,sender_id);
  255. } else {
  256. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  257. scc_kill(sender_id, SIG_ACK);
  258. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  259. //scc_kill(sender_id, SIG_ACK);
  260. my_stats.msg_count++;
  261. my_stats.distance += distance(node_id,sender_id);
  262. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  263. if (error != RCCE_SUCCESS) {
  264. RCCE_error_string(error, error_str, &str_len);
  265. fprintf(log_file,"I got an error in get data in sig_INIT_APP from %d with descr %s\n",sender_id,error_str);
  266. fflush(log_file);
  267. }
  268. init_app.id = data_array_local[0];
  269. memcpy(&init_app.A,&data_array_local[1],sizeof(int));
  270. memcpy(&init_app.var,&data_array_local[2],sizeof(int));
  271. memcpy(&init_app.workld,&data_array_local[3],sizeof(int));
  272. init_app.num_of_cores = data_array_local[4];
  273. //read(fd_r, &init_app, sizeof(app));
  274. fprintf(log_file,"My app is A = %f, var = %f, cores = %d\n",init_app.A,init_app.var,init_app.num_of_cores);
  275. fflush(log_file);
  276. if (state == IDLE_AGENT_WAITING_OFF || state == AGENT_SELF_CHK_OFFERS || state == AGENT_ZOMBIE || state == AGENT_ENDING)
  277. pending_state = INIT_MANAGER;
  278. else if (state == IDLE_AGENT) {
  279. if (timer_gettime(timerid, &chk_timer) == -1) perror("timer_gettime error init\n");
  280. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  281. its.it_value.tv_nsec = 0;
  282. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error init\n");
  283. pending_state = IDLE_AGENT;
  284. state = INIT_MANAGER;
  285. } else if (state == WORKING_NODE || state == AGENT_SELF_OPT) {
  286. pending_state = state;
  287. state = INIT_MANAGER;
  288. } else state = INIT_MANAGER;
  289. }
  290. cur_time = time(NULL);
  291. cur_t = localtime(&cur_time);
  292. fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_APP_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  293. fflush(log_file);
  294. signals_enable();
  295. }
  296. void sig_TIMER_handler(int signo, siginfo_t *info, void *context)
  297. {
  298. cur_time = time(NULL);
  299. cur_t = localtime(&cur_time);
  300. fprintf(log_file, "[%d:%d:%d]: Alarm went off state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,state,pending_state);
  301. fflush(log_file);
  302. if (state == IDLE_INIT_MAN) state = INIT_MAN_CHK_OFFERS;
  303. else if (state == IDLE_FAR_MAN) state = FAR_MAN_CHK_OFFERS;
  304. else if (state == IDLE_AGENT_WAITING_OFF) state = AGENT_SELF_CHK_OFFERS;
  305. else if (state == IDLE_AGENT) state = AGENT_SELF_OPT;
  306. else if (state == IDLE_CHK_APP_FILE) state = CHK_APP_FILE;
  307. else if (state == IDLE_INIT_MAN_SELFOPT_PENDING) {
  308. state = INIT_MAN_CHK_OFFERS_SELFOPT_PENDING;
  309. //printf("I am %d and ths giwrgria ths aresei na spermangarei k na mastourbarei\n",node_id);
  310. }
  311. else if (state == IDLE_INIT_MAN_WORK_PENDING) {
  312. state = INIT_MAN_CHK_OFFERS_WORK_PENDING;
  313. //printf("I am %d and poutana thalassa\n",node_id);
  314. }
  315. else if (state == WORKING_NODE_IDLE_INIT) {
  316. state = INIT_MAN_CHK_OFFERS;
  317. pending_state = WORKING_NODE;
  318. //printf("I am %d and poutana thalassa\n",node_id);
  319. }
  320. else if (state == AGENT_INIT_STATE) {
  321. if (pending_state == IDLE_INIT_MAN) pending_state = INIT_MAN_CHK_OFFERS;
  322. }
  323. else printf("i am %d, timer went off and i don't know what to do. My state is %d\n",node_id,state);
  324. cur_time = time(NULL);
  325. cur_t = localtime(&cur_time);
  326. fprintf(log_file, "[%d:%d:%d]: End of timer handler state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,state,pending_state);
  327. fflush(log_file);
  328. }
  329. void sig_INIT_handler(int sender_id)
  330. {
  331. int data_array_local[LINE_SIZE];
  332. int error, str_len;
  333. //RCCE_FLAG_STATUS receiver_status;
  334. char error_str[64];
  335. signals_disable();
  336. sender_id = get_id_from_pid(info->si_pid);
  337. //kill(info->si_pid, SIG_ACK);
  338. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  339. scc_kill(sender_id, SIG_ACK);
  340. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  341. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  342. if (error != RCCE_SUCCESS) {
  343. RCCE_error_string(error, error_str, &str_len);
  344. printf("I am %d and i got an error in get data in sig_INIT from %d with descr %s\n",node_id,sender_id,error_str);
  345. } else my_idag = data_array_local[0];
  346. my_stats.msg_count++; //gia to sig_ACK
  347. my_stats.distance += distance(node_id,my_idag);
  348. printf("I am node with id %d my idle agent is %d\n",node_id,my_idag);
  349. cur_time = time(NULL);
  350. cur_t = localtime(&cur_time);
  351. fprintf(log_file, "[%d:%d:%d]: my idle agent is %d and my pid is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_idag,getpid());
  352. fflush(log_file);
  353. signals_enable();
  354. }
  355. void sig_ACK_handler(int sender_id)
  356. {
  357. int clear=1, i, j, data_array_local[3 * LINE_SIZE];;//, fd_r;//fd_r, ; clear is 1 if node is to be removed
  358. inter_list *tmp_inter_list;
  359. DDS_list *tmp_DDS;//, *tmp_inter_prev=NULL;
  360. offer_list *tmp_offer_list;
  361. int error, str_len;
  362. //RCCE_FLAG_STATUS receiver_status;
  363. char error_str[64];
  364. signals_disable();
  365. cur_time = time(NULL);
  366. cur_t = localtime(&cur_time);
  367. fprintf(log_file, "[%d:%d:%d]: I entered sig_ACK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  368. fflush(log_file);
  369. if (core_inter_head[sender_id] == NULL) {
  370. fprintf(log_file,"We were fucked in sig_ACK! sender_id = %d\n",sender_id);
  371. fflush(log_file);
  372. } else {
  373. fprintf(log_file, "Type=%d\n",core_inter_head[sender_id]->type);
  374. fflush(log_file);
  375. tmp_inter_list = core_inter_head[sender_id];
  376. if (tmp_inter_list->type == INIT_CORE){
  377. data_array_local[0] = node_id;
  378. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  379. if (error != RCCE_SUCCESS) {
  380. RCCE_error_string(error, error_str, &str_len);
  381. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  382. }
  383. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  384. fprintf(log_file, "node_id=%d size=%d write_res=%d\n",node_id,sizeof(int),write_res);
  385. fflush(log_file);
  386. my_stats.message_size += sizeof(int);
  387. nodes_initialised++;
  388. } else if (tmp_inter_list->type == INIT_APP){
  389. //write_res = write(fd_w, &tmp_inter_list->data.new_app, sizeof(app));
  390. //fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",tmp_inter_list->data.new_app.A,sizeof(app),write_res);
  391. //fflush(log_file);
  392. data_array_local[0] = tmp_inter_list->data.new_app.id;
  393. memcpy(&data_array_local[1],&tmp_inter_list->data.new_app.A,sizeof(int));
  394. memcpy(&data_array_local[2],&tmp_inter_list->data.new_app.var,sizeof(int));
  395. memcpy(&data_array_local[3],&tmp_inter_list->data.new_app.workld,sizeof(int));
  396. data_array_local[4] = tmp_inter_list->data.new_app.num_of_cores;
  397. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  398. if (error != RCCE_SUCCESS) {
  399. RCCE_error_string(error, error_str, &str_len);
  400. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  401. }
  402. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  403. my_stats.message_size += sizeof(app);
  404. } else if (tmp_inter_list->type == IDAG_FIND_IDAGS_PENDING || tmp_inter_list->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) {//I am the requesting common node
  405. data_array_local[0] = tmp_inter_list->data.reg.C;
  406. data_array_local[1] = tmp_inter_list->data.reg.r;
  407. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  408. if (error != RCCE_SUCCESS) {
  409. RCCE_error_string(error, error_str, &str_len);
  410. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  411. }
  412. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  413. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  414. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  415. fflush(log_file);
  416. if (tmp_inter_list->type == IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = IDAG_FIND_IDAGS;
  417. else if (tmp_inter_list->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = SELFOPT_IDAG_FIND_IDAGS;
  418. my_stats.message_size += sizeof(region);
  419. clear = 0;
  420. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  421. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){
  422. data_array_local[0] = tmp_inter_list->data.reg.C;
  423. data_array_local[1] = tmp_inter_list->data.reg.r;
  424. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  425. if (error != RCCE_SUCCESS) {
  426. RCCE_error_string(error, error_str, &str_len);
  427. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  428. }
  429. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  430. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  431. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  432. fflush(log_file);
  433. my_stats.message_size += sizeof(region);
  434. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = IDAG_REQ_DDS;
  435. else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = FAR_REQ_IDAG_REQ_DDS;
  436. else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = SELFOPT_IDAG_REQ_DDS;
  437. clear = 0;
  438. } else if (tmp_inter_list->type == REP_IDAG_FIND_IDAGS) {//I am the idag
  439. data_array_local[0] = tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y];
  440. fprintf(log_file, "num_of_idags=%d\n",tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y]);
  441. fflush(log_file);
  442. my_stats.message_size += sizeof(int);
  443. j=1;
  444. for (i=0; i<num_idags_x*num_idags_y; i++)
  445. if (tmp_inter_list->data.idags_in_reg[i]) {
  446. //write_res = write(fd_w, &idag_id_arr[i], sizeof(int)); //matching of increasing number to node_id
  447. data_array_local[j] = idag_id_arr[i];
  448. j++;
  449. fprintf(log_file, "idag=%d\n",idag_id_arr[i]);
  450. fflush(log_file);
  451. my_stats.message_size += sizeof(int);
  452. }
  453. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  454. if (error != RCCE_SUCCESS) {
  455. RCCE_error_string(error, error_str, &str_len);
  456. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  457. }
  458. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  459. } else if (tmp_inter_list->type == REP_IDAG_REQ_DDS) {//I am the idag
  460. if (tmp_inter_list->data.agents_in_reg == NULL) {//debugging
  461. fprintf(log_file, "In null rep_idag_dds with sender %d and DDS_count %d\n",sender_id,DDS_count);
  462. fflush(log_file);
  463. data_array_local[0] = DDS_count;
  464. data_array_local[1] = 0;
  465. i=2;
  466. tmp_DDS = DDS;
  467. while(tmp_DDS != NULL){
  468. //write_res = write(fd_w, &tmp_DDS->agent_id, sizeof(int));
  469. //write_res = write(fd_w, &tmp_DDS->num_of_cores, sizeof(int));
  470. data_array_local[i++] = tmp_DDS->agent_id;
  471. data_array_local[i++] = tmp_DDS->num_of_cores;
  472. tmp_DDS = tmp_DDS->next;
  473. }
  474. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  475. if (error != RCCE_SUCCESS) {
  476. RCCE_error_string(error, error_str, &str_len);
  477. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  478. fflush(log_file);
  479. }
  480. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  481. } else {
  482. data_array_local[0] = tmp_inter_list->data.agents_in_reg[0];
  483. data_array_local[1] = 0;
  484. fprintf(log_file, "num_of_agents=%d\n",tmp_inter_list->data.agents_in_reg[0]);
  485. fflush(log_file);
  486. my_stats.message_size += sizeof(int);
  487. j=2;
  488. for (i=1; i<=2*tmp_inter_list->data.agents_in_reg[0]; i+=2){
  489. /*write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i], sizeof(int));
  490. fprintf(log_file, "agent=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i],sizeof(int),write_res);
  491. fflush(log_file);
  492. write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i+1], sizeof(int));//offset may be different!!!!!!
  493. fprintf(log_file, "cores=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i+1],sizeof(int),write_res);
  494. fflush(log_file);*/
  495. fprintf(log_file, "agent=%d cores=%d\n",tmp_inter_list->data.agents_in_reg[i],tmp_inter_list->data.agents_in_reg[i+1]);
  496. fflush(log_file);
  497. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i];
  498. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i+1];
  499. my_stats.message_size += 2 * sizeof(int);
  500. }
  501. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  502. if (error != RCCE_SUCCESS) {
  503. RCCE_error_string(error, error_str, &str_len);
  504. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  505. fflush(log_file);
  506. }
  507. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  508. }
  509. } else if (tmp_inter_list->type == AGENT_REQ_CORES_PENDING) {
  510. fprintf(log_file, "A=%0.2f\n",init_app.A);
  511. fflush(log_file);
  512. my_stats.message_size += sizeof(app);
  513. data_array_local[0] = init_app.id;
  514. memcpy(&data_array_local[1],&init_app.A,sizeof(int));
  515. memcpy(&data_array_local[2],&init_app.var,sizeof(int));
  516. memcpy(&data_array_local[3],&init_app.workld,sizeof(int));
  517. data_array_local[4] = init_app.num_of_cores;
  518. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  519. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  520. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  521. fflush(log_file);
  522. if (tmp_inter_list->data.reg_arr.num_of_regions > 1) {
  523. fprintf(log_file, "In init ack i have num_of_regions = %d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  524. fflush(log_file);
  525. }
  526. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  527. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  528. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg_arr.region_arr[i].C,tmp_inter_list->data.reg_arr.region_arr[i].r);
  529. fflush(log_file);
  530. data_array_local[6] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  531. data_array_local[7] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  532. my_stats.message_size += sizeof(region);
  533. }
  534. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  535. if (error != RCCE_SUCCESS) {
  536. RCCE_error_string(error, error_str, &str_len);
  537. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  538. fflush(log_file);
  539. }
  540. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  541. tmp_inter_list->type = AGENT_REQ_CORES;
  542. free(tmp_inter_list->data.reg_arr.region_arr);
  543. clear = 0;
  544. } else if (tmp_inter_list->type == SELFOPT_REQ_CORES_PENDING) {
  545. fprintf(log_file, "A=%0.2f num_of_cores = %d\n",my_app.A,my_app.num_of_cores);
  546. fflush(log_file);
  547. my_stats.message_size += sizeof(app);
  548. data_array_local[0] = my_app.id;
  549. memcpy(&data_array_local[1],&my_app.A,sizeof(int));
  550. memcpy(&data_array_local[2],&my_app.var,sizeof(int));
  551. memcpy(&data_array_local[3],&my_app.workld,sizeof(int));
  552. data_array_local[4] = my_app.num_of_cores;
  553. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  554. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  555. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  556. fflush(log_file);
  557. if (tmp_inter_list->data.reg_arr.num_of_regions > 1) {
  558. fprintf(log_file, "In selfopt ack i have num_of_regions = %d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  559. fflush(log_file);
  560. }
  561. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  562. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  563. data_array_local[6] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  564. data_array_local[7] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  565. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg_arr.region_arr[i].C,tmp_inter_list->data.reg_arr.region_arr[i].r);
  566. fflush(log_file);
  567. my_stats.message_size += sizeof(region);
  568. }
  569. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  570. if (error != RCCE_SUCCESS) {
  571. RCCE_error_string(error, error_str, &str_len);
  572. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  573. fflush(log_file);
  574. }
  575. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  576. tmp_inter_list->type = SELFOPT_REQ_CORES;
  577. free(tmp_inter_list->data.reg_arr.region_arr);
  578. clear = 0;
  579. /*} else if (tmp_inter_list->type == FAR_REQ_CORES_PENDING) {//I am the requesting common node
  580. //printf("I am common node inside with sender_id %d and %s\n",sender_id,fifo_name);
  581. //if (tmp_inter_list->type == AGENT_REQ_CORES) j
  582. //else
  583. //if (tmp_inter_list->type == FAR_REQ_CORES)
  584. //else write_res = write(fd_w, &my_app, sizeof(app));
  585. write_res = write(fd_w, &far_req_app, sizeof(app));
  586. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",far_req_app.A,sizeof(app),write_res);
  587. fflush(log_file);
  588. my_stats.message_size += sizeof(app);
  589. i = 1;
  590. write_res = write(fd_w, &i, sizeof(int));
  591. fprintf(log_file, "num_of_regions=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  592. fflush(log_file);
  593. my_stats.message_size += sizeof(int);
  594. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  595. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r,sizeof(region),write_res);
  596. fflush(log_file);
  597. my_stats.message_size += sizeof(region);
  598. tmp_inter_list->type = FAR_REQ_CORES;
  599. clear = 0;*/
  600. } else if (tmp_inter_list->type == REP_AGENT_REQ_CORES) {//I am the agent
  601. //printf("I am common node inside with sender_id %d and %s\n",sender_id,fifo_name);
  602. write_res = write(fd_w, &tmp_inter_list->data.off_arr.num_of_offers, sizeof(int));
  603. fprintf(log_file, "num_of_offers=%d size=%d write_res=%d\n",tmp_inter_list->data.off_arr.num_of_offers,sizeof(int),write_res);
  604. fflush(log_file);
  605. my_stats.message_size += sizeof(int);
  606. for (j=0; j<tmp_inter_list->data.off_arr.num_of_offers; j++){
  607. //printf("I am %d and in ack i offer %d cores\n",node_id,tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores);
  608. write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores, sizeof(int));
  609. fprintf(log_file, "num_of_cores=%d size=%d write_res=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores,sizeof(int),write_res);
  610. fflush(log_file);
  611. my_stats.message_size += sizeof(int);
  612. write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].spd_loss, sizeof(float));
  613. fprintf(log_file, "spd_loss=%0.2f size=%d write_res=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].spd_loss,sizeof(float),write_res);
  614. fflush(log_file);
  615. my_stats.message_size += sizeof(float);
  616. for (i=0; i<tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores; i++) {
  617. //printf ("Offering core = %d\n",tmp_inter_list->data.my_offer.offered_cores[i]);
  618. write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i], sizeof(int));
  619. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i],sizeof(int),write_res);
  620. fflush(log_file);
  621. //printf("I am %d and in here i offeres %d\n",node_id,tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i]);
  622. my_stats.message_size += sizeof(int);
  623. }
  624. }
  625. core_inter_head[sender_id]->type = AGENT_OFFER_SENT;
  626. /*free(tmp_inter_list->data.my_offer.offered_cores);
  627. tmp_inter_list = core_inter_head[sender_id]->next;
  628. free(core_inter_head[sender_id]);
  629. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  630. core_inter_head[sender_id]->next = tmp_inter_list;
  631. core_inter_head[sender_id]->type = AGENT_OFFER_SENT;*///kalutera na krataw thn prosfora gia na exw tous purhnes
  632. //core_inter_head[sender_id]->data.offer_accepted = -1;
  633. clear = 0;
  634. } else if (tmp_inter_list->type == REP_AGENT_OFFER_SENT) {
  635. fprintf(log_file, "I have to reply %d for %d offers\n",sender_id,tmp_inter_list->data.offer_acc_array[0]);
  636. fflush(log_file);
  637. my_stats.message_size += sizeof(int);
  638. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++){
  639. //printf("offer answer %d is %d\n",i,tmp_inter_list->data.offer_acc_array[i]);
  640. write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  641. fprintf(log_file, "offer_ans=%d size=%d write_res=%d\n",tmp_inter_list->data.offer_acc_array[i],sizeof(int),write_res);
  642. fflush(log_file);
  643. //fprintf(log_file, "I replied %d with %d\n",sender_id,tmp_inter_list->data.offer_acc_array[i]);
  644. my_stats.message_size += sizeof(int);
  645. }
  646. fflush(log_file);
  647. free(tmp_inter_list->data.offer_acc_array);
  648. //write_res = write(fd_w, &tmp_inter_list->data.offer_accepted, sizeof(int));
  649. } else if (tmp_inter_list->type == INIT_AGENT) {
  650. write_res = write(fd_w, &init_app, sizeof(app));
  651. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",init_app.A,sizeof(app),write_res);
  652. fflush(log_file);
  653. my_stats.message_size += sizeof(app);
  654. //printf("poutanes %d\n",init_app.num_of_cores);
  655. for (i=1; i<=init_app.num_of_cores; i++){
  656. //printf("karioles %d\n",tmp_inter_list->data.app_cores[i]);
  657. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  658. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  659. fflush(log_file);
  660. my_stats.message_size += sizeof(int);
  661. }
  662. init_app.A=-1.0;
  663. init_app.var=-1.0;
  664. init_app.num_of_cores=-1;
  665. kill(pid_num[0], SIG_INIT_APP);
  666. my_stats.msg_count++;
  667. my_stats.distance += distance(node_id,0);
  668. free(tmp_inter_list->data.app_cores);
  669. } else if (tmp_inter_list->type == IDAG_ADD_CORES_DDS || tmp_inter_list->type == IDAG_REM_CORES_DDS) {// || tmp_inter_list->type == REMOVE_APP
  670. //fprintf(log_file, "I am in add/remove/remove_app to %d with %d cores\n",sender_id,tmp_inter_list->data.app_cores[0]);
  671. //fflush(log_file);
  672. write_res = write(fd_w, &tmp_inter_list->data.app_cores[0], sizeof(int));
  673. fprintf(log_file, "app_cores=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[0],sizeof(int),write_res);
  674. fflush(log_file);
  675. my_stats.message_size += sizeof(int);
  676. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  677. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  678. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  679. fflush(log_file);
  680. my_stats.message_size += sizeof(int);
  681. }
  682. //I am an idag and i have to send to other idags my original sender
  683. if (my_idag == -1) {//idag_id != -1
  684. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  685. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  686. fflush(log_file);
  687. my_stats.message_size += sizeof(int);
  688. }
  689. } else if (tmp_inter_list->type == REMOVE_APP) {
  690. //fprintf(log_file, "I am in add/remove/remove_app to %d with %d cores\n",sender_id,tmp_inter_list->data.app_cores[0]);
  691. //fflush(log_file);
  692. if (my_idag != -1) {
  693. write_res = write(fd_w, &tmp_inter_list->data.app_cores[0], sizeof(int));
  694. fprintf(log_file, "app_cores=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[0],sizeof(int),write_res);
  695. fflush(log_file);
  696. my_stats.message_size += sizeof(int);
  697. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  698. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  699. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  700. fflush(log_file);
  701. my_stats.message_size += sizeof(int);
  702. }
  703. } else {
  704. //I am an idag and i have to send to other idags my original sender
  705. write_res = write(fd_w, &tmp_inter_list->data.agent_ended, sizeof(int));
  706. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.agent_ended,sizeof(int),write_res);
  707. fflush(log_file);
  708. my_stats.message_size += sizeof(int);
  709. }
  710. } else if (tmp_inter_list->type == FAR_INIT_REQ) {//I am the requesting common node
  711. write_res = write(fd_w, &init_app, sizeof(app));
  712. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",init_app.A,sizeof(app),write_res);
  713. fflush(log_file);
  714. my_stats.message_size += sizeof(app);
  715. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  716. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r,sizeof(region),write_res);
  717. fflush(log_file);
  718. my_stats.message_size += sizeof(region);
  719. clear = 0;
  720. } else if (tmp_inter_list->type == FAR_REQ_MAN_APPOINT) {//I am the requesting common node
  721. //fprintf(log_file, "I am appointing %d for far manager of %d with region (%d,%d)\n",
  722. // sender_id,tmp_inter_list->data.far_req.orig_sender,tmp_inter_list->data.far_req.reg.C,tmp_inter_list->data.far_req.reg.r);
  723. //fflush(log_file);
  724. write_res = write(fd_w, &tmp_inter_list->data.far_req.orig_sender, sizeof(int));
  725. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req.orig_sender,sizeof(int),write_res);
  726. fflush(log_file);
  727. my_stats.message_size += sizeof(int);
  728. write_res = write(fd_w, &tmp_inter_list->data.far_req.far_app, sizeof(app));
  729. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",tmp_inter_list->data.far_req.far_app.A,sizeof(app),write_res);
  730. fflush(log_file);
  731. my_stats.message_size += sizeof(app);
  732. write_res = write(fd_w, &tmp_inter_list->data.far_req.reg, sizeof(region));
  733. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req.reg.C,tmp_inter_list->data.far_req.reg.r,sizeof(region),write_res);
  734. fflush(log_file);
  735. my_stats.message_size += sizeof(region);
  736. if (tmp_inter_list->next != NULL && tmp_inter_list->next->type == FAR_REQ_MAN_APPOINT) { //gia na thrhthei h seira k na mhn skaei to apo katw
  737. core_inter_head[sender_id] = tmp_inter_list->next;
  738. free(tmp_inter_list);
  739. clear = 0;
  740. }
  741. /*far_req_app.A=-1.0;
  742. far_req_app.var=-1.0;
  743. far_req_app.num_of_cores=-1;
  744. far_req_or_sender = -1;*/
  745. /*far_req_max_man = -1;
  746. far_req_max_man_cores = -1;
  747. far_req_max_man_count = -1;*/
  748. } else if (tmp_inter_list->type == REP_FAR_INIT_REQ){
  749. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  750. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  751. fflush(log_file);
  752. my_stats.message_size += sizeof(int);
  753. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  754. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  755. if (tmp_inter_list->data.far_req_man != node_id && core_inter_head[tmp_inter_list->data.far_req_man] != NULL &&
  756. core_inter_head[tmp_inter_list->data.far_req_man]->type == FAR_REQ_MAN_APPOINT) {//next == NULL) {
  757. kill(pid_num[tmp_inter_list->data.far_req_man], SIG_FAR_REQ);
  758. fprintf(log_file, "I sent it\n");
  759. fflush(log_file);
  760. my_stats.msg_count++;
  761. my_stats.distance += distance(node_id,tmp_inter_list->data.far_req_man);
  762. } else {
  763. fprintf(log_file, "far_man=%d\n",tmp_inter_list->data.far_req_man);
  764. fflush(log_file);
  765. }
  766. } else if (tmp_inter_list->type == ABORT_FAR_MAN){
  767. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  768. fprintf(log_file, "far_req_man=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  769. fflush(log_file);
  770. my_stats.message_size += sizeof(int);
  771. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  772. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  773. } else if (tmp_inter_list->type == FAR_REQ_OFFER) {//I am the agent
  774. //printf("far_list_count = %d\n",far_list_count);
  775. write_res = write(fd_w, &far_list_count, sizeof(int));
  776. fprintf(log_file, "far_list_count=%d size=%d write_res=%d\n",far_list_count,sizeof(int),write_res);
  777. fflush(log_file);
  778. my_stats.message_size += sizeof(int);
  779. tmp_offer_list = far_man_offers;
  780. while (tmp_offer_list != NULL){
  781. write_res = write(fd_w, &tmp_offer_list->off.num_of_cores, sizeof(int));
  782. fprintf(log_file, "num_of_offers=%d size=%d write_res=%d\n",tmp_offer_list->off.num_of_cores,sizeof(int),write_res);
  783. fflush(log_file);
  784. my_stats.message_size += sizeof(int);
  785. write_res = write(fd_w, &tmp_offer_list->off.spd_loss, sizeof(float));
  786. fprintf(log_file, "spd_loss=%0.2f size=%d write_res=%d\n",tmp_offer_list->off.spd_loss,sizeof(float),write_res);
  787. fflush(log_file);
  788. my_stats.message_size += sizeof(float);
  789. for (i=0; i<tmp_offer_list->off.num_of_cores; i++) {
  790. //printf ("Offering core = %d\n",tmp_inter_list->data.my_offer.offered_cores[i]);
  791. write_res = write(fd_w, &tmp_offer_list->off.offered_cores[i], sizeof(int));
  792. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_offer_list->off.offered_cores[i],sizeof(int),write_res);
  793. fflush(log_file);
  794. my_stats.message_size += sizeof(int);
  795. }
  796. tmp_offer_list = tmp_offer_list->next;
  797. }
  798. core_inter_head[sender_id]->type = FAR_REQ_OFFER_SENT;
  799. clear = 0;
  800. } else if (tmp_inter_list->type == REP_FAR_REQ_OFFER_SENT) {
  801. fprintf(log_file, "num_of_offers=%d\n",tmp_inter_list->data.offer_acc_array[0]);
  802. fflush(log_file);
  803. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++) {
  804. write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  805. fprintf(log_file, "offer_ans=%d size=%d write_res=%d\n",tmp_inter_list->data.offer_acc_array[i],sizeof(int),write_res);
  806. fflush(log_file);
  807. my_stats.message_size += sizeof(int);
  808. }
  809. } else if (tmp_inter_list->type == INIT_WORK_NODE) {
  810. if (tmp_inter_list->data.work_time != -1) {
  811. i=1;
  812. write_res = write(fd_w, &i, sizeof(int));
  813. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  814. fflush(log_file);
  815. write_res = write(fd_w, &node_id, sizeof(int));
  816. fprintf(log_file, "node_id=%d size=%d write_res=%d\n",node_id,sizeof(int),write_res);
  817. fflush(log_file);
  818. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  819. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  820. fflush(log_file);
  821. my_stats.message_size += 3 * sizeof(int);
  822. } else {
  823. i=0;
  824. write_res = write(fd_w, &i, sizeof(int));
  825. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  826. fflush(log_file);
  827. my_stats.message_size += sizeof(int);
  828. }
  829. } else if (tmp_inter_list->type == APPOINT_WORK_NODE) {
  830. if (tmp_inter_list->data.work_time != -1) {
  831. i=1;
  832. write_res = write(fd_w, &i, sizeof(int));
  833. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  834. fflush(log_file);
  835. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  836. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  837. fflush(log_file);
  838. my_stats.message_size += 2 * sizeof(int);
  839. } else {
  840. i=0;
  841. write_res = write(fd_w, &i, sizeof(int));
  842. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  843. fflush(log_file);
  844. my_stats.message_size += sizeof(int);
  845. }
  846. } else if (tmp_inter_list->type == REP_CHK_REM_TIME) {
  847. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  848. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  849. fflush(log_file);
  850. my_stats.message_size += sizeof(int);
  851. //if (node_id == 28) printf("sender_id = %d a\n",sender_id);
  852. } else if (tmp_inter_list->type == REP_STATISTICS) {
  853. write_res = write(fd_w, &tmp_inter_list->data.stats, sizeof(metrics));
  854. state = TERMINATED;
  855. //if (node_id == 28) printf("sender_id = %d a\n",sender_id);
  856. } else printf("We were fucked inside ACK! node_id = %d sender_id = %d\n",node_id,sender_id);
  857. //sem_wait(&node_sem[sender_id]);
  858. sem_getvalue(&node_sem_out[node_id],&i);
  859. fprintf(log_file, "In ack Trying to acquire semaphore. Sem value = %d\n",i);
  860. fflush(log_file);
  861. sem_wait(&node_sem_out[node_id]);
  862. sem_getvalue(&node_sem_out[node_id],&i);
  863. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  864. fflush(log_file);
  865. close(fd_w);
  866. free(fifo_name);
  867. //if (node_id == 28) printf("sender_id = %d b\n",sender_id);
  868. if (clear){
  869. core_inter_head[sender_id] = tmp_inter_list->next;
  870. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  871. else send_next_signal(core_inter_head[sender_id],sender_id);
  872. //if (node_id == 28) printf("sender_id = %d c\n",sender_id);
  873. free(tmp_inter_list);
  874. }
  875. }
  876. //if (node_id == 28) printf("sender_id = %d d\n",sender_id);
  877. cur_time = time(NULL);
  878. cur_t = localtime(&cur_time);
  879. fprintf(log_file, "[%d:%d:%d]: I ended sig_ACK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  880. fflush(log_file);
  881. signals_enable();
  882. }
  883. void sig_IDAG_FIND_IDAGS_handler(int signo, siginfo_t *info, void *context)
  884. {
  885. int fd_r=-1, sender_id, num_of_idags, i, one_idag;
  886. char *fifo_name;
  887. region cur_reg;
  888. inter_list *tmp_inter_list;
  889. //printf("I came through FIND_IDAGS!! idag_id = %d, node_id = %d\n",idag_id,node_id);
  890. signals_disable();
  891. sender_id = get_id_from_pid(info->si_pid);
  892. fifo_name = get_pipe_name(node_id);
  893. cur_time = time(NULL);
  894. cur_t = localtime(&cur_time);
  895. fprintf(log_file, "[%d:%d:%d]: I entered sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  896. fflush(log_file);
  897. if (core_inter_head[sender_id] == NULL || core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS){ //I am the idag
  898. sem_getvalue(&node_sem[node_id],&i);
  899. fprintf(log_file, "In 1 Trying to acquire semaphore. Sem value = %d\n",i);
  900. fflush(log_file);
  901. kill(info->si_pid, SIG_ACK);
  902. sem_wait(&node_sem[node_id]);
  903. sem_getvalue(&node_sem[node_id],&i);
  904. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  905. fflush(log_file);
  906. my_stats.msg_count++;
  907. my_stats.distance += distance(node_id,sender_id);
  908. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  909. //printf("open3 idag_id=%d node_id=%d\n",idag_id,node_id);
  910. fd_r = open(fifo_name, O_RDONLY);
  911. if (fd_r == -1) {
  912. perror("opening reading pipe");
  913. signals_enable();
  914. return;
  915. } /*else {
  916. fprintf(log_file, "I went through open\n");
  917. fflush(log_file);
  918. } */
  919. read(fd_r, &cur_reg, sizeof(region));
  920. //printf("asdI am node with id %d idag_id %d and I am to investigate region C=%d r=%d\n",node_id,idag_id,cur_reg.C,cur_reg.r);
  921. if (core_inter_head[sender_id] == NULL){
  922. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  923. core_inter_tail[sender_id] = core_inter_head[sender_id];
  924. } else {
  925. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  926. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  927. }
  928. core_inter_tail[sender_id]->type = REP_IDAG_FIND_IDAGS;
  929. core_inter_tail[sender_id]->data.idags_in_reg = (int *) malloc((num_idags_x*num_idags_y+1)*sizeof(int));
  930. core_inter_tail[sender_id]->next = NULL;
  931. get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
  932. if (core_inter_head[sender_id]->next == NULL) kill(info->si_pid, SIG_IDAG_FIND_IDAGS);
  933. my_stats.msg_count++;
  934. my_stats.distance += distance(node_id,sender_id);
  935. //sem_post(&node_sem[node_id]);
  936. sem_getvalue(&node_sem_out[sender_id],&i);
  937. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  938. fflush(log_file);
  939. if (sem_post(&node_sem_out[sender_id]) == -1){
  940. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  941. perror("sem_post");
  942. }
  943. sem_getvalue(&node_sem_out[sender_id],&i);
  944. fprintf(log_file, "Sem value = %d\n",i);
  945. fflush(log_file);
  946. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  947. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  948. printf("I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  949. fprintf(log_file,"I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  950. fflush(log_file);
  951. kill(info->si_pid, SIG_REJECT);
  952. my_stats.msg_count++;
  953. my_stats.distance += distance(node_id,sender_id);
  954. /*kill(info->si_pid, SIG_ACK);
  955. my_stats.msg_count++;
  956. my_stats.distance += distance(node_id,sender_id);
  957. fd_r = open(fifo_name, O_RDONLY);
  958. if (fd_r == -1) {
  959. perror("opening reading pipe");
  960. signals_enable();
  961. return;
  962. }
  963. read(fd_r, &num_of_idags, sizeof(int));
  964. for (i=0; i<num_of_idags; i++) read(fd_r, &one_idag, sizeof(int));
  965. */
  966. tmp_inter_list = core_inter_head[sender_id];
  967. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  968. free(tmp_inter_list);
  969. if(core_inter_head[sender_id] == NULL) {
  970. if (core_inter_tail[sender_id] == NULL){
  971. printf("Malaka gamietai to core_inter_tail\n");
  972. core_inter_tail[sender_id] = NULL;
  973. }
  974. } else send_next_signal(core_inter_head[sender_id], sender_id);
  975. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS && state != IDLE_AGENT_WAITING_OFF) {
  976. printf("I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  977. fprintf(log_file,"I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  978. fflush(log_file);
  979. kill(info->si_pid, SIG_REJECT);
  980. my_stats.msg_count++;
  981. my_stats.distance += distance(node_id,sender_id);
  982. /*kill(info->si_pid, SIG_ACK);
  983. my_stats.msg_count++;
  984. my_stats.distance += distance(node_id,sender_id);
  985. fd_r = open(fifo_name, O_RDONLY);
  986. if (fd_r == -1) {
  987. perror("opening reading pipe");
  988. signals_enable();
  989. return;
  990. }
  991. read(fd_r, &num_of_idags, sizeof(int));
  992. for (i=0; i<num_of_idags; i++) read(fd_r, &one_idag, sizeof(int));
  993. */
  994. tmp_inter_list = core_inter_head[sender_id];
  995. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  996. free(tmp_inter_list);
  997. if(core_inter_head[sender_id] == NULL) {
  998. if (core_inter_tail[sender_id] == NULL){
  999. printf("Malaka gamietai to core_inter_tail\n");
  1000. core_inter_tail[sender_id] = NULL;
  1001. }
  1002. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1003. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS){ //I am the requesting common node
  1004. //printf("qweI am inside i am node with node id %d idag_id %d and sender_id %d %s\n",node_id,idag_id,sender_id,fifo_name);
  1005. sem_getvalue(&node_sem[node_id],&i);
  1006. fprintf(log_file, "In 2 Trying to acquire semaphore. Sem value = %d\n",i);
  1007. fflush(log_file);
  1008. kill(info->si_pid, SIG_ACK);
  1009. sem_wait(&node_sem[node_id]);
  1010. sem_getvalue(&node_sem[node_id],&i);
  1011. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1012. fflush(log_file);
  1013. my_stats.msg_count++;
  1014. my_stats.distance += distance(node_id,sender_id);
  1015. //printf("open4 idag_id=%d node_id=%d\n",idag_id,node_id);
  1016. fd_r = open(fifo_name, O_RDONLY);
  1017. if (fd_r == -1) {
  1018. perror("opening reading pipe");
  1019. signals_enable();
  1020. return;
  1021. } /*else {
  1022. fprintf(log_file, "I went through open\n");
  1023. fflush(log_file);
  1024. } */
  1025. read(fd_r, &num_of_idags, sizeof(int));
  1026. fprintf(log_file,"Number of agents in region %d\n",num_of_idags);
  1027. fflush(log_file);
  1028. //if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_FIND_IDAGS) far_req_max_man_count = num_of_idags;
  1029. //else
  1030. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) {
  1031. init_idags_areas_replies++;
  1032. init_DDS_idags += num_of_idags;
  1033. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) selfopt_DDS_idags += num_of_idags;
  1034. //if (node_id == 14) printf("Number of idags in region = %d\n",num_of_idags);
  1035. for (i=0; i<num_of_idags; i++){
  1036. read(fd_r, &one_idag, sizeof(int));
  1037. fprintf(log_file,"In the region I have idag with id %d\n",one_idag);
  1038. fflush(log_file);
  1039. if (core_inter_head[one_idag] == NULL){
  1040. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  1041. core_inter_tail[one_idag] = core_inter_head[one_idag];
  1042. } else {
  1043. for (tmp_inter_list = core_inter_head[one_idag]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  1044. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  1045. if (tmp_inter_list != NULL) {
  1046. fprintf(log_file,"I dismissed node %d in rep_idag_find_idags\n",one_idag);
  1047. fflush(log_file);
  1048. init_DDS_idags--;
  1049. continue;
  1050. }
  1051. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1052. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  1053. }
  1054. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = IDAG_REQ_DDS_PENDING;
  1055. else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
  1056. else core_inter_tail[one_idag]->type = SELFOPT_IDAG_REQ_DDS_PENDING;
  1057. core_inter_tail[one_idag]->data.reg.C = core_inter_head[sender_id]->data.reg.C;
  1058. core_inter_tail[one_idag]->data.reg.r = core_inter_head[sender_id]->data.reg.r;
  1059. core_inter_tail[one_idag]->next = NULL;
  1060. if (core_inter_head[one_idag]->next == NULL) {
  1061. kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  1062. my_stats.msg_count++;
  1063. my_stats.distance += distance(node_id,one_idag);
  1064. } else {
  1065. fprintf(log_file,"I did not sent req_dds to %d with interaction = %d inter 2=%d\n",one_idag,core_inter_head[one_idag]->type,core_inter_head[one_idag]->next->type);
  1066. fflush(log_file);
  1067. }
  1068. }
  1069. //sem_post(&node_sem[node_id]);
  1070. sem_getvalue(&node_sem_out[sender_id],&i);
  1071. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1072. fflush(log_file);
  1073. if (sem_post(&node_sem_out[sender_id]) == -1){
  1074. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1075. perror("sem_post");
  1076. }
  1077. sem_getvalue(&node_sem_out[sender_id],&i);
  1078. fprintf(log_file, "Sem value = %d\n",i);
  1079. fflush(log_file);
  1080. tmp_inter_list = core_inter_head[sender_id];
  1081. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1082. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1083. else send_next_signal(core_inter_head[sender_id], sender_id);
  1084. free(tmp_inter_list);
  1085. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING){
  1086. printf("I am %d and deadlock was prevented in sig_find_idags\n",node_id);
  1087. tmp_inter_list = core_inter_head[sender_id];
  1088. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1089. free(tmp_inter_list);
  1090. if(core_inter_head[sender_id] == NULL) {
  1091. if (core_inter_tail[sender_id] == NULL){
  1092. printf("Malaka gamietai to core_inter_tail\n");
  1093. core_inter_tail[sender_id] = NULL;
  1094. }
  1095. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1096. } else {
  1097. printf("I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d node_id=%d interaction=%d\n",sender_id,node_id,core_inter_head[sender_id]->type);
  1098. fprintf(log_file,"I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  1099. fflush(log_file);
  1100. kill(info->si_pid, SIG_REJECT);
  1101. my_stats.msg_count++;
  1102. my_stats.distance += distance(node_id,sender_id);
  1103. }
  1104. if (fd_r != -1) close(fd_r);
  1105. free(fifo_name);
  1106. cur_time = time(NULL);
  1107. cur_t = localtime(&cur_time);
  1108. fprintf(log_file, "[%d:%d:%d]: I ended sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1109. fflush(log_file);
  1110. signals_enable();
  1111. }
  1112. void sig_REQ_DDS_handler(int signo, siginfo_t *info, void *context)
  1113. {
  1114. int fd_r=-1, sender_id, num_of_agents, i, agent_id, num_of_cores,j;
  1115. char *fifo_name;
  1116. region cur_reg;
  1117. core_list *tmp_cores_list;
  1118. inter_list *tmp_inter_list;
  1119. DDS_list *tmp_DDS;
  1120. target_list *tmp_target_list;
  1121. signals_disable();
  1122. sender_id = get_id_from_pid(info->si_pid);
  1123. fifo_name = get_pipe_name(node_id);
  1124. cur_time = time(NULL);
  1125. cur_t = localtime(&cur_time);
  1126. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1127. fflush(log_file);
  1128. if (core_inter_head[sender_id] == NULL){ //I am the idag
  1129. //printf("I am %d and %d requested DDS\n",idag_id,sender_id);
  1130. sem_getvalue(&node_sem[node_id],&i);
  1131. fprintf(log_file, "In 3 Trying to acquire semaphore. Sem value = %d\n",i);
  1132. fflush(log_file);
  1133. kill(info->si_pid, SIG_ACK);
  1134. sem_wait(&node_sem[node_id]);
  1135. sem_getvalue(&node_sem[node_id],&i);
  1136. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1137. fflush(log_file);
  1138. my_stats.msg_count++;
  1139. my_stats.distance += distance(node_id,sender_id);
  1140. //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
  1141. fd_r = open(fifo_name, O_RDONLY);
  1142. if (fd_r == -1) {
  1143. perror("opening reading pipe");
  1144. signals_enable();
  1145. return;
  1146. } /*else {
  1147. fprintf(log_file, "I went through open\n");
  1148. fflush(log_file);
  1149. } */
  1150. read(fd_r, &cur_reg, sizeof(region));
  1151. //sem_post(&node_sem[sender_id]);
  1152. /*while (cur_reg.C < 0) {
  1153. printf("I am %d and i got shit region (%d,%d) in req_dds from %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
  1154. read(fd_r, &cur_reg, sizeof(region));
  1155. }*/
  1156. fprintf(log_file,"I am to investigate region C=%d r=%d for %d\n",cur_reg.C,cur_reg.r,sender_id);
  1157. fflush(log_file);
  1158. if (core_inter_head[sender_id] == NULL){
  1159. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1160. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1161. } else {
  1162. printf("Ton hpiame\n");
  1163. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1164. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1165. }
  1166. core_inter_tail[sender_id]->type = REP_IDAG_REQ_DDS;
  1167. core_inter_tail[sender_id]->next = NULL;
  1168. if (cur_reg.C > -1) {//gia na zhtaei o node 0 dds
  1169. core_inter_tail[sender_id]->data.agents_in_reg = (int *) malloc((2*DDS_count+1)*sizeof(int));
  1170. core_inter_tail[sender_id]->data.agents_in_reg[0] = 0;
  1171. for (i=1; i<=2*DDS_count; i+=2){
  1172. core_inter_tail[sender_id]->data.agents_in_reg[i] = -1;
  1173. core_inter_tail[sender_id]->data.agents_in_reg[i+1] = 0;
  1174. }
  1175. tmp_cores_list = my_cores;
  1176. while (tmp_cores_list != NULL){
  1177. if (distance(tmp_cores_list->core_id, cur_reg.C) <= cur_reg.r){
  1178. if (tmp_cores_list->offered_to == -1) agent_id=node_id;
  1179. else {
  1180. agent_id=tmp_cores_list->offered_to;
  1181. for (tmp_DDS = DDS->next; tmp_DDS!=NULL; tmp_DDS=tmp_DDS->next) if (tmp_DDS->agent_id == agent_id) break;
  1182. if (tmp_DDS == NULL) agent_id = node_id;
  1183. }
  1184. for (i=1; i<=2*DDS_count; i+=2)
  1185. if (core_inter_tail[sender_id]->data.agents_in_reg[i] == agent_id) break;
  1186. else if (core_inter_tail[sender_id]->data.agents_in_reg[i] == -1){
  1187. core_inter_tail[sender_id]->data.agents_in_reg[0]++;
  1188. core_inter_tail[sender_id]->data.agents_in_reg[i] = agent_id;
  1189. break;
  1190. }
  1191. core_inter_tail[sender_id]->data.agents_in_reg[i+1]++;
  1192. }
  1193. tmp_cores_list = tmp_cores_list->next;
  1194. }
  1195. } else core_inter_tail[sender_id]->data.agents_in_reg = NULL;
  1196. kill(info->si_pid, SIG_REQ_DDS);
  1197. my_stats.msg_count++;
  1198. my_stats.distance += distance(node_id,sender_id);
  1199. //sem_post(&node_sem[node_id]);
  1200. sem_getvalue(&node_sem_out[sender_id],&i);
  1201. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1202. fflush(log_file);
  1203. if (sem_post(&node_sem_out[sender_id]) == -1){
  1204. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1205. perror("sem_post");
  1206. }
  1207. sem_getvalue(&node_sem_out[sender_id],&i);
  1208. fprintf(log_file, "Sem value = %d\n",i);
  1209. fflush(log_file);
  1210. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1211. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1212. printf("I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1213. fprintf(log_file,"I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1214. fflush(log_file);
  1215. kill(info->si_pid, SIG_REJECT);
  1216. my_stats.msg_count++;
  1217. my_stats.distance += distance(node_id,sender_id);
  1218. /*kill(info->si_pid, SIG_ACK);
  1219. my_stats.msg_count++;
  1220. my_stats.distance += distance(node_id,sender_id);
  1221. fd_r = open(fifo_name, O_RDONLY);
  1222. if (fd_r == -1) {
  1223. perror("opening reading pipe");
  1224. signals_enable();
  1225. return;
  1226. }
  1227. read(fd_r, &num_of_agents, sizeof(int));
  1228. for (i=0; i<num_of_agents; i++){
  1229. read(fd_r, &agent_id, sizeof(int));
  1230. read(fd_r, &num_of_cores, sizeof(int));
  1231. }
  1232. */
  1233. tmp_inter_list = core_inter_head[sender_id];
  1234. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1235. free(tmp_inter_list);
  1236. if(core_inter_head[sender_id] == NULL) {
  1237. if (core_inter_tail[sender_id] == NULL){
  1238. printf("Malaka gamietai to core_inter_tail\n");
  1239. core_inter_tail[sender_id] = NULL;
  1240. }
  1241. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1242. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && state != IDLE_AGENT_WAITING_OFF) {
  1243. printf("I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1244. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1245. fflush(log_file);
  1246. kill(info->si_pid, SIG_REJECT);
  1247. my_stats.msg_count++;
  1248. my_stats.distance += distance(node_id,sender_id);
  1249. /*kill(info->si_pid, SIG_ACK);
  1250. my_stats.msg_count++;
  1251. my_stats.distance += distance(node_id,sender_id);
  1252. fd_r = open(fifo_name, O_RDONLY);
  1253. if (fd_r == -1) {
  1254. perror("opening reading pipe");
  1255. signals_enable();
  1256. return;
  1257. }
  1258. read(fd_r, &num_of_agents, sizeof(int));
  1259. for (i=0; i<num_of_agents; i++){
  1260. read(fd_r, &agent_id, sizeof(int));
  1261. read(fd_r, &num_of_cores, sizeof(int));
  1262. }
  1263. */
  1264. tmp_inter_list = core_inter_head[sender_id];
  1265. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1266. free(tmp_inter_list);
  1267. if(core_inter_head[sender_id] == NULL) {
  1268. if (core_inter_tail[sender_id] == NULL){
  1269. printf("Malaka gamietai to core_inter_tail\n");
  1270. core_inter_tail[sender_id] = NULL;
  1271. }
  1272. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1273. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){ //I am the requesting common node
  1274. //printf("vbnI am inside i am node with node id %d and sender_id %d\n",node_id,sender_id);
  1275. sem_getvalue(&node_sem[node_id],&i);
  1276. fprintf(log_file, "In 4 Trying to acquire semaphore. Sem value = %d\n",i);
  1277. fflush(log_file);
  1278. kill(info->si_pid, SIG_ACK);
  1279. sem_wait(&node_sem[node_id]);
  1280. sem_getvalue(&node_sem[node_id],&i);
  1281. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1282. fflush(log_file);
  1283. my_stats.msg_count++;
  1284. my_stats.distance += distance(node_id,sender_id);
  1285. //printf("open5 idag_id=%d node_id=%d\n",idag_id,node_id);
  1286. fd_r = open(fifo_name, O_RDONLY);
  1287. if (fd_r == -1) {
  1288. perror("opening reading pipe");
  1289. signals_enable();
  1290. return;
  1291. } /*else {
  1292. fprintf(log_file, "I went through open\n");
  1293. fflush(log_file);
  1294. } */
  1295. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  1296. init_DDS_replies++;
  1297. fprintf(log_file,"init_DDS_idags = %d, init_DDS_replies = %d init_idags_areas_replies=%d\n",init_DDS_idags,init_DDS_replies,init_idags_areas_replies);
  1298. fflush(log_file);
  1299. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS) {
  1300. selfopt_DDS_replies++;
  1301. fprintf(log_file,"selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  1302. fflush(log_file);
  1303. }
  1304. read(fd_r, &num_of_agents, sizeof(int));
  1305. while (num_of_agents > X_max*Y_max) {
  1306. printf("I am %d and in req_dds i got shit num_of_agents %d from %d\n",node_id,num_of_agents,sender_id);
  1307. read(fd_r, &num_of_agents, sizeof(int));
  1308. }
  1309. //if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) far_man_agent_count += num_of_agents;
  1310. //printf("Number of agents in region = %d\n",num_of_agents);
  1311. //fprintf(log_file, "Number of agents in region = %d of %d init_DDS_replies = %d init_DDS_idags = %d\n",num_of_agents,sender_id,init_DDS_replies,init_DDS_idags);
  1312. fprintf(log_file, "Number of agents in region = %d of %d reg = (%d,%d)\n",num_of_agents,sender_id,core_inter_head[sender_id]->data.reg.C,core_inter_head[sender_id]->data.reg.r);
  1313. fflush(log_file);
  1314. for (i=0; i<num_of_agents; i++) {
  1315. read(fd_r, &agent_id, sizeof(int));
  1316. read(fd_r, &num_of_cores, sizeof(int));
  1317. //printf("zxc I am %d and In the DDS of %d there is an agent with id %d and %d cores\n",node_id,sender_id,agent_id,num_of_cores);
  1318. fprintf(log_file, "there is an agent with id %d and %d cores\n",agent_id,num_of_cores);
  1319. fflush(log_file);
  1320. if (agent_id == node_id) continue;//((node_id == -1 && agent_id == idag_id) || node_id == agent_id)
  1321. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS){
  1322. tmp_target_list = init_targets_head;
  1323. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1324. //if (tmp_target_list->target != agent_id)
  1325. tmp_target_list = tmp_target_list->next;
  1326. if (tmp_target_list == NULL) {
  1327. if (init_targets_head == NULL) {
  1328. init_targets_head = (target_list *) malloc(sizeof(target_list));
  1329. init_targets_tail = init_targets_head;;
  1330. } else {
  1331. init_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1332. init_targets_tail = init_targets_tail->next;
  1333. }
  1334. //fprintf(log_file, "in here it begins a\n");
  1335. //fflush(log_file);
  1336. init_targets_tail->next = NULL;
  1337. init_targets_tail->target = agent_id;
  1338. init_targets_tail->num_of_regions = 1;
  1339. //init_targets_tail->region_arr = (region *) malloc(INIT_AREAS_NUM * sizeof(region));
  1340. init_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1341. //fprintf(log_file, "in here it begins b\n");
  1342. //fflush(log_file);
  1343. } else {
  1344. fprintf(log_file, "in here num_of_regions = %d\n",tmp_target_list->num_of_regions);
  1345. fflush(log_file);
  1346. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1347. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  1348. fprintf(log_file, "fucking area allready exists\n");
  1349. fflush(log_file);
  1350. break;
  1351. }
  1352. if (j == tmp_target_list->num_of_regions) {
  1353. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1354. //fprintf(log_file, "new area added\n");
  1355. //fflush(log_file);
  1356. }
  1357. }
  1358. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){
  1359. tmp_target_list = selfopt_targets_head;
  1360. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1361. tmp_target_list = tmp_target_list->next;
  1362. if (tmp_target_list == NULL) {
  1363. if (selfopt_targets_head == NULL) {
  1364. selfopt_targets_head = (target_list *) malloc(sizeof(target_list));
  1365. selfopt_targets_tail = selfopt_targets_head;;
  1366. } else {
  1367. selfopt_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1368. selfopt_targets_tail = selfopt_targets_tail->next;
  1369. }
  1370. selfopt_targets_tail->next = NULL;
  1371. selfopt_targets_tail->target = agent_id;
  1372. selfopt_targets_tail->num_of_regions = 1;
  1373. selfopt_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1374. } else {
  1375. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1376. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  1377. break;
  1378. }
  1379. if (j == tmp_target_list->num_of_regions)
  1380. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1381. } //tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1382. } else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS){
  1383. if (core_inter_head[agent_id] == NULL){
  1384. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1385. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1386. } else {
  1387. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1388. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1389. }
  1390. //if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1391. //else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) core_inter_tail[agent_id]->type = FAR_REQ_CORES;
  1392. //else core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1393. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  1394. core_inter_tail[agent_id]->data.reg = core_inter_head[sender_id]->data.reg;
  1395. core_inter_tail[agent_id]->next = NULL;
  1396. if (core_inter_head[agent_id]->next == NULL) {
  1397. kill(pid_num[agent_id], SIG_REQ_CORES); //newly created, not an idag
  1398. my_stats.msg_count++;
  1399. my_stats.distance += distance(node_id,agent_id);
  1400. }
  1401. //else printf("It is fucked here interaction= %d\n",core_inter_head[agent_id]->type);
  1402. } /*else if (far_req_max_man_cores == -1 || far_req_max_man_cores<num_of_cores){
  1403. far_req_max_man_cores = num_of_cores;
  1404. far_req_max_man = agent_id;
  1405. }*/
  1406. }
  1407. //sem_post(&node_sem[sender_id]);
  1408. /*if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_REQ_DDS)
  1409. if (--far_req_max_man_count == 0){//all idags have replied
  1410. printf("Agent with max cores in region is %d\n",far_req_max_man);
  1411. if (far_req_max_man == node_id)// || (node_id == -1 && far_req_max_man == idag_id)) //I am the one
  1412. printf("I am the one!!\n");
  1413. //completely inclomplete apparently
  1414. else {
  1415. if (core_inter_head[far_req_max_man] == NULL){
  1416. core_inter_head[far_req_max_man] = (inter_list *) malloc(sizeof(inter_list));
  1417. core_inter_tail[far_req_max_man] = core_inter_head[far_req_max_man];
  1418. } else {
  1419. core_inter_tail[far_req_max_man]->next = (inter_list *) malloc(sizeof(inter_list));
  1420. core_inter_tail[far_req_max_man] = core_inter_tail[far_req_max_man]->next;
  1421. }
  1422. core_inter_tail[far_req_max_man]->type = FAR_REQ_MAN_APPOINT;
  1423. core_inter_tail[far_req_max_man]->data.reg = core_inter_head[sender_id]->data.reg;
  1424. core_inter_tail[far_req_max_man]->next = NULL;
  1425. //dont't kill here for sychronisation reasons, original manager should be informed for the change in his far manager
  1426. //if (core_inter_head[far_req_max_man]->next == NULL) kill(pid_num[far_req_max_man], SIG_FAR_REQ);
  1427. if (core_inter_head[far_req_or_sender] == NULL){
  1428. core_inter_head[far_req_or_sender] = (inter_list *) malloc(sizeof(inter_list));
  1429. core_inter_tail[far_req_or_sender] = core_inter_head[far_req_or_sender];
  1430. } else {
  1431. core_inter_tail[far_req_or_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  1432. core_inter_tail[far_req_or_sender] = core_inter_tail[far_req_or_sender]->next;
  1433. }
  1434. core_inter_tail[far_req_or_sender]->type = REP_FAR_INIT_REQ;
  1435. core_inter_tail[far_req_or_sender]->data.far_req_man = far_req_max_man;
  1436. core_inter_tail[far_req_or_sender]->next = NULL;
  1437. if (core_inter_head[far_req_or_sender]->next == NULL) {
  1438. kill(pid_num[far_req_or_sender], SIG_INIT_FAR_REQ);
  1439. my_stats.msg_count++;
  1440. my_stats.distance += distance(node_id,far_req_or_sender);
  1441. }
  1442. }
  1443. }*/
  1444. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == INIT_AREAS_NUM) {//INIT_AREAS_NUM
  1445. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1446. agent_id = tmp_target_list->target;
  1447. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1448. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  1449. if (tmp_inter_list == NULL) {
  1450. if (core_inter_head[agent_id] == NULL){
  1451. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1452. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1453. } else {
  1454. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1455. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1456. }
  1457. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1458. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1459. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1460. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  1461. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1462. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1463. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1464. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1465. }
  1466. fprintf(log_file,"\n");
  1467. fflush(log_file);
  1468. core_inter_tail[agent_id]->next = NULL;
  1469. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1470. if (core_inter_head[agent_id]->next == NULL) {
  1471. kill(pid_num[agent_id], SIG_REQ_CORES);
  1472. my_stats.msg_count++;
  1473. my_stats.distance += distance(node_id,agent_id);
  1474. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  1475. kill(pid_num[agent_id], SIG_REQ_CORES);
  1476. my_stats.msg_count++;
  1477. my_stats.distance += distance(node_id,agent_id);
  1478. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  1479. } else
  1480. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  1481. }
  1482. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  1483. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4\n");
  1484. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && selfopt_DDS_replies == selfopt_DDS_idags) {
  1485. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1486. agent_id = tmp_target_list->target;
  1487. if (core_inter_head[agent_id] == NULL){
  1488. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1489. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1490. } else {
  1491. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1492. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1493. }
  1494. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1495. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1496. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1497. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  1498. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1499. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1500. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1501. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1502. }
  1503. fprintf(log_file,"\n");
  1504. fflush(log_file);
  1505. core_inter_tail[agent_id]->next = NULL;
  1506. if (core_inter_head[agent_id]->next == NULL) {
  1507. kill(pid_num[agent_id], SIG_REQ_CORES);
  1508. my_stats.msg_count++;
  1509. my_stats.distance += distance(node_id,agent_id);
  1510. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  1511. kill(pid_num[agent_id], SIG_REQ_CORES);
  1512. my_stats.msg_count++;
  1513. my_stats.distance += distance(node_id,agent_id);
  1514. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  1515. }
  1516. its.it_value.tv_nsec = 500 * MS;// 750000000;
  1517. selfopt_time_rem = its.it_value.tv_nsec;
  1518. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
  1519. if (state != IDLE_AGENT_WAITING_OFF) {
  1520. printf("I am %d and about to set my alarm for selfopt check and my state before that was %d\n",node_id,state);
  1521. state = IDLE_AGENT_WAITING_OFF;
  1522. }
  1523. }
  1524. //printf("I come here\n");
  1525. //sem_post(&node_sem[node_id]);
  1526. sem_getvalue(&node_sem_out[sender_id],&i);
  1527. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1528. fflush(log_file);
  1529. if (sem_post(&node_sem_out[sender_id]) == -1){
  1530. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1531. perror("sem_post");
  1532. }
  1533. sem_getvalue(&node_sem_out[sender_id],&i);
  1534. fprintf(log_file, "Sem value = %d\n",i);
  1535. fflush(log_file);
  1536. tmp_inter_list = core_inter_head[sender_id];
  1537. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1538. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1539. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  1540. send_next_signal(core_inter_head[sender_id], sender_id);
  1541. free(tmp_inter_list);
  1542. //printf("I come here as well\n");
  1543. } else if (core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){ //I am the requesting common node
  1544. //printf("I am inside i am node with node id %d and sender_id %d %s\n",node_id,sender_id,fifo_name);
  1545. sem_getvalue(&node_sem[node_id],&i);
  1546. fprintf(log_file, "In DEBUG_IDAG_REQ_DDS Trying to acquire semaphore. Sem value = %d\n",i);
  1547. fflush(log_file);
  1548. kill(info->si_pid, SIG_ACK);
  1549. sem_wait(&node_sem[node_id]);
  1550. sem_getvalue(&node_sem[node_id],&i);
  1551. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1552. fflush(log_file);
  1553. //kill(info->si_pid, SIG_ACK);
  1554. my_stats.msg_count++;
  1555. my_stats.distance += distance(node_id,sender_id);
  1556. //printf("open6 idag_id=%d node_id=%d\n",idag_id,node_id);
  1557. fd_r = open(fifo_name, O_RDONLY);
  1558. if (fd_r == -1) {
  1559. perror("opening reading pipe");
  1560. signals_enable();
  1561. return;
  1562. } /*else {
  1563. fprintf(log_file, "I went through open\n");
  1564. fflush(log_file);
  1565. } */
  1566. read(fd_r, &num_of_agents, sizeof(int));
  1567. printf("\n");
  1568. printf("Number of agents in region = %d\n",num_of_agents);
  1569. for (i=0; i<num_of_agents; i++){
  1570. read(fd_r, &agent_id, sizeof(int));
  1571. read(fd_r, &num_of_cores, sizeof(int));
  1572. printf("Agent no %d is %d with %d cores\n",i,agent_id,num_of_cores);
  1573. }
  1574. sem_getvalue(&node_sem_out[sender_id],&i);
  1575. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1576. fflush(log_file);
  1577. if (sem_post(&node_sem_out[sender_id]) == -1){
  1578. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1579. perror("sem_post");
  1580. }
  1581. sem_getvalue(&node_sem_out[sender_id],&i);
  1582. fprintf(log_file, "Sem value = %d\n",i);
  1583. fflush(log_file);
  1584. tmp_inter_list = core_inter_head[sender_id];
  1585. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1586. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1587. else send_next_signal(core_inter_head[sender_id], sender_id);
  1588. free(tmp_inter_list);
  1589. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  1590. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING){
  1591. printf("I am %d and deadlock was prevented in sig_req_dds\n",node_id);
  1592. tmp_inter_list = core_inter_head[sender_id];
  1593. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1594. free(tmp_inter_list);
  1595. if(core_inter_head[sender_id] == NULL) {
  1596. if (core_inter_tail[sender_id] == NULL){
  1597. printf("Malaka gamietai to core_inter_tail\n");
  1598. core_inter_tail[sender_id] = NULL;
  1599. }
  1600. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1601. } else {
  1602. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1603. printf("I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1604. fprintf(log_file,"I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1605. fflush(log_file);
  1606. /*kill(info->si_pid, SIG_ACK);
  1607. my_stats.msg_count++;
  1608. my_stats.distance += distance(node_id,sender_id);
  1609. fd_r = open(fifo_name, O_RDONLY);
  1610. if (fd_r == -1) {
  1611. perror("opening reading pipe");
  1612. signals_enable();
  1613. return;
  1614. }
  1615. read(fd_r, &num_of_agents, sizeof(int));
  1616. for (i=0; i<num_of_agents; i++){
  1617. read(fd_r, &agent_id, sizeof(int));
  1618. read(fd_r, &num_of_cores, sizeof(int));
  1619. }*/
  1620. kill(info->si_pid, SIG_REJECT);
  1621. my_stats.msg_count++;
  1622. my_stats.distance += distance(node_id,sender_id);
  1623. }
  1624. if (fd_r != -1) close(fd_r);
  1625. free(fifo_name);
  1626. cur_time = time(NULL);
  1627. cur_t = localtime(&cur_time);
  1628. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1629. fflush(log_file);
  1630. signals_enable();
  1631. }
  1632. void sig_REQ_CORES_handler(int signo, siginfo_t *info, void *context)
  1633. {
  1634. int fd_r=-1, sender_id, i, tmp_int, num_of_offers, j;//num_of_idags, i, one_idag;
  1635. char *fifo_name;
  1636. float req_gain;
  1637. region cur_reg;
  1638. app req_app;
  1639. offer one_offer;
  1640. offer_list *tmp_offer_list, *tmp_offer_prev = NULL, *tmp_head, *chosen_node;
  1641. inter_list *tmp_inter_list;
  1642. //printf("I came through REQ!CORES! idag_id = %d, node_id = %d\n",idag_id,node_id);
  1643. signals_disable();
  1644. sender_id = get_id_from_pid(info->si_pid);
  1645. fifo_name = get_pipe_name(node_id);
  1646. cur_time = time(NULL);
  1647. cur_t = localtime(&cur_time);
  1648. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1649. fflush(log_file);
  1650. if (core_inter_head[sender_id] == NULL && my_cores != NULL){ //I am the agent && (my_idag == -1 || (my_idag != -1 && my_app.num_of_cores != -1))
  1651. sem_getvalue(&node_sem[node_id],&i);
  1652. fprintf(log_file, "In 5 Trying to acquire semaphore. Sem value = %d\n",i);
  1653. fflush(log_file);
  1654. kill(info->si_pid, SIG_ACK);
  1655. sem_wait(&node_sem[node_id]);
  1656. sem_getvalue(&node_sem[node_id],&i);
  1657. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1658. fflush(log_file);
  1659. my_stats.msg_count++;
  1660. my_stats.distance += distance(node_id,sender_id);
  1661. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  1662. //printf("open7 idag_id=%d node_id=%d\n",idag_id,node_id);
  1663. fd_r = open(fifo_name, O_RDONLY);
  1664. if (fd_r == -1) {
  1665. perror("opening reading pipe");
  1666. signals_enable();
  1667. return;
  1668. } /*else {
  1669. fprintf(log_file, "I went through open\n");
  1670. fflush(log_file);
  1671. } */
  1672. read(fd_r, &req_app, sizeof(app));
  1673. //printf("I am node with id %d idag id %d with sender id %d and I am to investigate region C=%d r=%d and app %f\n",node_id,idag_id,sender_id,cur_reg.C,cur_reg.r,req_app.A);
  1674. if (core_inter_head[sender_id] == NULL){
  1675. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1676. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1677. } else {
  1678. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1679. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1680. }
  1681. core_inter_tail[sender_id]->type = REP_AGENT_REQ_CORES;
  1682. read(fd_r, &core_inter_tail[sender_id]->data.off_arr.num_of_offers, sizeof(int));
  1683. while (core_inter_tail[sender_id]->data.off_arr.num_of_offers > INIT_AREAS_NUM) {
  1684. printf("I am %d kai fagame skoupidia apo ton %d kai einai %d\n",node_id,sender_id,core_inter_tail[sender_id]->data.off_arr.num_of_offers);
  1685. read(fd_r, &core_inter_tail[sender_id]->data.off_arr.num_of_offers, sizeof(int));
  1686. }
  1687. core_inter_tail[sender_id]->data.off_arr.offer_arr = (offer *) malloc(core_inter_tail[sender_id]->data.off_arr.num_of_offers * sizeof(offer));
  1688. core_inter_tail[sender_id]->next = NULL;
  1689. //if (node_id == 14) printf("Sender is %d and num_of_offers = %d\n",sender_id,core_inter_tail[sender_id]->data.off_arr.num_of_offers);
  1690. for (i=0; i<core_inter_tail[sender_id]->data.off_arr.num_of_offers; i++){
  1691. read(fd_r, &cur_reg, sizeof(region));
  1692. if (my_idag == -1) {
  1693. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1694. tmp_int = offer_cores(my_cores, req_app, cur_reg, core_inter_tail[sender_id]->data.off_arr.offer_arr[i].offered_cores, sender_id);
  1695. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].num_of_cores = tmp_int;
  1696. my_stats.comp_effort++;
  1697. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].spd_loss = 0.0;
  1698. } else if (my_cores != NULL && my_cores_count>2) {
  1699. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1700. tmp_int = offer_cores(my_cores, req_app, cur_reg, core_inter_tail[sender_id]->data.off_arr.offer_arr[i].offered_cores, sender_id);
  1701. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].num_of_cores = tmp_int;
  1702. my_stats.comp_effort++;
  1703. //printf("I am %d in i = %d and i offer %d cores\n",node_id,i,tmp_int);
  1704. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-tmp_int);
  1705. req_gain = Speedup(req_app,req_app.num_of_cores+tmp_int) - Speedup(req_app,req_app.num_of_cores);
  1706. if (tmp_int > 0) {
  1707. fprintf(log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n",tmp_int,core_inter_tail[sender_id]->data.off_arr.offer_arr[i].spd_loss,req_gain);
  1708. fflush(log_file);
  1709. fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
  1710. ,tmp_int,core_inter_tail[sender_id]->data.off_arr.offer_arr[i].spd_loss,req_gain);
  1711. fflush(app_log_file);
  1712. }
  1713. } else {
  1714. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].num_of_cores = 0;
  1715. core_inter_tail[sender_id]->data.off_arr.offer_arr[i].spd_loss = 0.0;
  1716. }
  1717. fprintf(log_file, "I offered %d %d cores: ",sender_id,core_inter_tail[sender_id]->data.off_arr.offer_arr[i].num_of_cores);
  1718. for (j=0; j<core_inter_tail[sender_id]->data.off_arr.offer_arr[i].num_of_cores; j++)
  1719. fprintf(log_file, "%d, ",core_inter_tail[sender_id]->data.off_arr.offer_arr[i].offered_cores[j]);
  1720. fprintf(log_file, "\n");
  1721. fflush(log_file);
  1722. }
  1723. //sem_post(&node_sem[sender_id]);
  1724. //get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
  1725. if (core_inter_head[sender_id]->next == NULL) {
  1726. kill(info->si_pid, SIG_REQ_CORES);
  1727. my_stats.msg_count++;
  1728. my_stats.distance += distance(node_id,sender_id);
  1729. } else printf("Apparently not null interaction=%d\n",core_inter_head[sender_id]->type);
  1730. //sem_post(&node_sem[node_id]);
  1731. sem_getvalue(&node_sem_out[sender_id],&i);
  1732. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  1733. fflush(log_file);
  1734. if (sem_post(&node_sem_out[sender_id]) == -1){
  1735. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  1736. perror("sem_post");
  1737. }
  1738. sem_getvalue(&node_sem_out[sender_id],&i);
  1739. fprintf(log_file, "Sem value = %d\n",i);
  1740. fflush(log_file);
  1741. } else if (core_inter_head[sender_id] == NULL) {
  1742. printf("I am %d and i have to reject req_cores from %d with null interaction\n",node_id,sender_id);
  1743. fprintf(log_file,"i have to reject req_cores from %d. with null interaction\n",sender_id);
  1744. fflush(log_file);
  1745. kill(info->si_pid, SIG_REJECT);
  1746. my_stats.msg_count++;
  1747. my_stats.distance += distance(node_id,sender_id);
  1748. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1749. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1750. printf("I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1751. fprintf(log_file,"I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1752. fflush(log_file);
  1753. kill(info->si_pid, SIG_REJECT);
  1754. my_stats.msg_count++;
  1755. my_stats.distance += distance(node_id,sender_id);
  1756. /*kill(info->si_pid, SIG_ACK);
  1757. my_stats.msg_count++;
  1758. my_stats.distance += distance(node_id,sender_id);
  1759. fd_r = open(fifo_name, O_RDONLY);
  1760. if (fd_r == -1) {
  1761. perror("opening reading pipe");
  1762. signals_enable();
  1763. return;
  1764. }
  1765. read(fd_r, &num_of_offers, sizeof(int));
  1766. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1767. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1768. for (j=1; j<=num_of_offers; j++){
  1769. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1770. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1771. for (i=0; i<one_offer.num_of_cores; i++)
  1772. read(fd_r, &tmp_int, sizeof(int));
  1773. core_inter_head[sender_id]->data.offer_acc_array[j] = 0;
  1774. }
  1775. core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
  1776. kill(info->si_pid, SIG_REP_OFFERS);
  1777. my_stats.msg_count++;
  1778. my_stats.distance += distance(node_id,sender_id);*/
  1779. tmp_inter_list = core_inter_head[sender_id];
  1780. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1781. free(tmp_inter_list);
  1782. if(core_inter_head[sender_id] == NULL) {
  1783. if (core_inter_tail[sender_id] == NULL){
  1784. printf("Malaka gamietai to core_inter_tail\n");
  1785. core_inter_tail[sender_id] = NULL;
  1786. }
  1787. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1788. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && state != IDLE_AGENT_WAITING_OFF) {
  1789. printf("I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1790. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1791. fflush(log_file);
  1792. kill(info->si_pid, SIG_REJECT);
  1793. my_stats.msg_count++;
  1794. my_stats.distance += distance(node_id,sender_id);
  1795. /*kill(info->si_pid, SIG_ACK);
  1796. my_stats.msg_count++;
  1797. my_stats.distance += distance(node_id,sender_id);
  1798. fd_r = open(fifo_name, O_RDONLY);
  1799. if (fd_r == -1) {
  1800. perror("opening reading pipe");
  1801. signals_enable();
  1802. return;
  1803. }
  1804. read(fd_r, &num_of_offers, sizeof(int));
  1805. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1806. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1807. for (j=1; j<=num_of_offers; j++){
  1808. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1809. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1810. for (i=0; i<one_offer.num_of_cores; i++)
  1811. read(fd_r, &tmp_int, sizeof(int));
  1812. core_inter_head[sender_id]->data.offer_acc_array[j] = 0;
  1813. }
  1814. core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
  1815. kill(info->si_pid, SIG_REP_OFFERS);
  1816. my_stats.msg_count++;
  1817. my_stats.distance += distance(node_id,sender_id);*/
  1818. tmp_inter_list = core_inter_head[sender_id];
  1819. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1820. free(tmp_inter_list);
  1821. if(core_inter_head[sender_id] == NULL) {
  1822. if (core_inter_tail[sender_id] == NULL){
  1823. printf("Malaka gamietai to core_inter_tail\n");
  1824. core_inter_tail[sender_id] = NULL;
  1825. }
  1826. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1827. } else if (core_inter_head[sender_id]->type == FAR_REQ_CORES && (state == IDLE_IDAG || (node_id == 0 && time_for_farman == -1))) {
  1828. printf("I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1829. fprintf(log_file,"I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1830. fflush(log_file);
  1831. kill(info->si_pid, SIG_REJECT);
  1832. my_stats.msg_count++;
  1833. my_stats.distance += distance(node_id,sender_id);
  1834. /*kill(info->si_pid, SIG_ACK);
  1835. my_stats.msg_count++;
  1836. my_stats.distance += distance(node_id,sender_id);
  1837. fd_r = open(fifo_name, O_RDONLY);
  1838. if (fd_r == -1) {
  1839. perror("opening reading pipe");
  1840. signals_enable();
  1841. return;
  1842. }
  1843. read(fd_r, &num_of_offers, sizeof(int));
  1844. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1845. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1846. for (j=1; j<=num_of_offers; j++){
  1847. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1848. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1849. for (i=0; i<one_offer.num_of_cores; i++)
  1850. read(fd_r, &tmp_int, sizeof(int));
  1851. core_inter_head[sender_id]->data.offer_acc_array[j] = 0;
  1852. }
  1853. core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
  1854. kill(info->si_pid, SIG_REP_OFFERS);
  1855. my_stats.msg_count++;
  1856. my_stats.distance += distance(node_id,sender_id);*/
  1857. tmp_inter_list = core_inter_head[sender_id];
  1858. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1859. free(tmp_inter_list);
  1860. if(core_inter_head[sender_id] == NULL) {
  1861. if (core_inter_tail[sender_id] == NULL){
  1862. printf("Malaka gamietai to core_inter_tail\n");
  1863. core_inter_tail[sender_id] = NULL;
  1864. }
  1865. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1866. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == FAR_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES){ //I am the requesting common node
  1867. //printf("fghjfgI am inside i am node with node id %d idag_id %d and sender_id %d\n",node_id,idag_id,sender_id);
  1868. sem_getvalue(&node_sem[node_id],&i);
  1869. fprintf(log_file, "In 6 Trying to acquire semaphore. Sem value = %d\n",i);
  1870. fflush(log_file);
  1871. kill(info->si_pid, SIG_ACK);
  1872. sem_wait(&node_sem[node_id]);
  1873. sem_getvalue(&node_sem[node_id],&i);
  1874. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  1875. fflush(log_file);
  1876. my_stats.msg_count++;
  1877. my_stats.distance += distance(node_id,sender_id);
  1878. //printf("open8 idag_id=%d node_id=%d\n",idag_id,node_id);
  1879. fd_r = open(fifo_name, O_RDONLY);
  1880. if (fd_r == -1) {
  1881. perror("opening reading pipe");
  1882. signals_enable();
  1883. return;
  1884. } /*else {
  1885. fprintf(log_file, "I went through open\n");
  1886. fflush(log_file);
  1887. } */
  1888. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES) {//den erxontai ta far edw
  1889. read(fd_r, &num_of_offers, sizeof(int));
  1890. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1891. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1892. //core_inter_head[sender_id]->data.offer_accepted = -1;
  1893. //one_offer.offered_cores = NULL;
  1894. for (j=1; j<=num_of_offers; j++){
  1895. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1896. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1897. //if (one_offer.offered_cores != NULL) free(one_offer.offered_cores);
  1898. /*one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1899. for (i=0; i<one_offer.num_of_cores; i++)
  1900. read(fd_r, &one_offer.offered_cores[i], sizeof(int));
  1901. printf("asdasNode %d is offering %d cores: ",sender_id,one_offer.num_of_cores);
  1902. for (i=0; i<one_offer.num_of_cores; i++)
  1903. printf(" %d,",one_offer.offered_cores[i]);
  1904. printf("\n");*/
  1905. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && init_man_offers == NULL){
  1906. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1907. chosen_node = init_man_offers;
  1908. /*init_man_offers->off = one_offer;
  1909. init_man_offers->sender = sender_id;
  1910. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1911. init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1912. */
  1913. init_man_offers->next = NULL;
  1914. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && selfopt_man_offers == NULL){
  1915. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1916. chosen_node = selfopt_man_offers;
  1917. /*
  1918. selfopt_man_offers->off = one_offer;
  1919. selfopt_man_offers->sender = sender_id;
  1920. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1921. selfopt_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1922. */
  1923. selfopt_man_offers->next = NULL;
  1924. } else {
  1925. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) tmp_offer_list = init_man_offers;
  1926. else tmp_offer_list = selfopt_man_offers;
  1927. //if (node_id == 12) printf("aaa\n");
  1928. tmp_offer_prev = NULL;
  1929. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  1930. tmp_offer_prev = tmp_offer_list;
  1931. tmp_offer_list = tmp_offer_list->next;
  1932. }
  1933. //if (node_id == 12) printf("aa\n");
  1934. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  1935. //if (node_id == 12) printf("a\n");
  1936. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1937. tmp_offer_list = tmp_offer_prev->next;
  1938. tmp_offer_list->next = NULL;
  1939. chosen_node = tmp_offer_list;
  1940. /*tmp_offer_list->off = one_offer;
  1941. tmp_offer_list->sender = sender_id;
  1942. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1943. tmp_offer_list->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1944. */
  1945. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  1946. //if (node_id == 12) printf("b\n");
  1947. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  1948. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1949. tmp_head = init_man_offers;
  1950. } else {
  1951. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1952. tmp_head = selfopt_man_offers;
  1953. }
  1954. chosen_node = tmp_head;
  1955. /*tmp_head->off = one_offer;
  1956. tmp_head->sender = sender_id;
  1957. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1958. tmp_head->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];*/
  1959. tmp_head->next = tmp_offer_list;
  1960. } else {
  1961. //if (node_id == 12) printf("c\n");
  1962. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1963. tmp_offer_prev = tmp_offer_prev->next;
  1964. chosen_node = tmp_offer_prev;
  1965. /*tmp_offer_prev->off = one_offer;
  1966. tmp_offer_prev->sender = sender_id;
  1967. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1968. tmp_offer_prev->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1969. */
  1970. tmp_offer_prev->next = tmp_offer_list;
  1971. }
  1972. //for(tmp_offer_list=init_man_offers; tmp_offer_list!=NULL; tmp_offer_list=tmp_offer_list->next)
  1973. // printf("kariolares node_id=%d sender_id=%d\n",node_id,tmp_offer_list->sender);
  1974. }
  1975. chosen_node->off.num_of_cores = one_offer.num_of_cores;
  1976. chosen_node->off.spd_loss = one_offer.spd_loss;
  1977. chosen_node->off.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1978. for (i=0; i<one_offer.num_of_cores; i++)
  1979. read(fd_r, &chosen_node->off.offered_cores[i], sizeof(int));
  1980. /*printf("asdasNode %d is offering %d cores: ",sender_id,chosen_node->off.num_of_cores);
  1981. for (i=0; i<chosen_node->off.num_of_cores; i++)
  1982. printf(" %d,",chosen_node->off.offered_cores[i]);
  1983. printf("\n");*/
  1984. chosen_node->sender = sender_id;
  1985. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1986. chosen_node->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1987. cur_time = time(NULL);
  1988. cur_t = localtime(&cur_time);
  1989. fprintf(log_file, "[%d:%d:%d]: One node successfully added in list type=%d sender_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->type,sender_id);
  1990. fflush(log_file);
  1991. }
  1992. } else {
  1993. read(fd_r, &num_of_offers, sizeof(int));
  1994. //if (num_of_offers > 1) printf("I am %d in req cores in far and i am getting %d offers from %d\n",node_id,num_of_offers,sender_id);
  1995. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1996. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1997. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1998. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1999. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  2000. for (i=0; i<one_offer.num_of_cores; i++)
  2001. read(fd_r, &one_offer.offered_cores[i], sizeof(int));
  2002. //printf("asdasNode %d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  2003. //core_inter_head[sender_id]->data.offer_accepted = -1;
  2004. core_inter_head[sender_id]->data.offer_acc_array[1] = -1;
  2005. if (far_man_offers == NULL){
  2006. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  2007. far_man_offers_tail = far_man_offers;
  2008. } else {
  2009. far_man_offers_tail->next = (offer_list *) malloc(sizeof(offer_list));
  2010. far_man_offers_tail = far_man_offers_tail->next;
  2011. }
  2012. far_man_offers_tail->off = one_offer;
  2013. far_man_offers_tail->sender = sender_id;
  2014. far_man_offers_tail->answer = &core_inter_head[sender_id]->data.offer_acc_array[1];//offer_accepted;
  2015. far_man_offers_tail->next = NULL;
  2016. far_list_count++;
  2017. cur_time = time(NULL);
  2018. cur_t = localtime(&cur_time);
  2019. fprintf(log_file, "[%d:%d:%d]: One node successfully added in far_man_offers list %d\n",
  2020. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->data.offer_acc_array[0]);
  2021. fflush(log_file);
  2022. }
  2023. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  2024. sem_getvalue(&node_sem_out[sender_id],&i);
  2025. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2026. fflush(log_file);
  2027. if (sem_post(&node_sem_out[sender_id]) == -1){
  2028. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2029. perror("sem_post");
  2030. }
  2031. sem_getvalue(&node_sem_out[sender_id],&i);
  2032. fprintf(log_file, "Sem value = %d\n",i);
  2033. fflush(log_file);
  2034. //sem_post(&node_sem[node_id]);
  2035. ////sem_post(&node_sem[sender_id]);
  2036. /*tmp_inter_list = core_inter_head[sender_id]->next;
  2037. free(core_inter_head[sender_id]);
  2038. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  2039. core_inter_head[sender_id]->next = tmp_inter_list;
  2040. core_inter_head[sender_id]->type = REP_AGENT_OFFER_SENT;
  2041. core_inter_head[sender_id]->data.offer_accepted = -1;*/
  2042. /*if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2043. else send_next_signal(core_inter_head[sender_id], sender_id);
  2044. free(tmp_inter_list);*/
  2045. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING
  2046. || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING){
  2047. printf("I am %d and deadlock was prevented in req_cores by %d\n",node_id,sender_id);
  2048. tmp_inter_list = core_inter_head[sender_id];
  2049. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2050. free(tmp_inter_list);
  2051. if(core_inter_head[sender_id] == NULL) {
  2052. if (core_inter_tail[sender_id] == NULL){
  2053. printf("Malaka gamietai to core_inter_tail\n");
  2054. core_inter_tail[sender_id] = NULL;
  2055. }
  2056. } else send_next_signal(core_inter_head[sender_id], sender_id);
  2057. } else {
  2058. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2059. printf("I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2060. fprintf(log_file,"I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2061. fflush(log_file);
  2062. /*kill(info->si_pid, SIG_ACK);
  2063. my_stats.msg_count++;
  2064. my_stats.distance += distance(node_id,sender_id);
  2065. fd_r = open(fifo_name, O_RDONLY);
  2066. if (fd_r == -1) {
  2067. perror("opening reading pipe");
  2068. signals_enable();
  2069. return;
  2070. }
  2071. read(fd_r, &req_app, sizeof(app));
  2072. //printf("I am node with id %d idag id %d with sender id %d and I am to investigate region C=%d r=%d and app %f\n",node_id,idag_id,sender_id,cur_reg.C,cur_reg.r,req_app.A);
  2073. read(fd_r, &num_of_offers, sizeof(int));
  2074. for (i=0; i<num_of_offers; i++)
  2075. read(fd_r, &cur_reg, sizeof(region));
  2076. */
  2077. kill(info->si_pid, SIG_REJECT);
  2078. my_stats.msg_count++;
  2079. my_stats.distance += distance(node_id,sender_id);
  2080. }
  2081. //printf("We were fucked in sig_REQ_CORES_handler node_id = %d sender_id = %d interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2082. if (fd_r != -1) close(fd_r);
  2083. free(fifo_name);
  2084. cur_time = time(NULL);
  2085. cur_t = localtime(&cur_time);
  2086. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2087. fflush(log_file);
  2088. signals_enable();
  2089. }
  2090. void sig_REP_OFFERS_handler(int signo, siginfo_t *info, void *context)
  2091. {
  2092. int fd_r, sender_id, offer_ans, i, one_core, j, old_cores_cnt;//num_of_idags, i, one_idag;
  2093. core_list *tmp_cores, *tmp_cores_prev, *tmp_cores_list;
  2094. inter_list *tmp_inter_list, *tmp_inter_prev;
  2095. offer_list *tmp_offer_list;
  2096. char *fifo_name;
  2097. signals_disable();
  2098. sender_id = get_id_from_pid(info->si_pid);
  2099. fifo_name = get_pipe_name(node_id);
  2100. cur_time = time(NULL);
  2101. cur_t = localtime(&cur_time);
  2102. fprintf(log_file, "[%d:%d:%d]: I entered sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2103. fflush(log_file);
  2104. if (core_inter_head[sender_id] == NULL)
  2105. printf("fail!\n");
  2106. else if (core_inter_head[sender_id]->type == AGENT_OFFER_SENT) {
  2107. sem_getvalue(&node_sem[node_id],&i);
  2108. fprintf(log_file, "In 7 Trying to acquire semaphore. Sem value = %d\n",i);
  2109. fflush(log_file);
  2110. kill(info->si_pid, SIG_ACK);
  2111. sem_wait(&node_sem[node_id]);
  2112. sem_getvalue(&node_sem[node_id],&i);
  2113. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2114. fflush(log_file);
  2115. my_stats.msg_count++;
  2116. my_stats.distance += distance(node_id,sender_id);
  2117. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  2118. fd_r = open(fifo_name, O_RDONLY);
  2119. if (fd_r == -1) {
  2120. perror("opening reading pipe");
  2121. signals_enable();
  2122. return;
  2123. } /*else {
  2124. fprintf(log_file, "I went through open\n");
  2125. fflush(log_file);
  2126. } */
  2127. fprintf(log_file, "Num of offers is = %d\n",core_inter_head[sender_id]->data.off_arr.num_of_offers);
  2128. fflush(log_file);
  2129. //even if i am in a far req offer, my answer will be the first
  2130. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++) {
  2131. read(fd_r, &offer_ans, sizeof(int));
  2132. fprintf(log_file, "offer_ans = %d\n",offer_ans);
  2133. fflush(log_file);
  2134. //printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  2135. //printf("I am %d and i am getting bizarre answer = %d from %d with num of offers = %d\n",node_id,offer_ans,sender_id,core_inter_head[sender_id]->data.off_arr.num_of_offers);
  2136. while (offer_ans != 0 && offer_ans != 1){
  2137. printf("I am %d and i am getting bizarre answer = %d from %d\n",node_id,offer_ans,sender_id);
  2138. read(fd_r, &offer_ans, sizeof(int));
  2139. }
  2140. if (offer_ans == 0 && my_cores != NULL) { // && my_cores != NULL
  2141. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  2142. for (tmp_cores = my_cores->next; tmp_cores != NULL; tmp_cores = tmp_cores->next)
  2143. if (tmp_cores->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  2144. fprintf(log_file,"core id = %d offered_to = %d\n",tmp_cores->core_id,tmp_cores->offered_to);
  2145. fflush(log_file);
  2146. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  2147. break;
  2148. }
  2149. if (app_state == APP_TERMINATED) {//app_terminated
  2150. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2151. if (tmp_cores_list->offered_to != -1) break;
  2152. //if (tmp_cores_list != NULL && state ) state = AGENT_ZOMBIE;
  2153. //else state = AGENT_ENDING;
  2154. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  2155. }
  2156. /*tmp_cores = my_cores;
  2157. while (tmp_cores != NULL){
  2158. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  2159. tmp_cores = tmp_cores->next;
  2160. }*/
  2161. } else if (offer_ans == 1) {//&& (!app_terminated || my_idag == -1) my_cores != NULL inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  2162. if (my_idag == -1){ //I am an idag
  2163. //if (node_id == 0) printf("qwer asdf %d\n",DDS->num_of_cores);
  2164. //if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  2165. DDS->num_of_cores -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  2166. //if (node_id == 0) printf("kariolares asdf %d\n",core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores);
  2167. //printf("I did REM\n");
  2168. } else {//I am common node
  2169. //printf("I start REM here!!!!!\n");
  2170. /*first i must get the remaining time from my cores, before i change my core list*/
  2171. if (app_state != APP_TERMINATED) { //!app_terminated
  2172. old_Speedup = my_Speedup;
  2173. old_cores_cnt = my_cores_count;
  2174. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  2175. my_app.num_of_cores = my_cores_count;
  2176. my_Speedup = Speedup(my_app, my_cores_count);
  2177. fprintf(log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  2178. fflush(log_file);
  2179. fprintf(app_log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  2180. fflush(app_log_file);
  2181. if (app_state == RUNNING) {
  2182. chk_rem_num = old_cores_cnt-1;
  2183. chk_rem_count = 0;
  2184. sum_rem_time = 0;
  2185. app_state = RESIZING;
  2186. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  2187. one_core = tmp_cores_list->core_id;
  2188. if (core_inter_head[one_core] == NULL){
  2189. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  2190. core_inter_tail[one_core] = core_inter_head[one_core];
  2191. } else {
  2192. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2193. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2194. }
  2195. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2196. core_inter_tail[one_core]->next = NULL;
  2197. if (core_inter_head[one_core]->next == NULL) {
  2198. kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  2199. my_stats.msg_count++;
  2200. my_stats.distance += distance(node_id,one_core);
  2201. } else printf("I am %d and I am doing smth else with my working node %d in send SIG_CHECK_REM_TIME in rep offers type = %d\n",
  2202. node_id,one_core,core_inter_head[one_core]->type);
  2203. }
  2204. } //else {
  2205. } else {
  2206. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  2207. my_app.num_of_cores = my_cores_count;
  2208. fprintf(log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  2209. fflush(log_file);
  2210. fprintf(app_log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  2211. fflush(app_log_file);
  2212. }
  2213. if (core_inter_head[my_idag] == NULL){
  2214. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  2215. core_inter_tail[my_idag] = core_inter_head[my_idag];
  2216. } else {
  2217. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2218. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  2219. }
  2220. core_inter_tail[my_idag]->type = IDAG_REM_CORES_DDS;
  2221. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores+1)*sizeof(int));
  2222. core_inter_tail[my_idag]->data.app_cores[0] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  2223. for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
  2224. one_core = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  2225. if (app_state != APP_TERMINATED) {
  2226. tmp_inter_prev = core_inter_head[one_core];
  2227. tmp_inter_list = core_inter_head[one_core]->next;
  2228. } else {
  2229. tmp_inter_prev = NULL;
  2230. tmp_inter_list = core_inter_head[one_core];
  2231. }
  2232. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  2233. while (tmp_inter_list != NULL)
  2234. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  2235. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  2236. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  2237. fprintf(log_file, "Removing in rem offers one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  2238. fflush(log_file);
  2239. if (tmp_inter_prev == NULL) {
  2240. core_inter_head[one_core] = core_inter_head[one_core]->next;
  2241. free(tmp_inter_list);
  2242. tmp_inter_list = core_inter_head[one_core];
  2243. } else {
  2244. tmp_inter_prev->next = tmp_inter_list->next;
  2245. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  2246. free(tmp_inter_list);
  2247. tmp_inter_list = tmp_inter_prev->next;
  2248. }
  2249. } else {
  2250. tmp_inter_prev = tmp_inter_list;
  2251. tmp_inter_list = tmp_inter_list->next;
  2252. }
  2253. if (app_state != APP_TERMINATED) {
  2254. if (core_inter_head[one_core] == NULL) {
  2255. fprintf(log_file,"No interaction with %d. Theoritically impossible\n",one_core);
  2256. fflush(log_file);
  2257. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
  2258. fprintf(log_file,"I offered my new core %d. I will clear the interaction\n",one_core);
  2259. fflush(log_file);
  2260. tmp_inter_list = core_inter_head[one_core];
  2261. core_inter_head[one_core] = core_inter_head[one_core]->next;
  2262. if (core_inter_head[one_core] == NULL) core_inter_tail[one_core] = NULL;
  2263. else send_next_signal(core_inter_head[one_core], one_core);
  2264. free(tmp_inter_list);
  2265. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE || core_inter_head[one_core]->type == APPOINT_WORK_NODE) {
  2266. fprintf(log_file,"Invalidating %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2267. fflush(log_file);
  2268. core_inter_head[one_core]->data.work_time = -1;
  2269. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2270. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2271. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2272. core_inter_tail[one_core]->next = NULL;
  2273. } else if (core_inter_head[one_core]->type == APPOINT_WORK_NODE_PENDING) {
  2274. fprintf(log_file,"Everything ok %d.\n",one_core);
  2275. fflush(log_file);
  2276. core_inter_head[one_core]->type = REMOVED_NODE_REM_TIME;
  2277. } else {
  2278. fprintf(log_file,"Another interaction with %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2279. fflush(log_file);
  2280. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2281. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2282. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2283. core_inter_tail[one_core]->next = NULL;
  2284. }
  2285. }
  2286. tmp_cores = my_cores;
  2287. tmp_cores_prev = NULL;
  2288. while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
  2289. tmp_cores_prev = tmp_cores;
  2290. tmp_cores = tmp_cores->next;
  2291. }
  2292. if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
  2293. else {
  2294. if (tmp_cores_prev == NULL) {
  2295. printf("i fucking offered my agent core!!!\n");
  2296. my_cores = my_cores->next;
  2297. } else if (tmp_cores == my_cores_tail){
  2298. my_cores_tail = tmp_cores_prev;
  2299. my_cores_tail->next = NULL;
  2300. } else tmp_cores_prev->next = tmp_cores->next;
  2301. free(tmp_cores);
  2302. }
  2303. core_inter_tail[my_idag]->data.app_cores[i] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  2304. }
  2305. core_inter_tail[my_idag]->next = NULL;
  2306. if (core_inter_head[my_idag]->next == NULL) {
  2307. kill(pid_num[my_idag], SIG_REM_CORES_DDS);
  2308. my_stats.msg_count++;
  2309. my_stats.distance += distance(node_id,my_idag);
  2310. } else printf("I did not send rem signal!\n");
  2311. //printf("At least i am coming here!!!!!\n");
  2312. cur_time = time(NULL);
  2313. cur_t = localtime(&cur_time);
  2314. fprintf(app_log_file, "[%d:%d:%d]: Removal ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  2315. fprintf(app_log_file, "my cores are:");
  2316. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  2317. //fprintf(log_file, " %d", tmp_cores_list->core_id);
  2318. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  2319. //printf(" %d",tmp_cores_list->core_id);
  2320. }
  2321. //printf("\n");
  2322. //fprintf(log_file, "\n");
  2323. //fflush(log_file);
  2324. fprintf(app_log_file, "\n");
  2325. fflush(app_log_file);
  2326. if (app_state == APP_TERMINATED) {//app_terminated
  2327. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2328. if (tmp_cores_list->offered_to != -1) break;
  2329. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  2330. }
  2331. }
  2332. /*} else if (offer_ans == 1 && app_terminated) {
  2333. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  2334. fprintf(log_file,"Initialising removal 2 new_cores_count = %d\n",my_cores_count);
  2335. fflush(log_file);
  2336. for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
  2337. tmp_cores = my_cores;
  2338. tmp_cores_prev = NULL;
  2339. while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
  2340. tmp_cores_prev = tmp_cores;
  2341. tmp_cores = tmp_cores->next;
  2342. }
  2343. if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
  2344. else {
  2345. if (tmp_cores_prev == NULL) {
  2346. printf("i fucking offered my agent core!!!\n");
  2347. my_cores = my_cores->next;
  2348. } else if (tmp_cores == my_cores_tail){
  2349. my_cores_tail = tmp_cores_prev;
  2350. my_cores_tail->next = NULL;
  2351. } else tmp_cores_prev->next = tmp_cores->next;
  2352. free(tmp_cores);
  2353. }
  2354. }
  2355. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2356. if (tmp_cores_list->offered_to != -1) break;
  2357. //if (tmp_cores_list != NULL) state = AGENT_ZOMBIE;
  2358. //else state = AGENT_ENDING;
  2359. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;*/
  2360. } else printf("I am %d and my Answer from %d different than 0 or 1 and is %d!!\n",node_id,sender_id,offer_ans);
  2361. }
  2362. sem_getvalue(&node_sem_out[sender_id],&i);
  2363. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2364. fflush(log_file);
  2365. if (sem_post(&node_sem_out[sender_id]) == -1){
  2366. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2367. perror("sem_post");
  2368. }
  2369. sem_getvalue(&node_sem_out[sender_id],&i);
  2370. fprintf(log_file, "Sem value = %d\n",i);
  2371. fflush(log_file);
  2372. //sem_post(&node_sem[node_id]);
  2373. //sem_post(&node_sem[sender_id]);
  2374. } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT){
  2375. sem_getvalue(&node_sem[node_id],&i);
  2376. fprintf(log_file, "In 8 Trying to acquire semaphore. Sem value = %d\n",i);
  2377. fflush(log_file);
  2378. kill(info->si_pid, SIG_ACK);
  2379. sem_wait(&node_sem[node_id]);
  2380. sem_getvalue(&node_sem[node_id],&i);
  2381. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2382. fflush(log_file);
  2383. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  2384. fd_r = open(fifo_name, O_RDONLY);
  2385. if (fd_r == -1) {
  2386. perror("opening reading pipe");
  2387. signals_enable();
  2388. return;
  2389. } else {
  2390. fprintf(log_file, "I went through open\n");
  2391. fflush(log_file);
  2392. }
  2393. //even if i am in a far req offer, my answer will be the first
  2394. read(fd_r, &offer_ans, sizeof(int));
  2395. printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  2396. if (offer_ans == 0) {
  2397. tmp_cores = my_cores;
  2398. while (tmp_cores != NULL){
  2399. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  2400. tmp_cores = tmp_cores->next;
  2401. }
  2402. } else if (offer_ans == 1) {//inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  2403. if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  2404. DDS->num_of_cores -= core_inter_head[sender_id]->data.my_offer.num_of_cores;
  2405. } else printf("I am %d Answer different than 0 or 1 in far from %d!!\n",node_id,sender_id);
  2406. //if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {//FAR_REQ_OFFER
  2407. /*printf("poutanares\n");
  2408. if (far_man_offers == NULL) printf("kai poytanes kai karioles\n");
  2409. else printf("gamw ths papias\n");*/
  2410. tmp_offer_list = far_man_offers;
  2411. far_man_offers = far_man_offers->next;
  2412. free(tmp_offer_list);
  2413. while (far_man_offers != NULL){
  2414. //printf("kai edw ftamw\n");
  2415. read(fd_r, &offer_ans, sizeof(int));
  2416. printf("I am node %d and far offer answer for node %d is %d\n",node_id,far_man_offers->sender,offer_ans);
  2417. if (offer_ans == 0 || offer_ans == 1){
  2418. *far_man_offers->answer = offer_ans;
  2419. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  2420. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  2421. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  2422. } else printf("Apparently lists are poutana ola\n");
  2423. } else printf("Far Answer different than 0 or 1!!\n");
  2424. tmp_offer_list = far_man_offers;
  2425. far_man_offers = far_man_offers->next;
  2426. free(tmp_offer_list);
  2427. }
  2428. sem_getvalue(&node_sem_out[sender_id],&i);
  2429. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2430. fflush(log_file);
  2431. if (sem_post(&node_sem_out[sender_id]) == -1){
  2432. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2433. perror("sem_post");
  2434. }
  2435. sem_getvalue(&node_sem_out[sender_id],&i);
  2436. fprintf(log_file, "Sem value = %d\n",i);
  2437. fflush(log_file);
  2438. //sem_post(&node_sem[node_id]);
  2439. //sem_post(&node_sem[sender_id]);
  2440. far_req_app.A=-1.0;
  2441. far_req_app.var=-1.0;
  2442. far_req_app.num_of_cores=-1;
  2443. far_req_or_sender = -1;
  2444. } else printf("I am %d and fail 2\n",node_id);
  2445. tmp_inter_list = core_inter_head[sender_id];
  2446. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2447. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2448. else send_next_signal(core_inter_head[sender_id], sender_id);
  2449. free(tmp_inter_list);
  2450. close(fd_r);
  2451. free(fifo_name);
  2452. cur_time = time(NULL);
  2453. cur_t = localtime(&cur_time);
  2454. fprintf(log_file, "[%d:%d:%d]: I ended sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2455. fflush(log_file);
  2456. signals_enable();
  2457. }
  2458. void sig_INIT_AGENT_handler(int signo, siginfo_t *info, void *context)
  2459. {
  2460. int fd_r, i, tmp, sender_id;
  2461. char *fifo_name;
  2462. core_list *tmp_core;
  2463. signals_disable();
  2464. sender_id = get_id_from_pid(info->si_pid);
  2465. fifo_name = get_pipe_name(node_id);
  2466. cur_time = time(NULL);
  2467. cur_t = localtime(&cur_time);
  2468. fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2469. fflush(log_file);
  2470. sem_getvalue(&node_sem[node_id],&i);
  2471. fprintf(log_file, "In 9 Trying to acquire semaphore. Sem value = %d\n",i);
  2472. fflush(log_file);
  2473. kill(info->si_pid, SIG_ACK);
  2474. sem_wait(&node_sem[node_id]);
  2475. sem_getvalue(&node_sem[node_id],&i);
  2476. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2477. fflush(log_file);
  2478. my_stats.msg_count++;
  2479. my_stats.distance += distance(node_id,get_id_from_pid(info->si_pid));
  2480. //printf("open10 idag_id=%d node_id=%d\n",idag_id,node_id);
  2481. fd_r = open(fifo_name, O_RDONLY);
  2482. if (fd_r == -1) {
  2483. perror("opening reading pipe");
  2484. signals_enable();
  2485. return;
  2486. } else {
  2487. fprintf(log_file, "I went through open\n");
  2488. fflush(log_file);
  2489. }
  2490. if (my_app.num_of_cores != -1) { //Very very very important!!!
  2491. printf ("I am already managing an app!!! Fail!!!!\n");
  2492. while (my_cores != NULL){
  2493. tmp_core = my_cores;
  2494. my_cores = my_cores->next;
  2495. free(tmp_core);
  2496. }
  2497. my_cores_tail = NULL;
  2498. my_cores_count = 0;
  2499. }
  2500. read(fd_r, &my_app, sizeof(app));
  2501. my_cores_count = my_app.num_of_cores;//+1;
  2502. if (my_cores == NULL) {
  2503. my_cores = (core_list *) malloc(sizeof(core_list));
  2504. my_cores_tail = my_cores;
  2505. } else {
  2506. printf("My cores still not fucking null!!\n");
  2507. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2508. my_cores_tail = my_cores_tail->next;
  2509. }
  2510. my_cores_tail->core_id = node_id;
  2511. my_cores_tail->offered_to = -1;
  2512. my_cores_tail->next = NULL;
  2513. //I want myself to be first in my_cores list
  2514. for (i=0; i<my_app.num_of_cores; i++){
  2515. read(fd_r, &tmp, sizeof(int));
  2516. if (tmp != node_id){
  2517. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2518. my_cores_tail = my_cores_tail->next;
  2519. //read(fd_r, &my_cores_tail->core_id, sizeof(int));
  2520. my_cores_tail->core_id = tmp;
  2521. my_cores_tail->offered_to = -1;
  2522. my_cores_tail->next = NULL;
  2523. }
  2524. }
  2525. ////sem_post(&node_sem[sender_id]);
  2526. printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  2527. ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  2528. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  2529. if (core_inter_head[my_idag] == NULL){
  2530. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  2531. core_inter_tail[my_idag] = core_inter_head[my_idag];
  2532. } else {
  2533. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2534. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  2535. }
  2536. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  2537. //core_inter_tail[my_idag]->data.my_offer = core_inter_head[sender_id]->data.my_offer;
  2538. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  2539. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  2540. //for(i=1; i<=my_cores_count; i++)
  2541. // core_inter_tail[my_idag]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  2542. tmp_core = my_cores;
  2543. i=1;
  2544. while (tmp_core != NULL){
  2545. core_inter_tail[my_idag]->data.app_cores[i] = tmp_core->core_id;
  2546. tmp_core = tmp_core->next;
  2547. i++;
  2548. }
  2549. core_inter_tail[my_idag]->next = NULL;
  2550. if (core_inter_head[my_idag]->next == NULL) {
  2551. kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  2552. my_stats.msg_count++;
  2553. my_stats.distance += distance(node_id,my_idag);
  2554. } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);
  2555. if (my_agent != -1) {
  2556. printf("I am %d and i do this agent switch\n",node_id);
  2557. //if (state != IDLE_CORE) {
  2558. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  2559. if (core_inter_head[my_agent] == NULL){
  2560. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  2561. core_inter_tail[my_agent] = core_inter_head[my_agent];
  2562. } else {
  2563. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  2564. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  2565. }
  2566. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  2567. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  2568. core_inter_tail[my_agent]->next = NULL;
  2569. }
  2570. upper_work_bound = 0;
  2571. my_agent = -1;
  2572. }
  2573. //sem_post(&node_sem[node_id]);
  2574. sem_getvalue(&node_sem_out[sender_id],&i);
  2575. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2576. fflush(log_file);
  2577. if (sem_post(&node_sem_out[sender_id]) == -1){
  2578. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2579. perror("sem_post");
  2580. }
  2581. sem_getvalue(&node_sem_out[sender_id],&i);
  2582. fprintf(log_file, "Sem value = %d\n",i);
  2583. fflush(log_file);
  2584. printf("Init ok!! my_cores_count = %d app_cores=%d app_id=%d\n",my_cores_count,my_app.num_of_cores,my_app.id);
  2585. cur_time = time(NULL);
  2586. cur_t = localtime(&cur_time);
  2587. fprintf(log_file, "[%d:%d:%d]: Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count,my_app.id);
  2588. fprintf(log_file, "my cores are:");
  2589. for (tmp_core=my_cores; tmp_core!=NULL; tmp_core=tmp_core->next) fprintf(log_file, " %d", tmp_core->core_id);
  2590. fprintf(log_file, "\n");
  2591. printf("I am %d and about to do an selfopt agent with my state before change = %d\n",node_id,state);
  2592. if (state == IDLE_INIT_MAN || state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  2593. fprintf(log_file,"I have pending init\n");
  2594. //state = AGENT_INIT_STATE_INIT_INTERRUPTED;
  2595. pending_state = state;
  2596. state = AGENT_INIT_STATE;
  2597. } else state = AGENT_INIT_STATE;
  2598. close(fd_r);
  2599. free(fifo_name);
  2600. cur_time = time(NULL);
  2601. cur_t = localtime(&cur_time);
  2602. fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2603. fflush(log_file);
  2604. signals_enable();
  2605. }
  2606. //an mou erthei to shma apo allon idag prepei apla na valw, an mou erthei apo common node tote prepei na upologisw
  2607. void sig_ADD_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
  2608. {
  2609. int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
  2610. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2611. int *tmp_list, tmp_cnt, tmp_idag;
  2612. char *fifo_name;
  2613. core_list *tmp_cores;//, *tmp_cores_list;
  2614. DDS_list *tmp_DDS;
  2615. signals_disable();
  2616. sender_id = get_id_from_pid(info->si_pid);
  2617. fifo_name = get_pipe_name(node_id);
  2618. cur_time = time(NULL);
  2619. cur_t = localtime(&cur_time);
  2620. fprintf(log_file, "[%d:%d:%d]: I entered sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2621. fflush(log_file);
  2622. sem_getvalue(&node_sem[node_id],&i);
  2623. fprintf(log_file, "In 10 Trying to acquire semaphore. Sem value = %d\n",i);
  2624. fflush(log_file);
  2625. kill(info->si_pid, SIG_ACK);
  2626. sem_wait(&node_sem[node_id]);
  2627. sem_getvalue(&node_sem[node_id],&i);
  2628. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2629. fflush(log_file);
  2630. my_stats.msg_count++;
  2631. my_stats.distance += distance(node_id,sender_id);
  2632. //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
  2633. fd_r = open(fifo_name, O_RDONLY);
  2634. if (fd_r == -1) {
  2635. perror("opening reading pipe");
  2636. signals_enable();
  2637. return;
  2638. } else {
  2639. fprintf(log_file, "I went through open\n");
  2640. fflush(log_file);
  2641. }
  2642. is_sender_idag = 0;
  2643. for (i=0; i<num_idags; i++)
  2644. if (idag_id_arr[i] == sender_id){
  2645. is_sender_idag = 1;
  2646. break;
  2647. }
  2648. //printf("I am in add with is_sender_idag = %d\n",is_sender_idag);
  2649. if (is_sender_idag == 0){
  2650. read(fd_r, &nodes_cnt, sizeof(int));
  2651. while (nodes_cnt <=0){
  2652. printf("i am %d and i read %d in nodes_cnt from %d\n",node_id,nodes_cnt,sender_id);
  2653. read(fd_r, &nodes_cnt, sizeof(int));
  2654. }
  2655. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2656. for (i=0; i<nodes_cnt; i++)
  2657. read(fd_r, &nodes_to_process[i], sizeof(int));
  2658. ////sem_post(&node_sem[sender_id]);
  2659. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  2660. printf("I am %d in add cores with sender %d and nodes_cnt = %d\n",node_id,sender_id,nodes_cnt);
  2661. while (processed_cnt < nodes_cnt){
  2662. tmp_cnt = 0;
  2663. tmp_idag = -1;
  2664. for (i=0; i<nodes_cnt; i++){
  2665. if (processed_cnt == nodes_cnt) break;
  2666. if (nodes_to_process[i] == -1) continue;
  2667. else {
  2668. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  2669. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  2670. else {
  2671. tmp_list[tmp_cnt++] = nodes_to_process[i];
  2672. nodes_to_process[i] = -1;
  2673. processed_cnt++;
  2674. }
  2675. }
  2676. }
  2677. if (tmp_idag != node_id){
  2678. if (core_inter_head[tmp_idag] == NULL){
  2679. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  2680. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  2681. } else {
  2682. printf("Adding to DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  2683. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2684. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  2685. }
  2686. printf("In ADD tmp_idag = %d tmp_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,sender_id,node_id);
  2687. core_inter_tail[tmp_idag]->type = IDAG_ADD_CORES_DDS;
  2688. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  2689. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  2690. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  2691. for (j=1; j<= tmp_cnt; j++)
  2692. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  2693. core_inter_tail[tmp_idag]->next = NULL;
  2694. if (core_inter_head[tmp_idag]->next == NULL) {
  2695. kill(pid_num[tmp_idag], SIG_ADD_CORES_DDS);
  2696. my_stats.msg_count++;
  2697. my_stats.distance += distance(node_id,tmp_idag);
  2698. }
  2699. } else {
  2700. printf("In ADD same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  2701. new_agent_id = sender_id;
  2702. for (i=0; i<tmp_cnt; i++){//nodes_cnt
  2703. fprintf(log_file,"I am importing node %d\n",tmp_list[i]);
  2704. fflush(log_file);
  2705. tmp_cores = my_cores;
  2706. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  2707. tmp_cores = tmp_cores->next;
  2708. if (tmp_cores->offered_to == -1) {
  2709. fprintf(log_file,"Node %d was offered to nobody\n",tmp_list[i]);
  2710. fflush(log_file);
  2711. DDS->num_of_cores--;
  2712. tmp_cores->offered_to = new_agent_id;
  2713. } else if (tmp_cores->offered_to != new_agent_id) {
  2714. fprintf(log_file,"Node %d is offered to %d\n",tmp_list[i],tmp_cores->offered_to);
  2715. fflush(log_file);
  2716. tmp_cores->offered_to = new_agent_id;
  2717. }
  2718. }
  2719. tmp_DDS = DDS;
  2720. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  2721. tmp_DDS = tmp_DDS->next;
  2722. if (tmp_DDS != NULL)
  2723. tmp_DDS->num_of_cores += nodes_cnt;
  2724. else {
  2725. //printf("I am in here DDSing nodes_cnt=%d\n",nodes_cnt);
  2726. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  2727. DDS_tail = DDS_tail->next;
  2728. DDS_tail->agent_id = new_agent_id;
  2729. DDS_tail->num_of_cores = nodes_cnt;
  2730. DDS_tail->next = NULL;
  2731. DDS_count++;
  2732. }
  2733. }
  2734. }
  2735. } else {
  2736. read(fd_r, &nodes_cnt, sizeof(int));
  2737. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2738. for (i=0; i<nodes_cnt; i++)
  2739. read(fd_r, &nodes_to_process[i], sizeof(int));
  2740. read(fd_r, &new_agent_id, sizeof(int));
  2741. ////sem_post(&node_sem[sender_id]);
  2742. //printf("new agent id = %d\n",new_agent_id);
  2743. for (i=0; i<nodes_cnt; i++){
  2744. //printf("Node %d is %d\n",i,nodes_to_process[i]);
  2745. fprintf(log_file,"I am importing node %d\n",nodes_to_process[i]);
  2746. fflush(log_file);
  2747. tmp_cores = my_cores;
  2748. while (tmp_cores->core_id != nodes_to_process[i])
  2749. tmp_cores = tmp_cores->next;
  2750. if (tmp_cores->offered_to == -1) {
  2751. fprintf(log_file,"Node %d was offered to nobody\n",nodes_to_process[i]);
  2752. fflush(log_file);
  2753. DDS->num_of_cores--;
  2754. tmp_cores->offered_to = new_agent_id;
  2755. } else if (tmp_cores->offered_to != new_agent_id) {
  2756. fprintf(log_file,"Node %d is offered to %d\n",nodes_to_process[i],tmp_cores->offered_to);
  2757. fflush(log_file);
  2758. tmp_cores->offered_to = new_agent_id;
  2759. }
  2760. //tmp_cores->offered_to = new_agent_id;
  2761. }
  2762. tmp_DDS = DDS;
  2763. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  2764. tmp_DDS = tmp_DDS->next;
  2765. if (tmp_DDS != NULL)
  2766. tmp_DDS->num_of_cores += nodes_cnt;
  2767. else {
  2768. if (DDS_count == 1 && DDS != DDS_tail) printf("I am %d kai ta hpiame asxhma!\n",node_id);
  2769. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  2770. DDS_tail = DDS_tail->next;
  2771. DDS_tail->agent_id = new_agent_id;
  2772. DDS_tail->num_of_cores = nodes_cnt;
  2773. DDS_tail->next = NULL;
  2774. DDS_count++;
  2775. //printf("I did this with node_id = %d, new_agent_id = %d\n",node_id,new_agent_id);
  2776. }
  2777. }
  2778. //sem_post(&node_sem[node_id]);
  2779. sem_getvalue(&node_sem_out[sender_id],&i);
  2780. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2781. fflush(log_file);
  2782. if (sem_post(&node_sem_out[sender_id]) == -1){
  2783. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2784. perror("sem_post");
  2785. }
  2786. sem_getvalue(&node_sem_out[sender_id],&i);
  2787. fprintf(log_file, "Sem value = %d\n",i);
  2788. fflush(log_file);
  2789. /*for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2790. if (tmp_cores_list->offered_to != -1) my_stats.cores_utilized++;
  2791. printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  2792. printf("Number of agents in region = %d\n",DDS_count);
  2793. i=0;
  2794. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2795. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2796. i++;
  2797. }*/
  2798. my_stats.times_accessed++;
  2799. printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  2800. printf("Number of agents in region = %d\n",DDS_count);
  2801. printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
  2802. i=1;
  2803. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2804. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2805. my_stats.cores_utilized += tmp_DDS->num_of_cores;
  2806. i++;
  2807. }
  2808. close(fd_r);
  2809. free(fifo_name);
  2810. cur_time = time(NULL);
  2811. cur_t = localtime(&cur_time);
  2812. fprintf(log_file, "[%d:%d:%d]: I ended sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2813. fflush(log_file);
  2814. signals_enable();
  2815. }
  2816. void sig_REM_CORES_DDS_handler(int signo, siginfo_t *info, void *context)
  2817. {
  2818. int fd_r, i, is_sender_idag, sender_id, j, new_agent_id;
  2819. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2820. int *tmp_list, tmp_cnt, tmp_idag;
  2821. char *fifo_name;
  2822. DDS_list *tmp_DDS,*tmp_DDS_prev;
  2823. signals_disable();
  2824. sender_id = get_id_from_pid(info->si_pid);
  2825. fifo_name = get_pipe_name(node_id);
  2826. cur_time = time(NULL);
  2827. cur_t = localtime(&cur_time);
  2828. fprintf(log_file, "[%d:%d:%d]: I entered sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2829. fflush(log_file);
  2830. sem_getvalue(&node_sem[node_id],&i);
  2831. fprintf(log_file, "In 11 Trying to acquire semaphore. Sem value = %d\n",i);
  2832. fflush(log_file);
  2833. kill(info->si_pid, SIG_ACK);
  2834. sem_wait(&node_sem[node_id]);
  2835. sem_getvalue(&node_sem[node_id],&i);
  2836. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2837. fflush(log_file);
  2838. my_stats.msg_count++;
  2839. my_stats.distance += distance(node_id,sender_id);
  2840. //printf("open11 idag_id=%d node_id=%d\n",idag_id,node_id);
  2841. fd_r = open(fifo_name, O_RDONLY);
  2842. if (fd_r == -1) {
  2843. perror("opening reading pipe");
  2844. signals_enable();
  2845. return;
  2846. } else {
  2847. fprintf(log_file, "I went through open\n");
  2848. fflush(log_file);
  2849. }
  2850. is_sender_idag = 0;
  2851. for (i=0; i<num_idags; i++)
  2852. if (idag_id_arr[i] == sender_id){
  2853. is_sender_idag = 1;
  2854. break;
  2855. }
  2856. //printf("I am in rem with is_sender_idag = %d\n",is_sender_idag);
  2857. if (is_sender_idag == 0){
  2858. read(fd_r, &nodes_cnt, sizeof(int));
  2859. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2860. for (i=0; i<nodes_cnt; i++)
  2861. read(fd_r, &nodes_to_process[i], sizeof(int));
  2862. ////sem_post(&node_sem[sender_id]);
  2863. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  2864. while (processed_cnt < nodes_cnt){
  2865. tmp_cnt = 0;
  2866. tmp_idag = -1;
  2867. for (i=0; i<nodes_cnt; i++){
  2868. if (processed_cnt == nodes_cnt) break;
  2869. if (nodes_to_process[i] == -1) continue;
  2870. else {
  2871. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  2872. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  2873. else {
  2874. tmp_list[tmp_cnt++] = nodes_to_process[i];
  2875. nodes_to_process[i] = -1;
  2876. processed_cnt++;
  2877. }
  2878. }
  2879. }
  2880. if (tmp_idag != node_id){
  2881. if (core_inter_head[tmp_idag] == NULL){
  2882. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  2883. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  2884. } else {
  2885. printf("Removing from DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  2886. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2887. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  2888. }
  2889. //printf("In REM tmp_idag = %d tmp_cnt = %d sender_id = %d idag_id = %d\n",tmp_idag,tmp_cnt,sender_id,idag_id);
  2890. core_inter_tail[tmp_idag]->type = IDAG_REM_CORES_DDS;
  2891. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  2892. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  2893. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  2894. for (j=1; j<= tmp_cnt; j++)
  2895. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  2896. core_inter_tail[tmp_idag]->next = NULL;
  2897. if (core_inter_head[tmp_idag]->next == NULL) {
  2898. kill(pid_num[tmp_idag], SIG_REM_CORES_DDS);
  2899. my_stats.msg_count++;
  2900. my_stats.distance += distance(node_id,tmp_idag);
  2901. }
  2902. } else {
  2903. printf("In REM node_id = %d same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d \n",node_id,tmp_idag,tmp_cnt,nodes_cnt,sender_id);
  2904. new_agent_id = sender_id;
  2905. tmp_DDS = DDS;
  2906. tmp_DDS_prev = NULL;
  2907. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2908. tmp_DDS_prev = tmp_DDS;
  2909. tmp_DDS = tmp_DDS->next;
  2910. }
  2911. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  2912. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  2913. else {
  2914. tmp_DDS->num_of_cores -= nodes_cnt;
  2915. if (tmp_DDS->num_of_cores == 0){
  2916. DDS_count--;
  2917. if (tmp_DDS == DDS_tail){
  2918. DDS_tail = tmp_DDS_prev;
  2919. DDS_tail->next = NULL;
  2920. } else tmp_DDS_prev->next = tmp_DDS->next;
  2921. free(tmp_DDS);
  2922. }
  2923. }
  2924. }
  2925. }
  2926. } else { //den afairw tous purhnes apo thn core list giati mporei na exoun hdh ginei add
  2927. read(fd_r, &nodes_cnt, sizeof(int));
  2928. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2929. for (i=0; i<nodes_cnt; i++)
  2930. read(fd_r, &nodes_to_process[i], sizeof(int));
  2931. read(fd_r, &new_agent_id, sizeof(int));
  2932. ////sem_post(&node_sem[sender_id]);
  2933. //printf("I am in the second rem with new agent id = %d and nodes_cnt = %d\n",new_agent_id,nodes_cnt);
  2934. tmp_DDS = DDS;
  2935. tmp_DDS_prev = NULL;
  2936. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2937. tmp_DDS_prev = tmp_DDS;
  2938. tmp_DDS = tmp_DDS->next;
  2939. }
  2940. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  2941. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  2942. else {
  2943. tmp_DDS->num_of_cores -= nodes_cnt;
  2944. if (tmp_DDS->num_of_cores == 0){
  2945. DDS_count--;
  2946. if (tmp_DDS == DDS_tail){
  2947. DDS_tail = tmp_DDS_prev;
  2948. DDS_tail->next = NULL;
  2949. } else tmp_DDS_prev->next = tmp_DDS->next;
  2950. free(tmp_DDS);
  2951. }
  2952. }
  2953. }
  2954. //sem_post(&node_sem[node_id]);
  2955. sem_getvalue(&node_sem_out[sender_id],&i);
  2956. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2957. fflush(log_file);
  2958. if (sem_post(&node_sem_out[sender_id]) == -1){
  2959. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2960. perror("sem_post");
  2961. }
  2962. sem_getvalue(&node_sem_out[sender_id],&i);
  2963. fprintf(log_file, "Sem value = %d\n",i);
  2964. fflush(log_file);
  2965. printf("I am %d Removing ended well! with sender_id=%d!\n",node_id,sender_id);
  2966. printf("Number of agents in region = %d\n",DDS_count);
  2967. i=0;
  2968. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2969. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2970. i++;
  2971. }
  2972. close(fd_r);
  2973. free(fifo_name);
  2974. cur_time = time(NULL);
  2975. cur_t = localtime(&cur_time);
  2976. fprintf(log_file, "[%d:%d:%d]: I ended sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2977. fflush(log_file);
  2978. signals_enable();
  2979. }
  2980. void sig_FAR_REQ_handler(int signo, siginfo_t *info, void *context)
  2981. {
  2982. int fd_r=-1, sender_id, i, one_idag, *idags_in_reg, agent_id, num_of_offers, j, far_req_man, one_far_sender;//num_of_idags,
  2983. char *fifo_name;
  2984. region cur_reg;
  2985. offer one_offer;
  2986. inter_list *tmp_inter_list, *tmp_inter_prev;
  2987. DDS_list *tmp_DDS;
  2988. offer_list *tmp_offer_list, *tmp_offer_prev;
  2989. app one_app;
  2990. signals_disable();
  2991. sender_id = get_id_from_pid(info->si_pid);
  2992. fifo_name = get_pipe_name(node_id);
  2993. cur_time = time(NULL);
  2994. cur_t = localtime(&cur_time);
  2995. fprintf(log_file, "[%d:%d:%d]: I entered sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
  2996. fflush(log_file);
  2997. //I am the node that is far.
  2998. //I keep that as NULL because this must be the way, or else why is he sending me smth if we are allready communicating?
  2999. if (core_inter_head[sender_id] == NULL && signo == SIG_FAR_REQ && my_idag != -1) {
  3000. printf("I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
  3001. fprintf(log_file,"I am %d and i think i cought a stray far_manager reply from %d. My current state is %d\n",node_id,sender_id,state);
  3002. fflush(log_file);
  3003. kill(info->si_pid, SIG_REJECT);
  3004. my_stats.msg_count++;
  3005. my_stats.distance += distance(node_id,sender_id);
  3006. } else if (core_inter_head[sender_id] == NULL && (signo == SIG_INIT_FAR_REQ || (signo == SIG_FAR_REQ && my_idag == -1))) {
  3007. sem_getvalue(&node_sem[node_id],&i);
  3008. fprintf(log_file, "In 12 Trying to acquire semaphore. Sem value = %d\n",i);
  3009. fflush(log_file);
  3010. kill(info->si_pid, SIG_ACK);
  3011. sem_wait(&node_sem[node_id]);
  3012. sem_getvalue(&node_sem[node_id],&i);
  3013. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3014. fflush(log_file);
  3015. my_stats.msg_count++;
  3016. my_stats.distance += distance(node_id,sender_id);
  3017. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  3018. //printf("open12 idag_id=%d node_id=%d\n",idag_id,node_id);
  3019. fd_r = open(fifo_name, O_RDONLY);
  3020. if (fd_r == -1) {
  3021. perror("opening reading pipe");
  3022. signals_enable();
  3023. return;
  3024. } /*else {
  3025. fprintf(log_file, "I went through open\n");
  3026. fflush(log_file);
  3027. } */
  3028. if (signo == SIG_INIT_FAR_REQ) {
  3029. //far_req_or_sender = sender_id;
  3030. one_far_sender = sender_id;
  3031. //read(fd_r, &far_req_app, sizeof(app));
  3032. read(fd_r, &one_app, sizeof(app));
  3033. read(fd_r, &cur_reg, sizeof(region));
  3034. if (core_inter_head[one_far_sender] == NULL){
  3035. core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
  3036. core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
  3037. } else {
  3038. core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  3039. core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
  3040. }
  3041. core_inter_tail[one_far_sender]->type = REP_FAR_INIT_REQ;
  3042. if (my_idag != -1) {
  3043. far_req_man = my_idag;
  3044. if (core_inter_head[far_req_man] == NULL){
  3045. core_inter_head[far_req_man] = (inter_list *) malloc(sizeof(inter_list));
  3046. core_inter_tail[far_req_man] = core_inter_head[far_req_man];
  3047. } else {
  3048. core_inter_tail[far_req_man]->next = (inter_list *) malloc(sizeof(inter_list));
  3049. core_inter_tail[far_req_man] = core_inter_tail[far_req_man]->next;
  3050. }
  3051. //far_req_or_sender = one_far_sender;
  3052. //far_req_app = one_app;
  3053. core_inter_tail[far_req_man]->type = FAR_REQ_MAN_APPOINT;
  3054. core_inter_tail[far_req_man]->data.far_req.orig_sender = one_far_sender;
  3055. core_inter_tail[far_req_man]->data.far_req.far_app = one_app;
  3056. core_inter_tail[far_req_man]->data.far_req.reg = cur_reg;
  3057. core_inter_tail[far_req_man]->next = NULL;
  3058. //core_inter_tail[far_req_or_sender]->data.far_req_man = my_idag;
  3059. } else {
  3060. far_req_man = node_id;
  3061. if (far_req_or_sender == -1) {
  3062. far_req_or_sender = one_far_sender;
  3063. far_req_app = one_app;
  3064. }
  3065. }
  3066. //core_inter_tail[one_far_sender]->data.far_req_man = node_id;
  3067. core_inter_tail[one_far_sender]->data.far_req_man = far_req_man;
  3068. core_inter_tail[one_far_sender]->next = NULL;
  3069. if (core_inter_head[one_far_sender]->next == NULL) {
  3070. kill(pid_num[one_far_sender], SIG_INIT_FAR_REQ);
  3071. my_stats.msg_count++;
  3072. my_stats.distance += distance(node_id,one_far_sender);
  3073. } else printf("I am %d and did not send %d one_far_sender init_far_req in here. interaction = %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
  3074. } else { //SIG_FAR_REQ
  3075. //read(fd_r, &one_far_sender, sizeof(int));
  3076. //read(fd_r, &far_req_app, sizeof(app));
  3077. read(fd_r, &one_far_sender, sizeof(int));
  3078. read(fd_r, &one_app, sizeof(app));
  3079. if (far_req_or_sender == -1) {
  3080. far_req_or_sender = one_far_sender;
  3081. far_req_app = one_app;
  3082. //printf("I am %d and i come here far_req_or_sender = %d\n",node_id,far_req_or_sender);
  3083. } //else printf("I am %d and karioles far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
  3084. read(fd_r, &cur_reg, sizeof(region));
  3085. while (cur_reg.C > X_max * Y_max){
  3086. printf("I am %d kai mas taizei malakies o %d\n",node_id,sender_id);
  3087. read(fd_r, &cur_reg, sizeof(region));
  3088. }
  3089. fprintf(log_file,"one_far_sender=%d far_req_or_sender = %d\n",one_far_sender,far_req_or_sender);
  3090. fflush(log_file);
  3091. }
  3092. if (signo == SIG_FAR_REQ || (signo == SIG_INIT_FAR_REQ && far_req_man == node_id)) {
  3093. if (far_req_or_sender == one_far_sender && far_man_offers == NULL) {
  3094. printf("I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
  3095. fprintf(log_file,"I am node with id %d I am to far check-manage region C=%d r=%d for %d\n",node_id,cur_reg.C,cur_reg.r,far_req_or_sender);
  3096. fflush(log_file);
  3097. //my offer
  3098. if (far_man_offers == NULL){
  3099. far_list_count = 1;
  3100. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  3101. far_man_offers_tail = far_man_offers;
  3102. } else printf ("I am %d Far man offers list not NULL far_req_or_sender = %d one_far_sender = %d\n",node_id,far_req_or_sender,one_far_sender);
  3103. far_man_offers_tail->off.offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  3104. far_man_offers_tail->off.num_of_cores = offer_cores(my_cores, far_req_app, cur_reg, far_man_offers_tail->off.offered_cores, far_req_or_sender);
  3105. my_stats.comp_effort++;
  3106. //if (my_idag == -1)
  3107. far_man_offers_tail->off.spd_loss = 0.0;
  3108. //else far_man_offers_tail->off.spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-far_man_offers_tail->off.num_of_cores);
  3109. far_man_offers_tail->sender = node_id;
  3110. far_man_offers_tail->next = NULL;
  3111. //far_list_count++;
  3112. cur_time = time(NULL);
  3113. cur_t = localtime(&cur_time);
  3114. fprintf(log_file, "[%d:%d:%d]: I added my offer in far_man_offers list\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  3115. fflush(log_file);
  3116. idags_in_reg = (int *) malloc((num_idags+1)*sizeof(int));
  3117. get_reg_idags(cur_reg, idags_in_reg);
  3118. for (i=0; i<num_idags; i++)
  3119. if (idags_in_reg[i] && idag_id_arr[i] != node_id){
  3120. one_idag = idag_id_arr[i];
  3121. if (core_inter_head[one_idag] == NULL){
  3122. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  3123. core_inter_tail[one_idag] = core_inter_head[one_idag];
  3124. } else {
  3125. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  3126. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  3127. }
  3128. //if (signo == SIG_FAR_REQ)
  3129. //else core_inter_tail[one_idag]->type = FAR_INIT_IDAG_REQ_DDS;
  3130. core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
  3131. core_inter_tail[one_idag]->data.reg = cur_reg;
  3132. core_inter_tail[one_idag]->next = NULL;
  3133. if (core_inter_head[one_idag]->next == NULL) {
  3134. kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  3135. my_stats.msg_count += 2;
  3136. my_stats.distance += 2 * distance(node_id,one_idag);
  3137. } else printf("I am %d and i want far dds from %d but interaction is %d\n",node_id,one_idag,core_inter_head[one_idag]->type);
  3138. }
  3139. //printf("uiou 0 qw %d num_of_idags = %d idag_id=%d\n",idags_in_reg[idag_id],idags_in_reg[num_idags],idag_id);
  3140. //for (i=0; i<num_idags; i++)
  3141. //printf("idag i=%d node_num = %d idags_in_reg = %d\n",i,idag_id_arr[i],idags_in_reg[i]);
  3142. //asking offers from other agents in my DDS
  3143. tmp_DDS = DDS->next;
  3144. if (tmp_DDS != NULL) {
  3145. while (tmp_DDS != NULL){
  3146. agent_id = tmp_DDS->agent_id;
  3147. if (core_inter_head[agent_id] == NULL){
  3148. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3149. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3150. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  3151. core_inter_tail[agent_id]->data.reg = cur_reg;//core_inter_head[sender_id]->data.reg;
  3152. core_inter_tail[agent_id]->next = NULL;
  3153. //if (core_inter_head[agent_id]->next == NULL) {
  3154. kill(pid_num[agent_id], SIG_REQ_CORES);
  3155. my_stats.msg_count++;
  3156. my_stats.distance += distance(node_id,agent_id);
  3157. //} else printf("!!!!! I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
  3158. } else {
  3159. printf("I did not send I am %d and in sending local-far offer to %d i have interaction %d\n",node_id,agent_id,core_inter_head[agent_id]->type);
  3160. //core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3161. //core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3162. }
  3163. tmp_DDS = tmp_DDS->next;
  3164. }
  3165. if (state != IDLE_CHK_APP_FILE) {
  3166. its.it_value.tv_nsec = 250 * MS;
  3167. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error2\n");
  3168. state = IDLE_FAR_MAN;
  3169. } else time_for_farman = 25;
  3170. } else if (idags_in_reg[num_idags] == 1){//i am the only idag in region with no agents
  3171. if (core_inter_head[far_req_or_sender] == NULL){
  3172. core_inter_head[far_req_or_sender] = (inter_list *) malloc(sizeof(inter_list));
  3173. core_inter_tail[far_req_or_sender] = core_inter_head[far_req_or_sender];
  3174. } else {
  3175. core_inter_tail[far_req_or_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  3176. core_inter_tail[far_req_or_sender] = core_inter_tail[far_req_or_sender]->next;
  3177. }
  3178. core_inter_tail[far_req_or_sender]->type = FAR_REQ_OFFER;
  3179. core_inter_tail[far_req_or_sender]->data.my_offer = far_man_offers->off;
  3180. core_inter_tail[far_req_or_sender]->next = NULL;
  3181. if (core_inter_head[far_req_or_sender]->next == NULL) {
  3182. kill(pid_num[far_req_or_sender],SIG_FAR_REQ);
  3183. my_stats.msg_count++;
  3184. my_stats.distance += distance(node_id,far_req_or_sender);
  3185. } else printf("I am %d and i am doing smth else with far_req_or_sender %d interaction %d\n",node_id,far_req_or_sender,core_inter_head[far_req_or_sender]->type);
  3186. } else {
  3187. if (state != IDLE_CHK_APP_FILE) {
  3188. its.it_value.tv_nsec = 250 * MS;
  3189. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error3\n");
  3190. state = IDLE_FAR_MAN;
  3191. } else time_for_farman = 25;
  3192. }
  3193. } else if (far_req_or_sender != one_far_sender) {
  3194. fprintf(log_file,"I am node with id %d and I am allready far managing %d so i am going to send %d only my offer\n",node_id,far_req_or_sender,one_far_sender);
  3195. fflush(log_file);
  3196. if (core_inter_head[one_far_sender] == NULL){
  3197. core_inter_head[one_far_sender] = (inter_list *) malloc(sizeof(inter_list));
  3198. core_inter_tail[one_far_sender] = core_inter_head[one_far_sender];
  3199. core_inter_tail[one_far_sender]->type = REP_AGENT_REQ_CORES;
  3200. core_inter_tail[one_far_sender]->data.off_arr.num_of_offers = 1;
  3201. core_inter_tail[one_far_sender]->data.off_arr.offer_arr = (offer *) malloc(core_inter_tail[one_far_sender]->data.off_arr.num_of_offers * sizeof(offer));
  3202. core_inter_tail[one_far_sender]->next = NULL;
  3203. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  3204. j = offer_cores(my_cores, one_app, cur_reg, core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].offered_cores, one_far_sender);
  3205. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].num_of_cores = j;
  3206. my_stats.comp_effort++;
  3207. core_inter_tail[one_far_sender]->data.off_arr.offer_arr[0].spd_loss = 0.0;
  3208. i=0;
  3209. fprintf(log_file, "I offered in far only %d %d cores: ",one_far_sender,core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores);
  3210. for (j=0; j<core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].num_of_cores; j++)
  3211. fprintf(log_file, "%d, ",core_inter_tail[one_far_sender]->data.off_arr.offer_arr[i].offered_cores[j]);
  3212. fprintf(log_file, "\n");
  3213. fflush(log_file);
  3214. //get_reg_idags(cur_reg, core_inter_tail[one_far_sender]->data.idags_in_reg);
  3215. //if (core_inter_head[one_far_sender]->next == NULL) {
  3216. kill(pid_num[one_far_sender], SIG_FAR_REQ);
  3217. my_stats.msg_count++;
  3218. my_stats.distance += distance(node_id,one_far_sender);
  3219. //} else printf("i am %d and Apparently not null interaction in far only=%d\n",node_id,core_inter_head[one_far_sender]->type);
  3220. } else {
  3221. fprintf(log_file,"den to evala I am %d with one_far_sender %d in only my offer and interaction is %d\n",node_id,one_far_sender,core_inter_head[one_far_sender]->type);
  3222. fflush(log_file);
  3223. //core_inter_tail[one_far_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  3224. //core_inter_tail[one_far_sender] = core_inter_tail[one_far_sender]->next;
  3225. }
  3226. //far_man_agent_count = DDS_count;
  3227. //printf("uioy 1 far_list_count = %d far_man_agent_count=%d\n",far_list_count,far_man_agent_count);
  3228. } else printf("I am node with id %d and I am allready far managing %d so i am going to abort re managing him\n",node_id,far_req_or_sender);
  3229. } else printf("I am node with id %d and I am to far check region C=%d r=%d with sender %d\n",node_id,cur_reg.C,cur_reg.r,sender_id);
  3230. //sem_post(&node_sem[node_id]);
  3231. sem_getvalue(&node_sem_out[sender_id],&i);
  3232. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3233. fflush(log_file);
  3234. if (sem_post(&node_sem_out[sender_id]) == -1){
  3235. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3236. perror("sem_post");
  3237. }
  3238. sem_getvalue(&node_sem_out[sender_id],&i);
  3239. fprintf(log_file, "Sem value = %d\n",i);
  3240. fflush(log_file);
  3241. } else if (core_inter_head[sender_id]->type == FAR_INIT_REQ && signo == SIG_INIT_FAR_REQ) {//in original manager change who the far manager is
  3242. sem_getvalue(&node_sem[node_id],&i);
  3243. fprintf(log_file, "In 13 Trying to acquire semaphore. Sem value = %d\n",i);
  3244. fflush(log_file);
  3245. kill(info->si_pid, SIG_ACK);
  3246. sem_wait(&node_sem[node_id]);
  3247. sem_getvalue(&node_sem[node_id],&i);
  3248. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3249. fflush(log_file);
  3250. my_stats.msg_count++;
  3251. my_stats.distance += distance(node_id,sender_id);
  3252. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  3253. //printf("open13 idag_id=%d node_id=%d\n",idag_id,node_id);
  3254. fd_r = open(fifo_name, O_RDONLY);
  3255. if (fd_r == -1) {
  3256. perror("opening reading pipe");
  3257. signals_enable();
  3258. return;
  3259. } /*else {
  3260. fprintf(log_file, "I went through open\n");
  3261. fflush(log_file);
  3262. } */
  3263. read(fd_r, &agent_id, sizeof(int));
  3264. ////sem_post(&node_sem[sender_id]);
  3265. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  3266. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  3267. if (tmp_inter_list == NULL) {
  3268. if (core_inter_head[agent_id] == NULL){
  3269. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3270. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3271. } else {
  3272. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3273. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3274. }
  3275. core_inter_tail[agent_id]->type = FAR_REQ_MAN;
  3276. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  3277. core_inter_tail[agent_id]->next = NULL;
  3278. printf("I am node with id %d and my far request manager is %d\n",node_id,agent_id);
  3279. fprintf(log_file,"I am node with id %d and my far request manager is %d\n",node_id,agent_id);
  3280. fflush(log_file);
  3281. } else printf("I am node with id %d my far request manager is %d and i am aborting him being appointed again by sender=%d\n",node_id,agent_id,sender_id);
  3282. /*if (agent_id == sender_id) {
  3283. tmp_inter_prev = NULL;
  3284. tmp_inter_list = core_inter_head[agent_id];//core_inter_head[agent_id]->next;
  3285. } else {
  3286. tmp_inter_prev = core_inter_head[agent_id];
  3287. tmp_inter_list = core_inter_head[agent_id]->next;
  3288. }*/
  3289. tmp_inter_prev = core_inter_head[agent_id];
  3290. tmp_inter_list = core_inter_head[agent_id]->next;
  3291. j = 0;
  3292. while (tmp_inter_list != NULL) {
  3293. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == IDAG_REQ_DDS_PENDING) {
  3294. if (tmp_inter_list->type == IDAG_REQ_DDS_PENDING){
  3295. init_DDS_replies++;
  3296. if (init_DDS_idags == init_DDS_replies && init_idags_areas_replies == INIT_AREAS_NUM) j=1; //INIT_AREAS_NUM send_init_reqs(sender_id);
  3297. }
  3298. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,agent_id);
  3299. fflush(log_file);
  3300. if (tmp_inter_prev == NULL) {
  3301. core_inter_head[agent_id] = core_inter_head[agent_id]->next;
  3302. free(tmp_inter_list);
  3303. tmp_inter_list = core_inter_head[agent_id];
  3304. } else {
  3305. tmp_inter_prev->next = tmp_inter_list->next;
  3306. if (tmp_inter_prev->next == NULL) core_inter_tail[agent_id] = tmp_inter_prev;
  3307. free(tmp_inter_list);
  3308. tmp_inter_list = tmp_inter_prev->next;
  3309. }
  3310. }
  3311. else tmp_inter_list = tmp_inter_list->next;
  3312. }
  3313. if (j) send_init_reqs(sender_id);
  3314. //sem_post(&node_sem[node_id]);
  3315. sem_getvalue(&node_sem_out[sender_id],&i);
  3316. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3317. fflush(log_file);
  3318. if (sem_post(&node_sem_out[sender_id]) == -1){
  3319. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3320. perror("sem_post");
  3321. }
  3322. sem_getvalue(&node_sem_out[sender_id],&i);
  3323. fprintf(log_file, "Sem value = %d\n",i);
  3324. fflush(log_file);
  3325. tmp_inter_list = core_inter_head[sender_id];
  3326. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3327. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3328. else send_next_signal(core_inter_head[sender_id], sender_id);
  3329. free(tmp_inter_list);
  3330. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ
  3331. && state != IDLE_INIT_MAN && state != IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  3332. printf("I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  3333. fprintf(log_file,"I am %d and i think i cought a stray init far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  3334. fflush(log_file);
  3335. kill(info->si_pid, SIG_REJECT);
  3336. my_stats.msg_count++;
  3337. my_stats.distance += distance(node_id,sender_id);
  3338. /*kill(info->si_pid, SIG_ACK);
  3339. my_stats.msg_count++;
  3340. my_stats.distance += distance(node_id,sender_id);
  3341. fd_r = open(fifo_name, O_RDONLY);
  3342. if (fd_r == -1) {
  3343. perror("opening reading pipe");
  3344. signals_enable();
  3345. return;
  3346. }
  3347. read(fd_r, &num_of_offers, sizeof(int));
  3348. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  3349. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  3350. for (j=1; j<=num_of_offers; j++){
  3351. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  3352. read(fd_r, &one_offer.spd_loss, sizeof(float));
  3353. for (i=0; i<one_offer.num_of_cores; i++)
  3354. read(fd_r, &tmp_int, sizeof(int));
  3355. core_inter_head[sender_id]->data.offer_acc_array[j] = 0;
  3356. }
  3357. core_inter_head[sender_id]->type = REP_FAR_REQ_OFFER_SENT;
  3358. kill(info->si_pid, SIG_REP_OFFERS);
  3359. my_stats.msg_count++;
  3360. my_stats.distance += distance(node_id,sender_id); */
  3361. tmp_inter_list = core_inter_head[sender_id];
  3362. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3363. free(tmp_inter_list);
  3364. if(core_inter_head[sender_id] == NULL) {
  3365. if (core_inter_tail[sender_id] == NULL){
  3366. printf("Malaka gamietai to core_inter_tail\n");
  3367. core_inter_tail[sender_id] = NULL;
  3368. }
  3369. } else send_next_signal(core_inter_head[sender_id], sender_id);
  3370. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN && signo == SIG_FAR_REQ) {//in original manager get far offers
  3371. sem_getvalue(&node_sem[node_id],&i);
  3372. fprintf(log_file, "In 14 Trying to acquire semaphore. Sem value = %d\n",i);
  3373. fflush(log_file);
  3374. kill(info->si_pid, SIG_ACK);
  3375. sem_wait(&node_sem[node_id]);
  3376. sem_getvalue(&node_sem[node_id],&i);
  3377. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3378. fflush(log_file);
  3379. my_stats.msg_count++;
  3380. my_stats.distance += distance(node_id,sender_id);
  3381. //printf("I am inside with sender_id %d %s\n",sender_id,fifo_name);
  3382. //printf("open14 idag_id=%d node_id=%d\n",idag_id,node_id);
  3383. fd_r = open(fifo_name, O_RDONLY);
  3384. if (fd_r == -1) {
  3385. perror("opening reading pipe");
  3386. signals_enable();
  3387. return;
  3388. } /*else {
  3389. fprintf(log_file, "I went through open\n");
  3390. fflush(log_file);
  3391. } */
  3392. read(fd_r, &num_of_offers, sizeof(int));
  3393. fprintf(log_file,"num of offers = %d\n",num_of_offers);
  3394. fflush(log_file);
  3395. core_inter_head[sender_id]->type = REP_FAR_REQ_OFFER_PENDING;
  3396. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  3397. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  3398. //printf("qweNode %d num_of_offers %d \n",sender_id,num_of_offers);
  3399. for (i=1; i<=num_of_offers; i++){
  3400. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  3401. read(fd_r, &one_offer.spd_loss, sizeof(float));
  3402. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  3403. for (j=0; j<one_offer.num_of_cores; j++)
  3404. read(fd_r, &one_offer.offered_cores[j], sizeof(int));
  3405. //printf("qweNode %d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  3406. core_inter_head[sender_id]->data.offer_acc_array[i] = -1;
  3407. if (init_man_offers == NULL) {
  3408. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  3409. init_man_offers->off = one_offer;
  3410. init_man_offers->sender = sender_id;
  3411. init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  3412. init_man_offers->next = NULL;
  3413. } else {
  3414. tmp_offer_prev = NULL;
  3415. tmp_offer_list = init_man_offers;
  3416. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  3417. tmp_offer_prev = tmp_offer_list;
  3418. tmp_offer_list = tmp_offer_list->next;
  3419. }
  3420. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  3421. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  3422. tmp_offer_list = tmp_offer_prev->next;
  3423. tmp_offer_list->next = NULL;
  3424. tmp_offer_list->off = one_offer;
  3425. tmp_offer_list->sender = sender_id;
  3426. tmp_offer_list->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  3427. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  3428. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  3429. init_man_offers->off = one_offer;
  3430. init_man_offers->sender = sender_id;
  3431. init_man_offers->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  3432. init_man_offers->next = tmp_offer_list;
  3433. } else {
  3434. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  3435. tmp_offer_prev = tmp_offer_prev->next;
  3436. tmp_offer_prev->off = one_offer;
  3437. tmp_offer_prev->sender = sender_id;
  3438. tmp_offer_prev->answer = &core_inter_head[sender_id]->data.offer_acc_array[i];
  3439. tmp_offer_prev->next = tmp_offer_list;
  3440. }
  3441. }
  3442. cur_time = time(NULL);
  3443. cur_t = localtime(&cur_time);
  3444. fprintf(log_file, "[%d:%d:%d]: One node added in init_man_offers in sig_FAR_REQ\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  3445. fflush(log_file);
  3446. }
  3447. sem_getvalue(&node_sem_out[sender_id],&i);
  3448. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3449. fflush(log_file);
  3450. if (sem_post(&node_sem_out[sender_id]) == -1){
  3451. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3452. perror("sem_post");
  3453. }
  3454. sem_getvalue(&node_sem_out[sender_id],&i);
  3455. fprintf(log_file, "Sem value = %d\n",i);
  3456. fflush(log_file);
  3457. //sem_post(&node_sem[node_id]);
  3458. ////sem_post(&node_sem[sender_id]);
  3459. } else {
  3460. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3461. printf("I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  3462. fprintf(log_file,"I am %d and i have to reject sig_far_req from %d. Our interaction is %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  3463. fflush(log_file);
  3464. kill(info->si_pid, SIG_REJECT);
  3465. my_stats.msg_count++;
  3466. my_stats.distance += distance(node_id,sender_id);
  3467. }
  3468. //printf("IIIII I am %d in sig_far_req with sender %d and interaction = %d sig=%d\n",node_id,sender_id,core_inter_head[sender_id]->type,signo);
  3469. //sem_getvalue(&node_sem[node_id],&i);
  3470. //fprintf(log_file, "Sem value = %d\n",i);
  3471. //fflush(log_file);
  3472. if (fd_r != -1) close(fd_r);
  3473. free(fifo_name);
  3474. cur_time = time(NULL);
  3475. cur_t = localtime(&cur_time);
  3476. fprintf(log_file, "[%d:%d:%d]: I ended sig_FAR_REQ_handler with sender=%d and signo=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,signo,state);
  3477. fflush(log_file);
  3478. signals_enable();
  3479. }
  3480. void sig_APPOINT_WORK_handler(int signo, siginfo_t *info, void *context)
  3481. {
  3482. int fd_r, sender_id, i, valid=0;
  3483. char *fifo_name;
  3484. // inter_list *tmp_inter_list;
  3485. signals_disable();
  3486. sender_id = get_id_from_pid(info->si_pid);
  3487. fifo_name = get_pipe_name(node_id);
  3488. cur_time = time(NULL);
  3489. cur_t = localtime(&cur_time);
  3490. fprintf(log_file, "[%d:%d:%d]: I entered sig_APPOINT_WORK_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3491. fflush(log_file);
  3492. if (core_inter_head[sender_id] != NULL) {
  3493. //else {
  3494. printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3495. /*tmp_inter_list = core_inter_head[sender_id];
  3496. while (tmp_inter_list != NULL) {
  3497. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3498. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3499. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  3500. fflush(log_file);
  3501. free(tmp_inter_list);
  3502. tmp_inter_list = core_inter_head[sender_id];
  3503. }*/
  3504. }
  3505. sem_getvalue(&node_sem[node_id],&i);
  3506. fprintf(log_file, "In 15 Trying to acquire semaphore. Sem value = %d\n",i);
  3507. fflush(log_file);
  3508. kill(info->si_pid, SIG_ACK);
  3509. sem_wait(&node_sem[node_id]);
  3510. sem_getvalue(&node_sem[node_id],&i);
  3511. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3512. fflush(log_file);
  3513. my_stats.msg_count++;
  3514. my_stats.distance += distance(node_id,sender_id);
  3515. fd_r = open(fifo_name, O_RDONLY);
  3516. if (fd_r == -1) {
  3517. perror("opening reading pipe");
  3518. signals_enable();
  3519. return;
  3520. } else {
  3521. fprintf(log_file, "I went through open\n");
  3522. fflush(log_file);
  3523. }
  3524. read(fd_r, &valid, sizeof(int));
  3525. fprintf(log_file, "Validity of message = %d\n",valid);
  3526. fflush(log_file);
  3527. if (valid == 1) {
  3528. if (my_agent == -1) read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
  3529. else if (my_agent != sender_id) {
  3530. printf("I am %d and i do in appoint work switch of agent with upper_work_bound = %ld\n",node_id,upper_work_bound);
  3531. fprintf(log_file,"I do in appoint work switch of agent with upper_work_bound = %ld\n",upper_work_bound);
  3532. fflush(log_file);
  3533. //if (upper_work_bound > 0) {//case where my former agent has not collected my remaining work time
  3534. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  3535. if (core_inter_head[my_agent] == NULL){
  3536. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  3537. core_inter_tail[my_agent] = core_inter_head[my_agent];
  3538. } else {
  3539. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  3540. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  3541. }
  3542. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  3543. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  3544. core_inter_tail[my_agent]->next = NULL;
  3545. }
  3546. read(fd_r, &my_agent, sizeof(int));
  3547. }
  3548. //else if (state != WORKING_NODE) printf("I have been initialied but i am not working\n");
  3549. read(fd_r, &upper_work_bound, sizeof(long int));
  3550. time_worked=0;
  3551. ////sem_post(&node_sem[sender_id]);
  3552. printf("I am node %d with agent %d I am going to start working for %ld\n",node_id,my_agent,upper_work_bound);
  3553. if (upper_work_bound > 0) {
  3554. if (state == IDLE_INIT_MAN) state = WORKING_NODE_IDLE_INIT;
  3555. else if (state == INIT_MANAGER || state == INIT_MAN_CHK_OFFERS || state == INIT_MANAGER_SEND_OFFERS) {
  3556. //state = IDLE_INIT_MAN_WORK_PENDING;
  3557. pending_state = WORKING_NODE;
  3558. printf("I am node %d and sou milaw gia mallia\n",node_id);
  3559. } else if (state != WORKING_NODE_IDLE_INIT) state = WORKING_NODE;
  3560. } else {
  3561. kill(info->si_pid, SIG_FINISH);
  3562. my_stats.msg_count++;
  3563. my_stats.distance += distance(node_id,sender_id);
  3564. }
  3565. }
  3566. //sem_post(&node_sem[node_id]);
  3567. sem_getvalue(&node_sem_out[sender_id],&i);
  3568. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3569. fflush(log_file);
  3570. if (sem_post(&node_sem_out[sender_id]) == -1){
  3571. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3572. perror("sem_post");
  3573. }
  3574. sem_getvalue(&node_sem_out[sender_id],&i);
  3575. fprintf(log_file, "Sem value = %d\n",i);
  3576. fflush(log_file);
  3577. //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3578. close(fd_r);
  3579. free(fifo_name);
  3580. cur_time = time(NULL);
  3581. cur_t = localtime(&cur_time);
  3582. fprintf(log_file, "[%d:%d:%d]: I ended sig_APPOINT_WORK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3583. fflush(log_file);
  3584. signals_enable();
  3585. }
  3586. void sig_CHECK_REM_TIME_handler(int signo, siginfo_t *info, void *context)
  3587. {
  3588. int fd_r=-1, sender_id, tmp_rem_time, i;
  3589. char *fifo_name=NULL;
  3590. int time_per_node, time_left, time_to_work, one_core;
  3591. core_list *tmp_cores_list;
  3592. inter_list *tmp_inter_list;
  3593. float rem_workld;
  3594. //inter_list tmp_inter_list;
  3595. signals_disable();
  3596. sender_id = get_id_from_pid(info->si_pid);
  3597. fifo_name = get_pipe_name(node_id);
  3598. cur_time = time(NULL);
  3599. cur_t = localtime(&cur_time);
  3600. fprintf(log_file, "[%d:%d:%d]: I entered sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3601. fflush(log_file);
  3602. if (core_inter_head[sender_id] == NULL && sender_id == my_agent){ //edw tha mpei otan exw allaksei agent
  3603. fprintf(log_file, "I sig_CHECK_REM_TIME_handler a\n");
  3604. fflush(log_file);
  3605. if (core_inter_head[sender_id] == NULL){
  3606. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  3607. core_inter_tail[sender_id] = core_inter_head[sender_id];
  3608. } else {
  3609. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3610. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  3611. }
  3612. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a1\n");
  3613. //fflush(log_file);
  3614. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  3615. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  3616. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  3617. core_inter_tail[sender_id]->next = NULL;
  3618. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  3619. //fflush(log_file);
  3620. upper_work_bound = 0;
  3621. if (state == WORKING_NODE) state = IDLE_CORE;
  3622. kill(info->si_pid, SIG_CHECK_REM_TIME);
  3623. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a3\n");
  3624. //fflush(log_file);
  3625. my_stats.msg_count++;
  3626. my_stats.distance += distance(node_id,sender_id);
  3627. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a4\n");
  3628. //fflush(log_file);
  3629. } else if (core_inter_head[sender_id] == NULL && sender_id != my_agent) {
  3630. //I hope that this is where i end up when my former agent asks me for my remaining work but my original upper_work_bound was 0
  3631. fprintf(log_file, "I sig_CHECK_REM_TIME_handler c\n");
  3632. fflush(log_file);
  3633. if (core_inter_head[sender_id] == NULL){
  3634. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  3635. core_inter_tail[sender_id] = core_inter_head[sender_id];
  3636. } else {
  3637. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3638. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  3639. }
  3640. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  3641. core_inter_tail[sender_id]->data.work_time = 0;
  3642. core_inter_tail[sender_id]->next = NULL;
  3643. kill(info->si_pid, SIG_CHECK_REM_TIME);
  3644. my_stats.msg_count++;
  3645. my_stats.distance += distance(node_id,sender_id);
  3646. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  3647. fprintf(log_file, "I sig_CHECK_REM_TIME_handler b\n");
  3648. fflush(log_file);
  3649. kill(info->si_pid, SIG_CHECK_REM_TIME);
  3650. my_stats.msg_count++;
  3651. my_stats.distance += distance(node_id,sender_id);
  3652. } else if (core_inter_head[sender_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {//CHK_REM_TIME
  3653. //printf("I an in here node_id=%d\n",node_id);
  3654. sem_getvalue(&node_sem[node_id],&i);
  3655. fprintf(log_file, "In 16 Trying to acquire semaphore. Sem value = %d\n",i);
  3656. fflush(log_file);
  3657. kill(info->si_pid, SIG_ACK);
  3658. sem_wait(&node_sem[node_id]);
  3659. sem_getvalue(&node_sem[node_id],&i);
  3660. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3661. fflush(log_file);
  3662. my_stats.msg_count++;
  3663. my_stats.distance += distance(node_id,sender_id);
  3664. fd_r = open(fifo_name, O_RDONLY);
  3665. //printf("I come through here\n");
  3666. if (fd_r == -1) {
  3667. perror("opening reading pipe");
  3668. signals_enable();
  3669. return;
  3670. } /*else {
  3671. fprintf(log_file, "I went through open\n");
  3672. fflush(log_file);
  3673. } */
  3674. if (app_state != RESIZING) {
  3675. printf("I am %d in check rem and app_state = %d app_id = %d\n",node_id,app_state,my_app.id);
  3676. fprintf(log_file,"I am in check rem and app_state = %d app_id = %d\n",app_state,my_app.id);
  3677. fflush(log_file);
  3678. }
  3679. read(fd_r, &tmp_rem_time, sizeof(int));
  3680. fprintf(app_log_file,"tmp_rem_time = %d sender_id = %d\n",tmp_rem_time,sender_id);
  3681. fflush(app_log_file);
  3682. ////sem_post(&node_sem[sender_id]);
  3683. my_stats.app_turnaround -= tmp_rem_time;
  3684. sum_rem_time += tmp_rem_time;
  3685. if (++chk_rem_count == chk_rem_num){
  3686. nodes_ended_cnt = 0;
  3687. rem_workld = sum_rem_time * old_Speedup;
  3688. time_to_work = (int) roundf(rem_workld / my_Speedup);
  3689. if (my_cores_count > 1) {
  3690. time_per_node = time_to_work / (my_cores_count-1);
  3691. time_left = time_to_work % (my_cores_count-1);
  3692. printf("i am %d in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  3693. node_id,sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  3694. fprintf(app_log_file,"i am in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  3695. sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  3696. fflush(app_log_file);
  3697. if (rem_workld > 0.0) {
  3698. if (time_to_work == 0) time_left++;
  3699. tmp_cores_list = my_cores->next;
  3700. while (tmp_cores_list != NULL) {
  3701. one_core = tmp_cores_list->core_id;
  3702. if (core_inter_head[one_core]->type != APPOINT_WORK_NODE_PENDING && core_inter_head[one_core]->type != INIT_WORK_NODE_PENDING) {
  3703. printf("Wrong fucking interaction core = %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  3704. tmp_inter_list = core_inter_head[one_core];
  3705. while (tmp_inter_list != NULL) {
  3706. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  3707. tmp_inter_list = tmp_inter_list->next;
  3708. }
  3709. if (tmp_inter_list == NULL) printf("I am %d and mas ksekwliase o %d\n",node_id,one_core);
  3710. } else tmp_inter_list = core_inter_head[one_core];
  3711. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) tmp_inter_list->type = APPOINT_WORK_NODE;
  3712. else tmp_inter_list->type = INIT_WORK_NODE;
  3713. tmp_inter_list->data.work_time = time_per_node;
  3714. if (time_left > 0) {
  3715. tmp_inter_list->data.work_time++;
  3716. time_left--;
  3717. }
  3718. if (tmp_inter_list->data.work_time > 0) {
  3719. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  3720. //printf("sending core with id=%d\n",one_core);
  3721. if (core_inter_head[one_core]->type == APPOINT_WORK_NODE || core_inter_head[one_core]->type == INIT_WORK_NODE) {//||next == NULL
  3722. kill(pid_num[one_core], SIG_APPOINT_WORK);
  3723. my_stats.msg_count++;
  3724. my_stats.distance += distance(node_id,one_core);
  3725. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  3726. ,core_inter_head[one_core]->next->type);
  3727. } else nodes_ended_cnt++;
  3728. /*core_inter_head[one_core]->data.work_time = time_per_node;
  3729. if (time_left > 0) {
  3730. core_inter_head[one_core]->data.work_time++;
  3731. time_left--;
  3732. }
  3733. if (core_inter_head[one_core]->data.work_time > 0) {
  3734. my_stats.app_turnaround += core_inter_head[one_core]->data.work_time;
  3735. //printf("sending core with id=%d\n",one_core);
  3736. if (core_inter_head[one_core]->next == NULL) {
  3737. kill(pid_num[one_core], SIG_APPOINT_WORK);
  3738. my_stats.msg_count++;
  3739. my_stats.distance += distance(node_id,one_core);
  3740. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  3741. ,core_inter_head[one_core]->next->type);
  3742. } else nodes_ended_cnt++;*/
  3743. tmp_cores_list = tmp_cores_list->next;
  3744. }
  3745. app_state = RUNNING;
  3746. } else {
  3747. //app_terminated = 1;
  3748. app_state = APP_TERMINATED;
  3749. //state = AGENT_ENDING;
  3750. }
  3751. } else {
  3752. printf("I am %d and i am so stupid that i offered my only working core\n",node_id);
  3753. fprintf(app_log_file,"I have only one core state = %d and pending_state = %d\n",state,pending_state);
  3754. fflush(app_log_file);
  3755. if (selfopt_time_rem == -1 && state == IDLE_AGENT) state = AGENT_REWIND_FILE;
  3756. else if (pending_state == IDLE_AGENT) pending_state = AGENT_REWIND_FILE;
  3757. }
  3758. /*if (app_terminated) {
  3759. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3760. if (core_inter_head[tmp_cores_list->core_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[tmp_cores_list->core_id]->type == INIT_WORK_NODE_PENDING){
  3761. tmp_inter_list = core_inter_head[tmp_cores_list->core_id];
  3762. core_inter_head[tmp_cores_list->core_id] = core_inter_head[tmp_cores_list->core_id]->next;
  3763. free(tmp_inter_list);
  3764. printf("I am %d and i pre freed node %d\n",node_id,tmp_cores_list->core_id);
  3765. } else printf("I am %d and i tried to pre free but my interaction with my working node %d is %d\n",
  3766. node_id,tmp_cores_list->core_id,core_inter_head[tmp_cores_list->core_id]->type);
  3767. } else {*/
  3768. if (app_state != APP_TERMINATED) {//!app_terminated
  3769. if (pending_state == INIT_MANAGER) {
  3770. state = INIT_MANAGER;
  3771. pending_state = IDLE_AGENT;
  3772. } else {
  3773. if (selfopt_interval > 0){
  3774. its.it_value.tv_nsec = selfopt_interval * MS;
  3775. selfopt_time_rem = selfopt_interval;
  3776. if (timer_settime(timerid, 0, &its, NULL) == -1) {
  3777. perror("timer_settime error8");
  3778. printf("I am %d timer_settime error8 selfopt_interval = %d\n",node_id,selfopt_interval);
  3779. }
  3780. } else selfopt_time_rem = -1;
  3781. }
  3782. }
  3783. }
  3784. if (core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {
  3785. fprintf(log_file,"I am with sender_id = %d and a\n",sender_id);
  3786. fflush(log_file);
  3787. tmp_inter_list = core_inter_head[sender_id];
  3788. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3789. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3790. else send_next_signal(core_inter_head[sender_id], sender_id);
  3791. free(tmp_inter_list);
  3792. fprintf(log_file,"I am with sender_id = %d and b\n",sender_id);
  3793. fflush(log_file);
  3794. }
  3795. //printf("I am node %d my agent is %d and I am going to start working\n",node_id,my_agent);
  3796. //sem_post(&node_sem[node_id]);
  3797. sem_getvalue(&node_sem_out[sender_id],&i);
  3798. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  3799. fflush(log_file);
  3800. if (sem_post(&node_sem_out[sender_id]) == -1){
  3801. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  3802. perror("sem_post");
  3803. }
  3804. sem_getvalue(&node_sem_out[sender_id],&i);
  3805. fprintf(log_file, "Sem value = %d\n",i);
  3806. fflush(log_file);
  3807. } else if (sender_id == my_agent){
  3808. printf("I am %d and i am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3809. fprintf(log_file,"I am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  3810. fflush(log_file);
  3811. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3812. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  3813. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  3814. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  3815. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  3816. core_inter_tail[sender_id]->next = NULL;
  3817. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  3818. //fflush(log_file);
  3819. upper_work_bound = 0;
  3820. if (state == WORKING_NODE) state = IDLE_CORE;
  3821. } else {
  3822. printf("I am %d and i am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3823. fprintf(log_file,"I am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  3824. fflush(log_file);
  3825. kill(info->si_pid, SIG_REJECT);
  3826. my_stats.msg_count++;
  3827. my_stats.distance += distance(node_id,sender_id);
  3828. }
  3829. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a5\n");
  3830. //fflush(log_file);
  3831. if (fd_r != -1) close(fd_r);
  3832. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a6\n");
  3833. //fflush(log_file);
  3834. free(fifo_name);
  3835. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a7\n");
  3836. //fflush(log_file);
  3837. cur_time = time(NULL);
  3838. cur_t = localtime(&cur_time);
  3839. fprintf(log_file, "[%d:%d:%d]: I ended sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3840. fflush(log_file);
  3841. signals_enable();
  3842. }
  3843. void sig_FINISH_handler(int signo, siginfo_t *info, void *context)
  3844. {
  3845. int fd_r, i, is_sender_idag, sender_id, new_agent_id;
  3846. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  3847. int *tmp_list, tmp_cnt, tmp_idag;
  3848. char *fifo_name;
  3849. core_list *tmp_cores_list;//tmp_cores,
  3850. DDS_list *tmp_DDS,*prev_DDS;
  3851. inter_list *tmp_inter_list;
  3852. sender_id = get_id_from_pid(info->si_pid);
  3853. fifo_name = get_pipe_name(node_id);
  3854. cur_time = time(NULL);
  3855. cur_t = localtime(&cur_time);
  3856. fprintf(log_file, "[%d:%d:%d]: I entered sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3857. fflush(log_file);
  3858. //if i am an idag, sig_finish should be proceed instantly, so i am dropping.
  3859. //On the other hand if an common node doesn't set its agent to -1 its not a big deal. (Hopefully)
  3860. if (core_inter_head[sender_id] != NULL && my_idag == -1) {
  3861. //else {
  3862. printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3863. if (my_idag == -1) {
  3864. is_sender_idag = 0;
  3865. for (i=0; i<num_idags; i++)
  3866. if (idag_id_arr[i] == sender_id){
  3867. is_sender_idag = 1;
  3868. break;
  3869. }
  3870. if (!is_sender_idag) {
  3871. tmp_inter_list = core_inter_head[sender_id];
  3872. while (tmp_inter_list != NULL) {
  3873. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3874. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3875. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  3876. fflush(log_file);
  3877. free(tmp_inter_list);
  3878. tmp_inter_list = core_inter_head[sender_id];
  3879. }
  3880. }
  3881. }
  3882. }
  3883. if (my_idag != -1){
  3884. if (my_agent != -1 && my_agent == sender_id) my_agent = -1;//working node
  3885. if (++nodes_ended_cnt == (my_cores_count - 1)) app_state = APP_TERMINATED;//app_terminated = 1;
  3886. } else {
  3887. sem_getvalue(&node_sem[node_id],&i);
  3888. fprintf(log_file, "In 17 Trying to acquire semaphore. Sem value = %d\n",i);
  3889. fflush(log_file);
  3890. kill(info->si_pid, SIG_ACK);
  3891. sem_wait(&node_sem[node_id]);
  3892. sem_getvalue(&node_sem[node_id],&i);
  3893. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  3894. fflush(log_file);
  3895. my_stats.msg_count++;
  3896. my_stats.distance += distance(node_id,sender_id);
  3897. fd_r = open(fifo_name, O_RDONLY);
  3898. if (fd_r == -1) {
  3899. perror("opening reading pipe");
  3900. signals_enable();
  3901. return;
  3902. } /*else {
  3903. fprintf(log_file, "I went through open\n");
  3904. fflush(log_file);
  3905. } */
  3906. is_sender_idag = 0;
  3907. for (i=0; i<num_idags; i++)
  3908. if (idag_id_arr[i] == sender_id){
  3909. is_sender_idag = 1;
  3910. break;
  3911. }
  3912. printf("I am %d in sig finish with is_sender_idag = %d\n",node_id,is_sender_idag);
  3913. if (is_sender_idag == 0){
  3914. read(fd_r, &nodes_cnt, sizeof(int));
  3915. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  3916. for (i=0; i<nodes_cnt; i++)
  3917. read(fd_r, &nodes_to_process[i], sizeof(int));
  3918. ////sem_post(&node_sem[sender_id]);
  3919. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  3920. printf("I am node_id=%d Cores to be processed: ",node_id);
  3921. for (i=0; i<nodes_cnt; i++)
  3922. printf(" %d",nodes_to_process[i]);
  3923. printf("\n");
  3924. printf("I am node_id=%d nodes_cnt = %d\n",node_id,nodes_cnt);
  3925. while (processed_cnt < nodes_cnt){
  3926. tmp_cnt = 0;
  3927. tmp_idag = -1;
  3928. for (i=0; i<nodes_cnt; i++){
  3929. if (processed_cnt == nodes_cnt) break;
  3930. if (nodes_to_process[i] == -1) continue;
  3931. else {
  3932. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  3933. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  3934. else {
  3935. tmp_list[tmp_cnt++] = nodes_to_process[i];
  3936. nodes_to_process[i] = -1;
  3937. processed_cnt++;
  3938. }
  3939. }
  3940. }
  3941. if (tmp_idag != node_id){
  3942. if (core_inter_head[tmp_idag] == NULL){
  3943. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  3944. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  3945. } else {
  3946. printf("finish agent DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  3947. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  3948. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  3949. }
  3950. printf("node_id = %d In finish tmp_idag = %d tmp_cnt = %d sender_id = %d is_sender_idag=%d tmp_list[0]=%d\n",node_id,tmp_idag,tmp_cnt,sender_id,is_sender_idag,tmp_list[0]);
  3951. core_inter_tail[tmp_idag]->type = REMOVE_APP;
  3952. core_inter_tail[tmp_idag]->data.agent_ended = sender_id;
  3953. /*core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  3954. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  3955. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  3956. for (j=1; j<= tmp_cnt; j++)
  3957. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  3958. */
  3959. core_inter_tail[tmp_idag]->next = NULL;
  3960. //if (tmp_idag != 0) why???
  3961. if (core_inter_head[tmp_idag]->next == NULL) {
  3962. kill(pid_num[tmp_idag], SIG_FINISH);
  3963. my_stats.msg_count++;
  3964. my_stats.distance += distance(node_id,tmp_idag);
  3965. }
  3966. } else {
  3967. printf("In FINISH same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  3968. new_agent_id = sender_id;
  3969. /*for (i=0; i<tmp_cnt; i++){
  3970. tmp_cores = my_cores;
  3971. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  3972. tmp_cores = tmp_cores->next;
  3973. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  3974. }
  3975. DDS->num_of_cores += tmp_cnt;*/
  3976. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3977. if (tmp_cores_list->offered_to == new_agent_id) {
  3978. tmp_cores_list->offered_to = -1;
  3979. DDS->num_of_cores++;
  3980. }
  3981. prev_DDS = DDS;
  3982. tmp_DDS = DDS->next;
  3983. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  3984. prev_DDS = tmp_DDS;
  3985. tmp_DDS = tmp_DDS->next;
  3986. }
  3987. if (tmp_DDS != NULL){
  3988. DDS_count--;
  3989. prev_DDS->next = tmp_DDS->next;
  3990. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  3991. free(tmp_DDS);
  3992. } else printf("Fuck agent that finished is not in my DDS\n");
  3993. printf("My removal of agent complete node_id = %d sender_id=%d\n",node_id,sender_id);
  3994. printf("Number of agents in region = %d\n",DDS_count);
  3995. i=0;
  3996. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  3997. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  3998. i++;
  3999. }
  4000. }
  4001. }
  4002. free(nodes_to_process);
  4003. free(tmp_list);
  4004. } else {
  4005. /*read(fd_r, &nodes_cnt, sizeof(int));
  4006. printf("I am %d and secondary node cnt by %d is %d\n",node_id,sender_id,nodes_cnt);
  4007. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  4008. for (i=0; i<nodes_cnt; i++)
  4009. read(fd_r, &nodes_to_process[i], sizeof(int));
  4010. read(fd_r, &new_agent_id, sizeof(int));
  4011. ////sem_post(&node_sem[sender_id]);
  4012. printf("Secondary Cores to be processed of %d by %d: ",new_agent_id,node_id);
  4013. for (i=0; i<nodes_cnt; i++)
  4014. printf(" %d",nodes_to_process[i]);
  4015. printf("\n");
  4016. for (i=0; i<nodes_cnt; i++){
  4017. tmp_cores = my_cores;
  4018. while (tmp_cores->core_id != nodes_to_process[i])//nodes_to_process[i]
  4019. tmp_cores = tmp_cores->next;
  4020. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  4021. }
  4022. DDS->num_of_cores += nodes_cnt;
  4023. */
  4024. read(fd_r, &new_agent_id, sizeof(int));
  4025. printf("I am %d in Secondary sig_finish for %d\n",node_id,new_agent_id);
  4026. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  4027. if (tmp_cores_list->offered_to == new_agent_id) {
  4028. tmp_cores_list->offered_to = -1;
  4029. DDS->num_of_cores++;
  4030. }
  4031. prev_DDS = DDS;
  4032. tmp_DDS = DDS->next;
  4033. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  4034. prev_DDS = tmp_DDS;
  4035. tmp_DDS = tmp_DDS->next;
  4036. }
  4037. if (tmp_DDS != NULL){
  4038. DDS_count--;
  4039. prev_DDS->next = tmp_DDS->next;
  4040. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  4041. free(tmp_DDS);
  4042. } else printf("Fuck agent that finished is not in my DDS\n");
  4043. //free(nodes_to_process);
  4044. printf("Secondary removal of agent complete node_id =%d sender_id=%d\n",node_id,sender_id);
  4045. printf("Number of agents in region = %d\n",DDS_count);
  4046. i=0;
  4047. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  4048. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  4049. i++;
  4050. }
  4051. }
  4052. sem_getvalue(&node_sem_out[sender_id],&i);
  4053. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  4054. fflush(log_file);
  4055. if (sem_post(&node_sem_out[sender_id]) == -1){
  4056. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  4057. perror("sem_post");
  4058. }
  4059. sem_getvalue(&node_sem_out[sender_id],&i);
  4060. fprintf(log_file, "Sem value = %d\n",i);
  4061. fflush(log_file);
  4062. close(fd_r);
  4063. }
  4064. //sem_post(&node_sem[node_id]);
  4065. //} else printf("I am %d in sig finish with sender %d and i don't know what to do with interaction %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  4066. //free(fifo_name);
  4067. cur_time = time(NULL);
  4068. cur_t = localtime(&cur_time);
  4069. fprintf(log_file, "[%d:%d:%d]: I ended sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  4070. fflush(log_file);
  4071. signals_enable();
  4072. }
  4073. void sig_REJECT_handler(int signo, siginfo_t *info, void *context)
  4074. {
  4075. int sender_id, agent_id, i, j;//, crit_app_id;//fd_r,
  4076. //char *fifo_name;
  4077. inter_list *tmp_inter_list;
  4078. core_list *tmp_cores_list;
  4079. target_list *tmp_target_list;
  4080. offer_list *tmp_offer_list;
  4081. signals_disable();
  4082. //inter_list tmp_inter_list;
  4083. sender_id = get_id_from_pid(info->si_pid);
  4084. //fifo_name = get_pipe_name(node_id);
  4085. cur_time = time(NULL);
  4086. cur_t = localtime(&cur_time);
  4087. fprintf(log_file, "[%d:%d:%d]: I entered sig_REJECT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  4088. fflush(log_file);
  4089. if (core_inter_head[sender_id] != NULL){
  4090. fprintf(log_file, "[%d:%d:%d]: Interaction with sender=%d is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,core_inter_head[sender_id]->type);
  4091. fflush(log_file);
  4092. }
  4093. if (core_inter_head[sender_id] == NULL){ //edw tha mpei otan exw allaksei agent
  4094. printf("I am %d in sig_reject and i have null interaction with sender %d\n",node_id,sender_id);
  4095. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS
  4096. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  4097. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  4098. init_DDS_replies++;
  4099. fprintf(log_file, "One init_req_dds has been rejected by %d!\n",sender_id);
  4100. fflush(log_file);
  4101. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) {
  4102. selfopt_DDS_replies++;
  4103. fprintf(log_file, "One selfopt_req_dds has been rejected by %d!\n",sender_id);
  4104. fflush(log_file);
  4105. }
  4106. //printf("selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  4107. if ((core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING)
  4108. && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == INIT_AREAS_NUM)//INIT_AREAS_NUM
  4109. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  4110. agent_id = tmp_target_list->target;
  4111. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  4112. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  4113. if (tmp_inter_list == NULL) {
  4114. if (core_inter_head[agent_id] == NULL){
  4115. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  4116. core_inter_tail[agent_id] = core_inter_head[agent_id];
  4117. } else {
  4118. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  4119. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  4120. }
  4121. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  4122. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  4123. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  4124. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  4125. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  4126. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  4127. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  4128. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  4129. }
  4130. fprintf(log_file,"\n");
  4131. fflush(log_file);
  4132. core_inter_tail[agent_id]->next = NULL;
  4133. //kill(pid_num[agent_id], SIG_REQ_CORES);
  4134. if (core_inter_head[agent_id]->next == NULL) {
  4135. kill(pid_num[agent_id], SIG_REQ_CORES);
  4136. my_stats.msg_count++;
  4137. my_stats.distance += distance(node_id,agent_id);
  4138. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  4139. kill(pid_num[agent_id], SIG_REQ_CORES);
  4140. my_stats.msg_count++;
  4141. my_stats.distance += distance(node_id,agent_id);
  4142. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  4143. } else
  4144. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  4145. }
  4146. else if ((core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) && selfopt_DDS_replies == selfopt_DDS_idags)
  4147. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  4148. agent_id = tmp_target_list->target;
  4149. if (core_inter_head[agent_id] == NULL){
  4150. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  4151. core_inter_tail[agent_id] = core_inter_head[agent_id];
  4152. } else {
  4153. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  4154. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  4155. }
  4156. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  4157. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  4158. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  4159. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  4160. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  4161. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  4162. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  4163. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  4164. }
  4165. fprintf(log_file,"\n");
  4166. fflush(log_file);
  4167. core_inter_tail[agent_id]->next = NULL;
  4168. if (core_inter_head[agent_id]->next == NULL) {
  4169. kill(pid_num[agent_id], SIG_REQ_CORES);
  4170. my_stats.msg_count++;
  4171. my_stats.distance += distance(node_id,agent_id);
  4172. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  4173. kill(pid_num[agent_id], SIG_REQ_CORES);
  4174. my_stats.msg_count++;
  4175. my_stats.distance += distance(node_id,agent_id);
  4176. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  4177. }
  4178. tmp_inter_list = core_inter_head[sender_id];
  4179. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4180. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4181. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  4182. send_next_signal(core_inter_head[sender_id], sender_id);
  4183. free(tmp_inter_list);
  4184. } else if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_CORES
  4185. || core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  4186. || core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING
  4187. || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING
  4188. || core_inter_head[sender_id]->type == REP_FAR_INIT_REQ) {
  4189. tmp_inter_list = core_inter_head[sender_id];
  4190. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4191. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4192. else send_next_signal(core_inter_head[sender_id], sender_id);
  4193. free(tmp_inter_list);
  4194. } else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER) {//FAR_REQ_OFFER_SENT
  4195. //fprintf(log_file, "a\n");
  4196. //fflush(log_file);
  4197. tmp_cores_list = my_cores;
  4198. while (tmp_cores_list != NULL){
  4199. if (tmp_cores_list->offered_to == sender_id) tmp_cores_list->offered_to = -1;
  4200. tmp_cores_list = tmp_cores_list->next;
  4201. }
  4202. //fprintf(log_file, "b\n");
  4203. //fflush(log_file);
  4204. tmp_offer_list = far_man_offers;
  4205. far_man_offers = far_man_offers->next;
  4206. free(tmp_offer_list);
  4207. //fprintf(log_file, "c\n");
  4208. //fflush(log_file);
  4209. //if (far_man_offers == NULL) {
  4210. // fprintf(log_file, "karioles\n");
  4211. // fflush(log_file);
  4212. //}
  4213. while (far_man_offers != NULL){
  4214. printf("I am node %d in reject and far offer answer for node %d is 0\n",node_id,far_man_offers->sender);
  4215. *far_man_offers->answer = 0;
  4216. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  4217. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  4218. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  4219. } else printf("I am %d and Apparently lists are poutana ola\n",node_id);
  4220. tmp_offer_list = far_man_offers;
  4221. far_man_offers = far_man_offers->next;
  4222. free(tmp_offer_list);
  4223. }
  4224. far_req_app.A=-1.0;
  4225. far_req_app.var=-1.0;
  4226. far_req_app.num_of_cores=-1;
  4227. far_req_or_sender = -1;
  4228. tmp_inter_list = core_inter_head[sender_id];
  4229. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4230. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4231. else send_next_signal(core_inter_head[sender_id], sender_id);
  4232. free(tmp_inter_list);
  4233. } else if (core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS) {
  4234. free(core_inter_head[sender_id]->data.idags_in_reg);
  4235. tmp_inter_list = core_inter_head[sender_id];
  4236. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4237. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4238. else send_next_signal(core_inter_head[sender_id], sender_id);
  4239. free(tmp_inter_list);
  4240. } else if (core_inter_head[sender_id]->type == REP_IDAG_REQ_DDS) {
  4241. free(core_inter_head[sender_id]->data.agents_in_reg);
  4242. tmp_inter_list = core_inter_head[sender_id];
  4243. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4244. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4245. else send_next_signal(core_inter_head[sender_id], sender_id);
  4246. free(tmp_inter_list);
  4247. } else if (core_inter_head[sender_id]->type == FAR_REQ_MAN_APPOINT) {
  4248. agent_id = core_inter_head[sender_id]->data.far_req.orig_sender;
  4249. if (core_inter_head[agent_id] == NULL){
  4250. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  4251. core_inter_tail[agent_id] = core_inter_head[agent_id];
  4252. } else {
  4253. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  4254. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  4255. }
  4256. core_inter_tail[agent_id]->type = ABORT_FAR_MAN;
  4257. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  4258. core_inter_tail[agent_id]->next = NULL;
  4259. if (core_inter_head[agent_id]->next == NULL) {
  4260. kill(pid_num[agent_id], SIG_REMOVE_FAR_MAN);
  4261. my_stats.msg_count++;
  4262. my_stats.distance += distance(node_id,agent_id);
  4263. }
  4264. tmp_inter_list = core_inter_head[sender_id];
  4265. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4266. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4267. else send_next_signal(core_inter_head[sender_id], sender_id);
  4268. free(tmp_inter_list);
  4269. } else if (core_inter_head[sender_id]->type == REP_AGENT_REQ_CORES) {
  4270. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++)
  4271. if (my_cores != NULL)
  4272. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  4273. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  4274. if (tmp_cores_list->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  4275. tmp_cores_list->offered_to = -1;
  4276. break;
  4277. }
  4278. free(core_inter_head[sender_id]->data.off_arr.offer_arr);
  4279. tmp_inter_list = core_inter_head[sender_id];
  4280. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4281. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4282. else send_next_signal(core_inter_head[sender_id], sender_id);
  4283. free(tmp_inter_list);
  4284. } else if (core_inter_head[sender_id]->type == INIT_APP) {
  4285. printf("I am 0 and %d rejected my init_app req\n",sender_id);
  4286. fprintf(log_file,"I am 0 and %d rejected my init_app req\n",sender_id);
  4287. fflush(log_file);
  4288. if (init_pending_head == NULL){
  4289. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  4290. init_pending_tail = init_pending_head;
  4291. } else {
  4292. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  4293. init_pending_tail = init_pending_tail->next;
  4294. }
  4295. init_pending_tail->type = INIT_APP;
  4296. init_pending_tail->data.new_app = core_inter_head[sender_id]->data.new_app;
  4297. init_pending_tail->data.new_app.num_of_cores = sender_id;
  4298. init_pending_tail->next = NULL;
  4299. tmp_inter_list = core_inter_head[sender_id];
  4300. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4301. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4302. else send_next_signal(core_inter_head[sender_id], sender_id);
  4303. free(tmp_inter_list);
  4304. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  4305. printf("I am %d and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",node_id,sender_id,core_inter_head[sender_id]->data.work_time);
  4306. fprintf(log_file,"I am and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",sender_id,core_inter_head[sender_id]->data.work_time);
  4307. fflush(log_file);
  4308. tmp_inter_list = core_inter_head[sender_id];
  4309. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4310. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4311. else send_next_signal(core_inter_head[sender_id], sender_id);
  4312. free(tmp_inter_list);
  4313. } else printf("I am %d in sig_reject and i have interaction with sender %d interaction = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  4314. cur_time = time(NULL);
  4315. cur_t = localtime(&cur_time);
  4316. fprintf(log_file, "[%d:%d:%d]: I ended sig_REJECT_handler with sender = %d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  4317. fflush(log_file);
  4318. //fprintf(log_file, "[%ld:%ld:%ld:%ld]: my idle agent is %d and my pid is %d\n",tv.tv_sec/3600,tv.tv_sec%3600/60,tv.tv_sec%3600%60,(long int) tv.tv_usec,my_idag,getpid());
  4319. //close(fd_r);
  4320. //free(fifo_name);
  4321. signals_enable();
  4322. /*signals_disable();
  4323. state = TERMINATED;
  4324. signals_enable();*/
  4325. }
  4326. void sig_REMOVE_FAR_MAN_handler(int signo, siginfo_t *info, void *context)
  4327. {
  4328. int fd_r, sender_id, i, my_agent;
  4329. char *fifo_name;
  4330. inter_list *tmp_inter_list, *tmp_inter_prev;
  4331. signals_disable();
  4332. sender_id = get_id_from_pid(info->si_pid);
  4333. fifo_name = get_pipe_name(node_id);
  4334. cur_time = time(NULL);
  4335. cur_t = localtime(&cur_time);
  4336. fprintf(log_file, "[%d:%d:%d]: I entered sig_REMOVE_FAR_MAN_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  4337. fflush(log_file);
  4338. if (core_inter_head[sender_id] != NULL) {
  4339. printf("I am %d and i in am doing smth else with my agent %d in sig_REMOVE_FAR_MAN_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  4340. /*tmp_inter_list = core_inter_head[sender_id];
  4341. while (tmp_inter_list != NULL) {
  4342. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  4343. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  4344. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  4345. fflush(log_file);
  4346. free(tmp_inter_list);
  4347. tmp_inter_list = core_inter_head[sender_id];
  4348. }*/
  4349. }
  4350. sem_getvalue(&node_sem[node_id],&i);
  4351. fprintf(log_file, "In REMOVE_FAR_MAN Trying to acquire semaphore. Sem value = %d\n",i);
  4352. fflush(log_file);
  4353. kill(info->si_pid, SIG_ACK);
  4354. sem_wait(&node_sem[node_id]);
  4355. sem_getvalue(&node_sem[node_id],&i);
  4356. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  4357. fflush(log_file);
  4358. my_stats.msg_count++;
  4359. my_stats.distance += distance(node_id,sender_id);
  4360. fd_r = open(fifo_name, O_RDONLY);
  4361. if (fd_r == -1) {
  4362. perror("opening reading pipe");
  4363. signals_enable();
  4364. return;
  4365. } /*else {
  4366. fprintf(log_file, "I went through open\n");
  4367. fflush(log_file);
  4368. }*/
  4369. read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
  4370. tmp_inter_prev = NULL;
  4371. tmp_inter_list = core_inter_head[my_agent];
  4372. while (tmp_inter_list != NULL) {
  4373. //the far manager being clear had to be the one tha has been appointed by sender
  4374. if (tmp_inter_list->type == FAR_REQ_MAN && tmp_inter_list->data.far_req_man == sender_id) {
  4375. fprintf(log_file, "I dismissed type = %d of node %d sender = %d\n",tmp_inter_list->type,my_agent,sender_id);
  4376. fflush(log_file);
  4377. if (tmp_inter_prev == NULL) {
  4378. core_inter_head[my_agent] = core_inter_head[my_agent]->next;
  4379. if (core_inter_head[my_agent] == NULL) core_inter_tail[my_agent] = NULL;
  4380. else send_next_signal(core_inter_head[my_agent], my_agent);
  4381. } else {
  4382. tmp_inter_prev->next = tmp_inter_list->next;
  4383. if (tmp_inter_prev->next == NULL) core_inter_tail[my_agent] = tmp_inter_prev;
  4384. }
  4385. free(tmp_inter_list);
  4386. break;
  4387. //tmp_inter_list = tmp_inter_prev->next;
  4388. }
  4389. else tmp_inter_list = tmp_inter_list->next;
  4390. }
  4391. //sem_post(&node_sem[node_id]);
  4392. sem_getvalue(&node_sem_out[sender_id],&i);
  4393. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  4394. fflush(log_file);
  4395. if (sem_post(&node_sem_out[sender_id]) == -1){
  4396. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  4397. perror("sem_post");
  4398. }
  4399. sem_getvalue(&node_sem_out[sender_id],&i);
  4400. fprintf(log_file, "Sem value = %d\n",i);
  4401. fflush(log_file);
  4402. //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  4403. close(fd_r);
  4404. free(fifo_name);
  4405. cur_time = time(NULL);
  4406. cur_t = localtime(&cur_time);
  4407. fprintf(log_file, "[%d:%d:%d]: I ended REMOVE_FAR_MAN with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  4408. fflush(log_file);
  4409. signals_enable();
  4410. }